阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL 5.6主从同步配置案例

113次阅读
没有评论

共计 9209 个字符,预计需要花费 24 分钟才能阅读完成。

MySQL 5.6 主从同步配置案例分享,希望对大家有所帮助。

本文环境  
主库:CentOS6.5 x64 192.168.0.65 mysql-5.6.29    
备库:CentOS6.5 x64 192.168.0.66 mysql-5.6.29

一、常规配置方式一

1. mysql 主服务器配置

# vi /etc/my.cnf

[mysqld]  
log-bin                = master-bin    
log-bin-index          = master-bin.index    
binlog_format          = mixed    
server-id              = 1

# service mysqld restart

mysql> show master status;  
+——————-+———-+————–+——————+——————-+    
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |    
+——————-+———-+————–+——————+——————-+    
| master-bin.000001 |      353 |              |                  |                  |    
+——————-+———-+————–+——————+——————-+    
1 row in set (0.00 sec)    
mysql>

2. 主服务器配置同步复制帐号

grant replication slave on *.* to ‘repl’@’%’ identified by ‘123456’;  
flush privileges;

3. mysql 从服务器配置

说明: 默认只要 server-id 不相同即可。

# vi /etc/my.cnf

[mysqld]  
log-bin                = mysql-bin    
binlog_format          = mixed    
server-id              = 11    
relay-log              = slave-relay-bin    
relay-log-index        = slave-relay-bin.index

配置说明: 不同步 mysql 库可以实现主从库有不同的帐号权限,经过测试,mysql5.6.29 中只在从库中配置有效。

其它参数:
binlog-do-db                = mydb    仅同步一个数据库    
#replicate-ignore-db        = mysql  忽略掉 mysql 库, 该参数产生很多意外的同步问题,还是不使用。
replicate_wild_ignore_table = mysql.% 忽略掉 mysql 库

# service mysqld restart

4. 测试示例

CREATE DATABASE `mydb`;

CREATE TABLE `user` (
  `id` varchar(20) NOT NULL,    
  `username` varchar(20) NOT NULL,    
  `password` char(32) NOT NULL,    
  PRIMARY KEY (`id`)    
) ;

INSERT INTO user  VALUES (‘1’, ‘koumm’, ‘123456’);  
INSERT INTO user  VALUES (‘2’, ‘zhangsan’, ‘123456’);    
INSERT INTO user  VALUES (‘3’, ‘lisi’, ‘123456’);    
INSERT INTO user  VALUES (‘4’, ‘li2si’, ‘123456’);    
INSERT INTO user  VALUES (‘5’, ‘abc’, ‘123456’);    
INSERT INTO user  VALUES (‘6’, ‘tom’, ‘123456’);    
INSERT INTO user  VALUES (‘7’, ‘jk’, ‘123456’);    
INSERT INTO user  VALUES (‘8’, ‘xb’, ‘123456’);

5. 正常的主从配置过程

(1) 主库锁表

mysql> flush tables with read lock;  
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;  
+——————-+———-+————–+——————+——————-+    
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |    
+——————-+———-+————–+——————+——————-+    
| master-bin.000001 |      353 |              |                  |                  |    
+——————-+———-+————–+——————+——————-+    
1 row in set (0.00 sec)

mysql> show master logs;    
+——————-+———–+    
| Log_name          | File_size |    
+——————-+———–+    
| master-bin.000001 |      353 |    
+——————-+———–+    
1 row in set (0.00 sec)

mysql>

(2) 主库备份

[root@master ~]# mysqldump -uroot -p -B mydb > mydb.sql  
说明:- B 参数有建库语句。

(3) 主库解开锁表功能

mysql> unlock tables;  
Query OK, 0 rows affected (0.00 sec)    
mysql>

6. 从库导入数据库

# mysql -uroot -padmin < mydb.sql

7. 从库配置同步过程

(1) 配置同步,手动执行同步参数,该配置会写入 master.info 文件中。

mysql >

CHANGE MASTER TO  
MASTER_HOST=’192.168.0.65′,    
MASTER_PORT=3306,    
MASTER_USER=’repl’,    
MASTER_PASSWORD=’123456′,    
MASTER_LOG_FILE=’master-bin.000001′,    
MASTER_LOG_POS=353;

(2) 启动从同步进程

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

mysql> show slave status\G;  
*************************** 1. row ***************************    
              Slave_IO_State: Waiting for master to send event    
                  Master_Host: 192.168.0.65    
                  Master_User: repl    
                  Master_Port: 3306    
                Connect_Retry: 60    
              Master_Log_File: master-bin.000001    
          Read_Master_Log_Pos: 353    
              Relay_Log_File: slave-relay-bin.000002    
                Relay_Log_Pos: 284    
        Relay_Master_Log_File: master-bin.000001    
            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: 353    
              Relay_Log_Space: 457    
              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: c8bb22a1-024e-11e6-a1e8-000c29225fa0    
            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

mysql>

# 查看如下两个参数为 YES,说明从库运行正常。
            Slave_IO_Running: Yes    
            Slave_SQL_Running: Yes

(3) 测试同步情况

# 主库插入一条记录

