Mysql read-write separate construction-basic construction (1)
1. Basic Preparation
(1) Install two machines, one machine as master and one machine as slave
(2) Install the mysql version as much as possible. This article recommends mysql8.0 to ensure mutual visits. If it is a virtual machine, please set to bridge mode.
(3) Installation steps are recommended for everyone to read
https://blog.csdn.net/qq_37350706/article/details/81707862
2. Basic commands
#Start mysql service
net start mysql #Stop
mysql service
net stop mysql #Modify the
mysql user password
mysqladmin -uroot -p password
#How to log in to mysql under windows
(1) win + R Enter cmd to enter the console
(2) Enter mysql -u root -p Enter the password to complete the login. The commands in the subsequent chapters can only be executed after logging in to mysql.
3. Configuration
3.1 Server configuration
(1) Modify the configuration file
[mysqld]
#以下为主从读写配置
server-id=1
#备份数据库 如果是多个数据库 就配置第二行 如test、test1
binlog-do-db=test
#binlog-do-db=test1
#可以忽略的数据库 这里如果不填写 应该就不执行了
#binlog-ignore-do-db=test2
#备份方式
log-bin=mysql-bin
(2) New users
create user 'repl'@'%' identified by 'repl';
grant replication slave on *.* to 'repl'@'%' ;
#给用户授予相应的数据库操作权限 测试的数据库是test
grant all privileges on test.* to 'repl'@'%';
flush privileges;
(3) Verify the configuration effect
show variables like 'server_id';
(4) Collect host data
show master status\G
File parameter: specifies the name of the log file to be synchronized.
Position parameter: This node needs to pay attention, it means that the data log has been written to this location, then we copy the data from here.
3.2 Client configuration
[mysqld]
#以下为主从读写配置
server-id=2
#备份数据库 如果是多个数据库 就配置第二行 如test、test1
binlog-do-db=test
log-bin=mysql-bin
4. Start synchronization
4.1 Basic data synchronization
Before synchronization, you need to synchronize the data in the master library to the slave library, and keep the data consistent before the two libraries are synchronized. You can use the master library to export scripts and import from the library.
4.2 Collect server information
Execute commands on the server
show master status\G
Pay attention to the two nodes of File and Position.
4.3 The client performs synchronization
#暂停客户端服务
stop slave;
In the configuration parameters
MASTER_HOST = server IP
MASTER_USER = server user
MASTER_PASSWORD = server password
MASTER_PORT = server port
MASTER_LOG_FILE = server synchronization file (corresponding to the File parameter collected by the 4.2 command)
MASTER_LOG_POS = server synchronization file (corresponding to the 4.2 command collection Position parameter)
#配置同步参数
CHANGE MASTER TO MASTER_HOST= '169.254.210.192', MASTER_USER='repl', MASTER_PASSWORD='repl', MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000003',MASTER_CONNECT_RETRY=30,MASTER_LOG_POS=1023;
#启动客户端服务
start slave;
#显示同步结果
show slave status\G
If these two items are yes, you can basically meet the needs of synchronization
Slave_IO_Runing: YES
Slave_SQL_Runing: YES
execution here, you can synchronize between the two databases.