centos7下mysql实现主从同步

centos7下mysql(mysql5.1版以上)实现主从同步:

master:192.168.31.127(主)

backup:192.168.31.128(备)

复制myql配置文件

cp /usr/share/mysql/my-default.cnf /etc/my.cnf


修改192.168.31.127(主)的配置文件

vi /etc/my.cnf

在my.cnf文件中添加一下 

log-bin=mysql-bin

binlog_format=mixed

server-id = 1

binlog-do-db =test 要同步的数据库

binlog-ignore-db =mysql 不需要同步的数据库

replicate-do-db =test要同步的数据库

replicate-ignore-db= mysql不需要同步的数据库

sync-binlog = 1

修改192.168.31.128(备)的配置文件

vi /etc/my.cnf

在my.cnf文件中添加一下 :

log-bin=mysql-bin

binlog_format=mixed

server-id = 2

binlog-do-db =test 要同步的数据库

binlog-ignore-db =mysql 不需要同步的数据库

replicate-do-db =test要同步的数据库

replicate-ignore-db= mysql不需要同步的数据库

sync-binlog = 1

在主备数据中服务器停止同步

#192.168.31.127(主)服务器停止同步

stop salve;

#192.168.31.127(主)服务器清空MASTER日志

reset master;

# 192.168.31.127(主)服务器清空SLAVE日志

reset slave;

# 192.168.31.127(主) 服务器授权同步账户

GRANT REPLICATION SLAVE ON *.* TO 'master_slave'@'192.168.31%' IDENTIFIED BY 'Huawei123';

FLUSH PRIVILEGES;

#192.168.31.128(备)服务器停止同步

stop salve;

#192.168.31.128(备)服务器清空MASTER日志

reset master;

# 192.168.31.128(备)服务器清空SLAVE日志

reset slave;

# 查看 192.168.31.128(备) 服务器主机(记录二进制开始文件,位置

show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      120 | test         | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+

# 修改 192.168.31.127(主)服务器配置

CHANGE MASTER TO MASTER_HOST='192.168.31.128',MASTER_USER='master_slave',MASTER_PASSWORD='Huawei123',MASTER_LOG_FILE=' mysql-bin.000001',MASTER_LOG_POS=120;

# 开启 192.168.31.127(主)服务器同步进程

start slave;

# 查看 192.168.31.127(主) 服务器主机状态(记录二进制开始文件,位置)

show master status;

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      120 | test         | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+

# 修改 192.168.31.128(备) 服务器配置 (修改为主服务器的主机IP)

CHANGE MASTER TO MASTER_HOST='192.168.31.127', MASTER_USER='master_slave',

MASTER_PASSWORD='Huawei123', MASTER_LOG_FILE=' mysql-bin.000001',MASTER_LOG_POS=120;

# 开启 B 服务器同步进程

start slave;

# 分别查看 主备 服务器同步状态,确定是否成功

show slave status\G


mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.31.127
                  Master_User: master_slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 415
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

              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: 415
              Relay_Log_Space: 460
              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: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 3e20f317-dd5b-11e6-bcbc-000c29a770d7
             Master_Info_File: /var/lib/mysql/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


到此数据主从同步完成!!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值