Centos 7 系统Mysql 数据库主从同步 之 数据库主从配置

一、主数据库基本配置

1、打开主数据的配置文件my.cnf (/etc/my.cnf)

在【mysqld]配置区域添加如下内容】

[mysqld]

# [数据库唯一ID,主从的标识号绝对不能重复]
server-id = 1
# [开启bin-log,并指定文件目录和文件名前缀]
log-bin=mysql-bin
# [需要同步liting数据库。如果是多个同步库,就以此格式另写几行即可。如果不指明对某个具体库同步,就去掉此行,表示同步所有库(除了ignore忽略的库)]
#binlog-do-db=liting
# [不同步mysql系统数据库。如果是多个不同步库,就以此格式另写几行;也可以在一行,中间逗号隔开]
#binlog-ignore-db=mysql
# [确保binlog日志写入后与硬盘同步]
sync_binlog = 1
# [跳过现有的采用checksum的事件,mysql5.6.5以后的版本中binlog_checksum=crc32,而低版本都是binlog_checksum=none]
binlog_checksum = crc32
# [bin-log日志文件格式,设置为MIXED可以防止主键重复]
binlog_format = mixed

2、重启mysql

service mysqld restart

3、在master主库上设置数据同步权限

[root@localhost mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2852821
Server version: 5.6.22-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> grant replication slave,replication client on *.* to ‘replicate’@'%' identified by "123456";
# 允许所有网段的使用 replicate用户,且密码为 123456 连接主库做数据同步
# 若要所有网段则设置repl@'%' ;部分网段:repl@'192.168.0.%'
mysql> flush privileges;

4、权限查看


mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.01 sec)

mysql> show grants for 'replicate'@'%';
+------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for replicate@%                                                                                                                   |
+------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replicate'@'%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 

二、从库slave配置信息

1、设置slave数据库的my.cnf配置文件(/etc/my.cnf)

[mysqld]
# [设置从服务器id,必须于主服务器不同]
server-id = 5
# [启动MySQ二进制日志系统]
log-bin=mysql-bin
# [需要同步的数据库名。如果不指明同步哪些库,就去掉这行,表示所有库的同步(除了ignore忽略的库)]
#replicate-do-db = mysql
# [不同步mysql数据库]
#replicate-ignore-db=mysql
# [跳过所有的错误,继续执行复制操作]
slave-skip-errors = all
# 日志记录的格式
binlog_format=mixed

2、重启mysql

service mysqld restart

3、配置主从同步指令

[root@localhost data]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.22-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='172.16.97.137',master_user='replicate',master_password='123456',master_log_file='mysql-bin.000736',master_log_pos=662217819;
Query OK, 0 rows affected, 2 warnings (1.37 sec)

mysql> start slave;
Query OK, 0 rows affected (0.10 sec)

mysql> 

master_log_file、master_log_pos 通过全备目录内的Xtrabackup_binlog_info 文件,可以找到主从位置点(在主从同步时使用)

[root@localhost 2019-11-08_09-14-39]# cat xtrabackup_binlog_info 
mysql-bin.000736        662217819
[root@localhost 2019-11-08_09-14-39]# 

4、从库执行:show slave status \G;查看是否正常与状态

mysql> show processlist;show slave status \G;
+----+-------------+-----------+------------+---------+--------+----------------------------------------+-------------------+
| Id | User        | Host      | db         | Command | Time   | State                                  | Info              |
+----+-------------+-----------+------------+---------+--------+----------------------------------------+-------------------+
|  3 | system user |           | NULL       | Connect |    223 | Queueing master event to the relay log | NULL              |
|  4 | system user |           | NULL       | Connect | 447937 | Reading event from the relay log       | NULL              |
|  5 | root        | localhost | NULL       | Query   |      0 | init                                   | show processlist  |
|  6 | root        |           | ss_product | Sleep   |     13 |                                        | NULL              |
|  7 | root        |           | ss_product | Query   |     16 | Opening tables                         | SHOW TABLE STATUS |
+----+-------------+-----------+------------+---------+--------+----------------------------------------+-------------------+
5 rows in set (0.00 sec)

*************************** 1. row ***************************
               Slave_IO_State: Queueing master event to the relay log
                  Master_Host: 172.16.97.137
                  Master_User: replicate
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000737
          Read_Master_Log_Pos: 761785729
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 59634140
        Relay_Master_Log_File: mysql-bin.000736
             Slave_IO_Running: Yes
            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: 721851676
              Relay_Log_Space: 1176501312
              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: 447975
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: 9115a0a4-b34d-11e4-9965-44a842072a73
             Master_Info_File: /opt/sumscope/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Reading event from the relay log
           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.37 sec)

ERROR: 
No query specified

mysql> 

如上,当IO和SQL线程的状态均为Yes,则表示主从已实现同步了!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值