mysql> use mydb;  
Database changed    
mysql> select * from stu;    
+—-+———-+———-+    
| id | username | password |    
+—-+———-+———-+    
|  1 | koumm    | 123456  |    
|  2 | zhangsan | 123456  |    
|  3 | lisi    | 123456  |    
|  4 | wangwu  | 123456  |    
|  5 | zhaoliu  | 123456  |    
|  6 | zhouqi  | 123456  |    
+—-+———-+———-+    
6 rows in set (0.00 sec)

mysql>    
mysql> INSERT INTO stu  VALUES (‘7’, ‘tom’, ‘123456’);     
Query OK, 1 row affected (0.05 sec)

mysql> commit;  
Query OK, 0 rows affected (0.00 sec)

mysql>

# 从库查询同步情况

mysql> use mydb;  
Database changed    
mysql> select * from stu;    
+—-+———-+———-+    
| id | username | password |    
+—-+———-+———-+    
|  1 | koumm    | 123456  |    
|  2 | zhangsan | 123456  |    
|  3 | lisi    | 123456  |    
|  4 | wangwu  | 123456  |    
|  5 | zhaoliu  | 123456  |    
|  6 | zhouqi  | 123456  |    
|  7 | tom      | 123456  |    
+—-+———-+———-+    
7 rows in set (0.00 sec)

mysql>

小结:

本文只是配置 mysql 主从的一个过程。还是有很多需要考虑与改进的地址。
(1) 主从库同步用户权限的问题考虑,是直接从主库同步权限呢,还是单独考虑从库的权限。
(2) mysql5.5 有半自动主从同步复制    
(3) mysql5.6 有基于 GTID 主从复制

二、 快速配置从库方式二

1,主库备份 (全库备份)

mysqldump -uroot -p -A -B –events –master-data=1 > mydb.sql

全库备份记录了数据库备份时的 master-bin.000003′, MASTER_LOG_POS=583 位置,在配置从库时无需要在主库锁表,记录 POS 位置,解锁等配置。

[root@localhost ~]# egrep -v “#|\*|–|^$” mydb.sql |head

CHANGE MASTER TO MASTER_LOG_FILE=’master-bin.000003′, MASTER_LOG_POS=583;
USE `mydb`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` varchar(20) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` char(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
LOCK TABLES `user` WRITE;

2,从库创建同步的数据库, 并导入数据

mysql -uroot -padmin
create database mydb;
mysql -uroot -padmin mydb < mydb.sql

3,配置主从同步

mysql>
CHANGE MASTER TO
MASTER_HOST=’192.168.0.65′,
MASTER_PORT=3306,
MASTER_USER=’repl’,
MASTER_PASSWORD=’123456′;

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

mysql> show slave status \G;
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.65
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 583
              Relay_Log_File: testdb-relay-bin.000004
                Relay_Log_Pos: 747
        Relay_Master_Log_File: master-bin.000003
            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: mysql.%
                  Last_Errno: 0
                  Last_Error:
                Skip_Counter: 0
          Exec_Master_Log_Pos: 583
              Relay_Log_Space: 2536
              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: c8bb22a1-024e-11e6-a1e8-000c29225fa0
            Master_Info_File: /usr/local/mysql-5.6.29-linux-glibc2.5-x86_64/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)

三、主从同步简单管理

1. 停止 MYSQL 同步

STOP SLAVE IO_THREAD;    #停止 IO 进程
STOP SLAVE SQL_THREAD;    #停止 SQL 进程
STOP SLAVE;              #停止 IO 和 SQL 进程

2. 启动 MYSQL 同步

START SLAVE IO_THREAD;    #启动 IO 进程
START SLAVE SQL_THREAD;  #启动 SQL 进程
START SLAVE;              #启动 IO 和 SQL 进程

3. 重置 MYSQL 同步

RESET SLAVE;
清除主从同步参数,它会删除 master.info 和 relay-log.info 文件,以及所有的中继日志,并启动一个新的中继日志。
适用重新再次配置一次从库的情况。

4. 查看 MYSQL 同步状态

SHOW SLAVE STATUS;

5. 临时跳过 MYSQL 同步错误

确保数据一致的情况下临时的跳过这个错误操作如下,有可能要多次。

STOP SLAVE;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
START SLAVE;

# vi /etc/my.cnf

[mysqld]
#slave-skip-errors=1062,1053,1146 #跳过指定 error no 类型的错误
#slave-skip-errors=all            #跳过所有错误

这里出现一次 1146 报错,是因为配置 binlog-do-db 过滤,出现该配置的 BUG 问题,所有要慎用 binlog-do-db。

实现两个 MySQL 数据库之间的主从同步 http://www.linuxidc.com/Linux/2016-02/128100.htm

Linux 环境中 MySQL 主从同步 – 添加新的从库 http://www.linuxidc.com/Linux/2015-08/122448.htm

通过 XtraBackup 实现不停机不锁表搭建 MySQL 主从同步 http://www.linuxidc.com/Linux/2015-08/121806.htm

MySQL 主从同步配置记录 http://www.linuxidc.com/Linux/2015-07/119939.htm

Linux 下 MySQL 数据库主从同步配置 http://www.linuxidc.com/Linux/2016-03/129138.htm

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-04/130729.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计9209字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中