Ubuntu virtual machine MySQL implements master-slave replication

problem reproduction

When I was working on the St. Regis takeaway project in Shang Silicon Valley, the video used the CentOS system, and I have been using ubuntu, and I have been configuring it for a long time, so I don’t want to change to CentOS, so there are a lot of mistakes—— First of all, I am not very familiar with Ubuntu, so I have been looking for the path of the MySQL configuration file for a long time, followed by the configuration sequence, etc.

Configuration process (applicable to MySQL8.0.30 and Ubuntu20.04LTS)

the host

configuration file

Ubuntu needs to be in

\etc\mysql\mysql.conf.d\mysqld.cnf

In the host configuration file under the path, add under [mysqld]

# 主机唯一标识
server-id=1
# 二进制日志文件路径
bin-log=mysql-bin

Remember to execute after adding

systemctl restart mysql

Restart MySQL for the configuration file to take effect

MySQL internal configuration

A communication user with authority is required between the master library and the slave library to communicate data—just like a speaker or a matchmaker

GRANT REPLICATION SLAVE ON *.* TO 'slaveName'@'%' IDENTIFIED BY 'password';

The slaveName and password here can be modified to your own, or not modified

Refresh:

FLUSH PRIVILEGES;

Then execute:

show master status;

After execution, the following interface will be displayed:

Record the corresponding File and Position in the above figure, because it will be used in a while, and the host configuration is completed - then try not to operate the host database, which may cause the value recorded above to change.

Slave

configuration file

also in

\etc\mysql\mysql.conf.d\mysqld.cnf

Add below [ mysqld]

# 从机的id,没有特别要求,但是要与主机不同
server-id=2

Notice:

If your slave is cloned, remember to change the static ip and uuid of the slave. The configuration of the static IP can be changed by itself, but it should not be the same as the master. The uuid can be used in the database

select uuid();

Generate uuid.

Remember to restart MySQL after changing the configuration file

MySQL internal operation

Can be executed first

stop slave;
reset slave;

Suspend the slave's original task - if any

The code to be executed next needs to fill in your own information. The template is as follows:

change master to master_host='主机ip' , master_user='主机创建的通信用户',master_password='创建通信用户时的密码',master_log_file='File',master_log_pos=Position;

In the above statement, except for the Chinese characters, the File and Position are the File and Position recorded after the host executes the "select master status;" statement—corresponding to the log name and offset

followed by execution

start slave;

Start the master-slave replication, and then you can execute

show slave status\G;

To output master-slave replication information to check whether it is successful.

When the following information is displayed, it means that the master-slave replication connection is successful, and the operation is performed on the master library, and the slave library will perform corresponding operations according to the log records

Accidental Situation and Analysis

Slave_IO_State is always Connecting

As shown in the picture, this problem troubled me for several hours at that time. As mentioned above, at first it was because I didn’t understand the files, etc., but as I got more understanding, I was sure that there was no problem with my configuration file, but it was still in this state. , you can start from the following points:

  1. Is the network communication between the master and slave systems normal? Use the ping command to ping each other, and once the host suddenly fails to ping

  1. Is the firewall turned off - key points

Many people may forget to turn off the firewall. If there is no important information in the master-slave system and it is just a virtual machine for learning, you can actually turn off the firewall permanently:

# 关闭防火墙
systemctl stop firewaldd
# 查看防火墙状态
systemctl status firewalld
# 禁用防火墙
systemctl status firewalls
  1. Whether the slave machine is a clone machine, and whether the uuid has been modified

  1. You can check the master status of the host

show master status;

It may be that the binary file and offset of the host have changed, causing the connection to fail

Slave_IO_State is always No

I rarely see this state, it is very likely that I forgot to turn off the firewall

restart MySQL

It may be that the configuration file is configured but there is no restart

Summarize

To sum up, sometimes the cause of a BUG may be a small detail, and it is not very comprehensive—for example, the configuration of the host can be set to bind a fixed database, etc., and you can learn more if you need it.

Hope it helps you! ! !

Guess you like

Origin blog.csdn.net/weixin_53811934/article/details/129245547