MySQL master-slave synchronization
My configuration: MySQL 8.0, Centos 7 (master), Windows 10 (slave)
Step 1: Check if binlog is enabled
Starting from MySQL8.0, binlog is enabled by default. If it is version 5.7, it can also be configured and enabled in my.cnf/my.ini.
show variables like '%log_bin%';
the host
Slave
Step 2: Set up the configuration file
the host
The MySQL configuration file under Linux is in /etc/my.cnf by default, and can be edited using the vim command
vim /etc/my.cnf
Configure the information under [mysqld]. Note that it is under [mysqld]. If it is configured in other places, it will not be read.
The binlog-do-db here can not be configured, according to the needs.
Slave
The MySQL configuration file under Windows is in C:\Program Files\MySQL by default, open my.ini for editing
Similarly, configure information under [mysqld]
Step 3: Restart the service
the host
MySQL restart command under Linux
systemctl restart mysqld.service
Slave
Open cmd, first enter net stop mysql, and then enter net start mysql
net stop mysql
net start mysql
Step 4: Set up an account
the host
Usually, in the master-slave synchronization business scenario, an account is given to the slave library. Normally, the root account is not given. Here I will use the account zhku I created before for demonstration (you can also use the root account, but it is not recommended).
First check whether the account has the master-slave synchronization permission
select Repl_slave_priv, Repl_client_priv from mysql.user where user = 'zhku';
It shows that the permission is not granted
grant permission
grant replication slave, replication client on *.* to 'zhku'@'%';
Step Five: Check Status
the host
View master status
show master status;
file: Indicates which file the synchronized bin-log information starts from;
position: Indicates where to start from the file;
binlog_do_db: Indicates which library to synchronize (if not set in the configuration file, it is empty)
Slave
Set the master server IP address on the slave
change master to master_host='120.***.***.110',master_port=3306,master_user='zhku',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=156;
Here set master_host as the IP address of the host, master_user as the login user, such as zhku, and set master_password as the password, here my password is 123456, master_log_file is the file for viewing the master status on the host, and master_log_pos is the position viewed on the host.
open slave
start slave;
Query slave status
show slave status;
There are too many results, so I will display them in columns to look better.
You can see the two most critical parameters, the last two, Slave_IO_Running and Slave_SQL_Running, showing Yes means that the master-slave synchronization has been successfully achieved, and the first Slave_IO_State also shows that it is waiting for the host to send event information.
So far the configuration is successful!
problems encountered
The first problem: the startup fails when restarting the service in the third step
The reason is that there is a problem with the configuration file, and it is necessary to check whether the configuration file is configured correctly, such as the spelling of words, whether the database exists, etc.
The second problem: when the slave machine checks the slave status in the fifth step, Slave_IO_Running shows Connecting
Reason: You can continue to view the following columns and find the Last_IO_Error column
Viewing the error report inside shows Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection..
Go to host query
It is found that the plugin value of the zhku account is different from that of the root account. You need to modify the plugin value of the zhku account to mysql_native_password (use the update statement, which will not be described here). After modification, two YES will be displayed successfully.
It is also possible that the error message here is not the problem. This column shows that there are three main connecting problems, one is a network problem, the other is an incorrect account password, and the other is an incorrect setting of master_log_file and master_log_pos. If there is a network problem, you can check whether the firewall is enabled. The author is using the Alibaba Cloud server here. The security group is enabled on the server, and the firewall port is enabled on Centos, so it can be accessed. If it is a virtual machine, it depends on the bridge mode. repeat.
Open and close on Linux, check the blog post link of the firewall:
https://blog.csdn.net/weixin_45930241/article/details/123219592
The third problem: when the slave machine checks the slave status in the fifth step, Slave_IO_Running shows No
Same as the second question, query the following information column to see what is the reason for the error
It was found that the display file name was inconsistent
show master status;
Use the show master status command on the slave machine to view
Use the show master status command on the host to view
The comparison found that File and Position are inconsistent.
Use the stop slave command to stop the slave of the slave machine, go back to step 5, use the change master command to set again, and finally start the slave again. If it still fails, check whether the configuration files of the two machines have been modified correctly.
stop slave;