Mysql 主从复制

参考自 https://www.jianshu.com/p/faf0127f1cb2

 

MySQL集群

 yum install -y mariadb-server
 systemctl enable mariadb&&systemctl restart mariadb

主端

vim /etc/my.cnf

 [mysqld]
 datadir=/var/lib/mysql
 socket=/var/lib/mysql/mysql.sock
 # Disabling symbolic-links is recommended to prevent assorted security risks
 symbolic-links=0
 # Settings user and group are ignored when systemd is used.
 # If you need to run mysqld under a different user or group,
 # customize your systemd unit file for mariadb according to the
 # instructions in http://fedoraproject.org/wiki/Systemd
 
 #-----------------------------加入如下行------------------------------
 server_id=38
 log-bin=mysql-bin
 #-------------------------------------------------------
 
 [mysqld_safe]
 log-error=/var/log/mariadb/mariadb.log
 pid-file=/var/run/mariadb/mariadb.pid
 
 #
 # include all files from the config directory
 #
 !includedir /etc/my.cnf.d

重启

systemctl restart mariadb

验证是否已经配置成功

能够查询对应配置文件中的server_id 说明已经配置成功,如下图,则表示配置成功。server_id=38

 
 MariaDB [mysql]> show variables like '%server_id%';
 +---------------+-------+
 | Variable_name | Value |
 +---------------+-------+
 | server_id     | 38   |
 +---------------+-------+
 1 row in set (0.00 sec)
 
 show master status;
 # 能够看到同步的文件,和行数说明已经配置成功。
 MariaDB [mysql]> show master status;
 +------------------+----------+--------------+------------------+
 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
 +------------------+----------+--------------+------------------+
 | mysql-bin.000004 |      729 |             |                 |
 +------------------+----------+--------------+------------------+
 1 row in set (0.00 sec)
 

锁定主端数据库

 flush tables with read unlock;
 # 先锁住主端数据库,禁止读写,防止出现gap
 # 待备端配好后 UNLOCK TABLES; 解锁主端数据库

 

 

从端

vi /etc/my.cnf

 [mysqld]
 datadir=/var/lib/mysql
 socket=/var/lib/mysql/mysql.sock
 # Disabling symbolic-links is recommended to prevent assorted security risks
 symbolic-links=0
 # Settings user and group are ignored when systemd is used.
 # If you need to run mysqld under a different user or group,
 # customize your systemd unit file for mariadb according to the
 # instructions in http://fedoraproject.org/wiki/Systemd
 
 server_id=39
 # 配置服务器id server_id
 # 这里的server_id 尽量配置为服务器地址的后3位 以用来区分
 log-bin=mysql-bin
 # 开启日志文件(binLog)
 binlog_do_db=test
 # 添加需用同步的数据库,不添加这一行则同步所有的数据库
 
 [mysqld_safe]
 log-error=/var/log/mariadb/mariadb.log
 pid-file=/var/run/mariadb/mariadb.pid
 
 #
 # include all files from the config directory
 #
 !includedir /etc/my.cnf.d

 

从服务器同步主服务器配置

master_host 主服务器地址 master_user 主服务器用户名 master_password 主服务器密码 master_log_file 主服务器配置文件 master_log_pos 主服务器读取配置文件的开始位置,也就是从第多少行开始读取。

 

 MariaDB [mysql]> show master status;
 +------------------+----------+--------------+------------------+
 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
 +------------------+----------+--------------+------------------+
 | mysql-bin.000004 |      729 |             |                 |
 +------------------+----------+--------------+------------------+
 1 row in set (0.00 sec)
 
 # File字段代表日志文件
 # Position字段代表开始读取的行数

 

 
 # 从服务器同步主服务器配置
 change master to master_host='172.16.102.38',master_user='root',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=729;
 MariaDB [mysql]> change master to master_host='172.16.102.38',master_user='root',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=729;
 Query OK, 0 rows affected (0.01 sec)
 
 change master to master_host='172.16.102.38',master_user='root',master_password='123456',master_log_file='mysql-bin.000005',master_log_pos=245;
 
 # 开始同步
 start slave;
 # 关闭同步
 stop slave;
 
 

 

解锁主端数据库

 UNLOCK TABLES;
 # 解锁主端数据库

 


在从站上暂停复制

您可以使用STOP SLAVESTART SLAVE语句停止并启动从站上的复制 。

要停止从主服务器处理二进制日志,请使用 STOP SLAVE

 

 mysql> STOP SLAVE;

当复制停止时,从I / O线程停止从主二进制日志读取事件并将它们写入中继日志,并且SQL线程停止从中继日志读取事件并执行它们。您可以通过指定线程类型单独暂停I / O或SQL线程:

 

 mysql> STOP SLAVE IO_THREAD;
 mysql> STOP SLAVE SQL_THREAD;

要再次开始执行,请使用以下START SLAVE语句:

 

 mysql> START SLAVE;

要启动特定线程,请指定线程类型:

 

 mysql> START SLAVE IO_THREAD;
 mysql> START SLAVE SQL_THREAD;

排错

 

 # 检查从服务器复制功能状态,Slave_IO_Running,Slave_SQL_Running必须为YES才行。
 SHOW SLAVE STATUS\G;
 MariaDB [mysql]> SHOW SLAVE STATUS\G;
 *************************** 1. row ***************************
                Slave_IO_State: Connecting to master
                  Master_Host: 172.16.102.38
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 729
                Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000004
              Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
            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: 729
              Relay_Log_Space: 245
              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: NULL
 Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1045
                Last_IO_Error: error connecting to master 'root@172.16.102.38:3306' - retry-time: 60 retries: 86400 message: Access denied for user 'root'@'HDSS7-21' (using password: YES)
                Last_SQL_Errno: 0
                Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
              Master_Server_Id: 0
 1 row in set (0.00 sec)
 
 ERROR: No query specified
 
 #Slave_IO_Running显示Connecting原因:
 #防火墙没关
 #密码不对
 #主数据库无法连接
 #总之就是连不上主库,备库一直在尝试Connecting

 

如果出现Slave_SQL_Running为No错误

 MariaDB [mysql]> show slave status\G;
 *************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.102.38
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 2854
                Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 800
        Relay_Master_Log_File: mysql-bin.000005
              Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
            Replicate_Do_Table:
        Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                    Last_Errno: 1051
                    Last_Error: Error 'Unknown table 't'' on query. Default database: 'test'. Query: 'DROP TABLE `t` /* generated by server */'
                  Skip_Counter: 0
          Exec_Master_Log_Pos: 516
              Relay_Log_Space: 4594
              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: NULL
 Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
                Last_SQL_Errno: 1051
                Last_SQL_Error: Error 'Unknown table 't'' on query. Default database: 'test'. Query: 'DROP TABLE `t` /* generated by server */'
  Replicate_Ignore_Server_Ids:
              Master_Server_Id: 38
 1 row in set (0.00 sec)
 
 ERROR: No query specified

多次执行以下语句就好

 stop slave;
 set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
 start slave;
 

 

 

 

 

 

 

 

posted @ 2020-11-09 21:52  士官长  阅读(110)  评论(0编辑  收藏  举报