Centos7 builds mysql master-slave replication

Environment description

Operating system: CentOS7 master server master : 192.168.32.130 slave server slave : 192.168.32.129

master-slave replication

I have written an article about configuring the mysql database on centos7 before, so I will skip the mysql installation directly here .

1 ) The principle of master-slave replication



 

( The picture comes from the Internet )

Master-slave replication is divided into synchronous replication and asynchronous replication. Most of the actual replication architectures are asynchronous replication. The basic process of replication is as follows: 1. The IO process on the Slave connects to the Master and requests the log content after the specified location of the specified log file (or from the first log);    2. The Master receives the IO process from the Slave . After the request, the IO process responsible for replication reads the log information after the specified location of the log according to the request information, and returns it to the IO process of the slave . In addition to the information contained in the log, the returned information also includes the name of the bin-log file and the location of the bin-log where the information returned this time has been sent to the Master;    3. After the IO process of the Slave receives the information, it will receive The log content is added to the end of the relay-log file on the slave side in turn , and the read file name and location of the bin-log on the master side are recorded in master-infofile, so that the Master can be clearly told the next time it is read .    4. After the Slave Sql process detects the newly added content in the relay-log , it will immediately parse the content of the relay-log into the executable content when it is actually executed on the Master side, and execute it on its own.

 

  1. Modify the mysql master server

    On the master server ( 192.168.32.130 here ), switch to /etc/ to find the my.cnf file, and modify it. The modification content is mainly to add the following content at the top of the file:

    server-id=202 #Set the server's unique id, the default is 1, we set the last segment of the ip, and the slave is set to 203

    log-bin=mysql-bin # Enable binary logging

    binlog-ignore-db = mysql, information_schema #Ignore the library written to binlog



      

     

  2. Modify the mysql slave server

    On the slave server (I am 192.168.32.1 29 here ), switch to /etc/ to find the my.cnf file, and modify it. The modification content is mainly to add the following content at the top of the file:

    server-id=203 #This value only needs to be unique with the mysql server in the cluster , it can be customized

    replicate-do-db=jeff #Only synchronize the jeff library

    slave-skip-errors = all #Ignore all errors due to replication



      

  3. Restart the master and slaver servers

     systemctl restart mysqld , in fact, you can also close systemctl stop mysqld first and then start systemctl start mysqld

     

  4. Create an account on the master server and authorize the slave

    GRANT REPLICATION SLAVE ON *.* to 'mysqlsync'@'192.168.32.129' identified by 'chenfeng1234.Com';

     

  5. View master database status

    Log in to the master database, and use the command show master status; to view the status of the master database, mainly to see the values ​​corresponding to the master 's File and Position , so that it can be configured to the slave later.



      

     

  6. configuration from database

    Log in to the slave database and use the command

    change master to

    master_host='192.168.32.130',

    master_user='chenfeng',

    master_password='ChenFeng0814!',

    master_log_file='mysql-bin.000003',

    master_log_pos=1731;

    Here is an explanation of the meaning of each command:

    master_host represents the IP address of the master server ;

    master_user represents the remote login user name given by the master server;

    master_password represents the remote login password;

    master_log_file represents the File value in the master service status ;

    master_log_pos表示master服务状态中Position值。

     

  7. 启动slave同步进程并查看状态

    登录slave数据库,通过命令show slave status\G;当红圈内的两个状态都是yes就表明已经配置同步成功了,如下图所示:



      

     

  8. 验证主从同步

    此时所有主从同步配置已完成,现在我在master服务器上jeff数据库中插入一张表并导入部分数据来验证下slave服务器是否能够正常同步过来,可以看到此时数据库中并没有表的存在。



      

    master中执行添加表语句



      

    可以看到此时master上面表已经建成了



      

    再查看slave上面是否已经同步过去了,确实是已经同步过来了



      

     

  9. 常见错误



      

    如上图,查看slave状态是IO_RUNNING状态为no,这个可能是/var/lib/mysql/auto.cnf中的server-uuid主、从重复了,解决办法是将master上的auto.cnf文件删除,然后重启master服务器的mysql服务,将会生成一个新的auto.cnf文件,且server-uuid肯定和slave上面的值不等。



      

    如上图,查看slave状态是IO_RUNNING状态为connecting,这个错误可能由三个原因导致:1.远程用户、密码配置错误;2.read_master_log_posrelay_master_log_file配置错误;3.网络问题或者防火墙问题。

     

    参考博文地址:http://blog.jobbole.com/94595/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326462959&siteId=291194637