Detailed explanation of MySQL master-slave replication configuration

1. Configure the environment

Operating system: Two Linux systems with CentOS 7.6

Database version: MySQL 5.6.39

Main server IP: 192.168.0.1

Slave server IP: 192.168.0.2

2. Install the database

I have explained the steps of installing MySQL on CentOS in detail to my friends before. For those who have not seen it, you can click here:

" Detailed steps to install MySQL in Linux environment "

3. Configuration prerequisites

1. It is necessary to ensure that the firewall is opened or closed on port 3306, which is introduced in the MySQL installation.

2. The two servers can ping each other

--在192.168.0.2上输入ping命令
ping 192.168.0.1
--在192.168.0.1上输入ping命令
ping 192.168.0.2

3. After successfully installing a MySQL, use a virtual machine to clone one as a slave server

4. Configure the master database

4.1. Modify the database configuration file

[root@localhost ~]# vi /etc/my.cnf

Change the content inside to

[mysqld]
#开启二进制日志
log-bin=mysql-bin
#标识唯一id(必须),一般使用ip最后位
server-id=1
#不同步的数据库,可设置多个
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#指定需要同步的数据库(和slave是相互匹配的),可以设置多个
binlog-do-db=test

Add log storage method and rules (optional)

#设置存储模式不设置默认
binlog_format=MIXED
#日志清理时间
expire_logs_days=7
#日志大小
max_binlog_size=100m
#缓存大小
binlog_cache_size=4m
#最大缓存大小
max_binlog_cache_size=521m

Note: I set the storage capacity of the log to be relatively small, of course, you can modify it to be larger according to the actual situation.

4.2, restart the database service mysqld

service mysqld restart

If you installed mysql correctly according to the above, you can restart normally here. If the startup is not normal, the following error will appear:

The server quit without updating PID file......

You need to use the following command to see if there is still a mysqld process

ps -ef|grep mysqld

If there is, you can use the command: kill -9 mysqld's process number to end it, then restart mysqld

I have encountered the above situation. Of course there are other reasons, here is a solution for other possible reasons for reference:

https://javawind.net/p141

4.3. Log in to the MySQL database to allow the main library log from the library

[root@localhost ~]# mysql -u root -p

Note: You do not need to enter the root password for the first login.

After entering, do the following configuration:

#给从库放权限
mysql>GRANT FILE ON *.* TO 'root'@'192.168.0.2' IDENTIFIED BY 'root password'; #创建用户
mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.0.2' IDENTIFIED BY 'root password'; #修改用户权限
mysql>select host ,user ,password from mysql.user; #查看是否修改成功
mysql>FLUSH PRIVILEGES; #刷新权限

4.4. Restart the MySQL service, log in to MySQL, and view the main database information

[root@localhost ~]# service mysqld restart #重启mysql服务
[root@localhost ~]# mysql -u root -p #登陆mysql
mysql> show master status; #查看master状态

Shows roughly the following

+------------------+----------+--------------+----------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
| mysql-bin.000006 |    120 | ufind_db | information_schema,performance_schema,mysql | |
+------------------+----------+--------------+----------------------------------+-------------------+
1 row in set (0.00 sec)

Note : If this step is always executed Empty set(0.00 sec), it means that the previous my.cnf is not configured correctly , please go back and recheck the configuration steps.

5. Configure the slave database

5.1. Modify the database configuration file of the slave library

[root@localhost ~]# vi /etc/my.cnf

Change the content inside to

#开启二进制日志
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#与主库配置保持一致
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60

5.2. Restart the MySQL service and log in to MySQL

[root@localhost ~]# service mysqld restart

[root@localhost ~]# mysql -u root -p

and make the following modifications:

#关闭Slave
mysql> stop slave; #设置连接主库信息
mysql> change master to master_host='192.168.0.1',master_user='root',master_password='root password',master_log_file='mysql-bin.000006', master_log_pos=120;
#开启Slave
mysql> start slave;

Note : The above master_log_file is the File field when configuring the Master, and master_log_pos is the Position field when configuring the Master. must match
 

5.3. View the status information of the slave library

mysql> show slave status \G;

If successful, the following information will be displayed:

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.1
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 120
               Relay_Log_File: localhost-relay-bin.000006
                Relay_Log_Pos: 520
        Relay_Master_Log_File: mysql-bin.000006
             Slave_IO_Running: Yes //显示yes为成功
            Slave_SQL_Running: Yes //显示yes为成功,如果为no,一般为没有启动master
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql//上面的都是配置文件中的信息
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 357
              Relay_Log_Space: 697
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: //如果为no,此处会显示错误信息
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 2
                  Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6a
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)
 
ERROR:
No query specified

Note : If Slave_IO_Running: No and the following error occurs

Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

It means that the UUID of the master server and the UUID of the slave server are repeated, because I cloned a database directly after successfully installing it, so their UUIDs are the same, and this error will be reported. You can modify the UUID of the slave library.

We first generate a UUID in the database from the library

mysql>select UUID();

Copy the UUID queried from the database, and then edit the UUID configuration file of the slave library

If you also installed the same as me, the path to the configuration file should be here:

[root@localhost ~]# vi /usr/local/mysql/data/auto.cnf

After entering, replace a string of 32-bit UUIDs with the UUIDs we just queried in the database.

If Slave_IO_Running: No and the following error occurs

Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

Solution: Reset

mysql>stop slave; //停止
mysql>reset slave; //复位
mysql>start slave; //开启

At this point the whole process is configured.

Some friends may ask, I have configured these configuration files, and the information is the same as yours. I am still not sure whether the configuration is successful.

Then you can create a table on the master server, and then query the newly created table on the slave server to see if it exists.

6、Tips

6.1. Regarding the inconsistency of master-slave data with regard to additions, deletions, revisions, and inquiries:

#select 语句,暂时没有发现问题
 
#insert 语句,暂时没有发现问题
 
#update 语句,暂时没有发现问题
 
#delete 语句,主库删除多条数据,发现数据不一致

Reason: There is indeed a delete statement in the logbin of the main library, but there is no delete statement in the logbin of the slave library

Solution: Use use database to select the database that needs to be operated in the current database schema, and then execute the delete, OK synchronization is successful

6.2, the method of querying the binlog master-slave log

#查看binlog全部文件
mysql>show binary logs;
 
#查看binlog是否开启NO为开启
mysql> show variables like 'log_bin%';
 
#详细信息
mysql>  show variables like 'binlog%';
 
#查看binlog日志
mysql> show binlog events in'mysql-bin.000019';
 
#或者使用mysqlbinlog,如果报错使用--no-defaults(使用全路径)
[root@localhost ~]# /usr/local/mysql/bin/mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.000019

6.3. Manually clean up the master log, it is best to close the log, in /etc/my.cnf

#手动刷新日志
mysql> show master status;
#删除全部
mysql> reset slave;或 rest master;
#删除MySQL-bin.004
mysql> PURGE MASTER LOGS TO 'MySQL-bin.004';

おすすめ

転載: blog.csdn.net/ytp552200ytp/article/details/125987646