环境:
主机:192.168.53.81
从机:192.168.53.82
一、查看主库mysql版本号,从库的mysql版本号要和主库一致。
[root@53-81 ~]# /usr/local/mysql/bin/mysql -V
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.1.60, for unknown-linux-gnu(x86_64) using readline 5.1
创建测试表
# mysql -p123456
#use test;
mysql> create table t2 (id int(4));
mysql> insert into t2 values (1);
二、从库安装相同版本的mysql数据库。
1,下载相同版本的mysql源码包。
2,查看主机mysql编译参数。grep configure /usr/local/mysql/bin/mysqlbug
3,开始安装。可以写成批处理文件。
tar zxvf mysql-5.1.60.tar.gz
cd mysql-5.1.60
./configure '--prefix=/usr/local/mysql' '--with-extra-charsets=complex''--enable-assembler' '--with-mysqld-ldflags=-all-static' '--with-charset=utf8''--enable-thread-safe-client' '--with-big-tables' '--with-readline''--with-ssl' '--with-embedded-server' '--enable-local-infile'
make&& make install
cd ../
groupaddmysql
useradd-s /sbin/nologin -M -g mysql mysql
cp/usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
sed -i's/skip-locking/skip-external-locking/g' /etc/my.cnf
/usr/local/mysql/bin/mysql_install_db--user=mysql
chown -Rmysql /usr/local/mysql/var
chgrp -Rmysql /usr/local/mysql/.
cp/usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod 755/etc/init.d/mysql
cat >/etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/mysql
/usr/local/lib
EOF
ldconfig
ln -s/usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s/usr/local/mysql/include/mysql /usr/include/mysql
/etc/init.d/mysqlstart
ln -s/usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s/usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -s/usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
/usr/local/mysql/bin/mysqladmin-u root password 123456
三、主库创建/etc/my.cnf,修改里边的键值增加
server-id=1
log-bin=binlog_name
重启数据库,增加日志功能
四、主库增加用户,用于从库读取主库日志。
mysql> grant all on *.* toslave@'192.168.53.82' identified by 'slavekjh-123';
五、从库的操作
1,从库连接主库进行测试。
# mysql -u slave -pslavekjh-123 -h192.168.53.81
如果连接成功说明主库配置成功
2,停从库,
/etc/init.d/mysql stop
修改从库/etc/my.cnf,增加选项:
server-id=2
skip-slave-start
replicate-do-db=test #需要同步的数据库
replicate-do-db=test2 #需要同步的数据库
replicate-do-db=email #需要同步的数据库
replicate-do-db=ttreport #需要同步的数据库
3,启动从库,进行主从库数据同步
/etc/init.d/mysql start
六,同步数据。
1,主库锁定数据库
mysql> flush tables with read lock;
测试是否锁定
mysql> use test;
mysql> insert into t2 values (1);
ERROR 1223 (HY000): Can't execute the querybecause you have a conflicting read lock
查看主库状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position |Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000033 | 74195486 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
2,从库开始同步
## mysql -p123456
mysql> change master tomaster_host='192.168.53.81',master_user='slave',master_password='slavekjh-123',master_port=3306,master_log_file='mysql-bin.000033',master_log_pos=74195486;
开始同步主库
mysql>load data from master;
开始同步
slave start ;
查看同步状态
show slave status/G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
删除/etc/my.cnf
skip-slave-start
3,主库操作
解锁:
unlock tables;
插入数据看是否同步。
mysql> use test;
mysql> insert into t2 values (123456);
4,从库查看是否同步成功
mysql> use test;
Database changed
mysql> select * from t2;
完成===============================================================
测试
一,从库停止一段时间后,再启动是否能自动同步?是的。
二,主库停止一段时间后,主库启动,从库能否自动同步?是的。
问题1:同步主库时出现Net error reading from master 报错
原因:load table from master只支持myisam表,如果试图载入一个非MyISAM表,会导致以下错误。如果抛出这个错误,确保所有在主表是MyISAM。如果是这样,请重试运行的SQL语句。在InnoDB表或其他引擎的数据库的情况下,使用mysqldump从初始化。
解决方法:

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment