导航

mysql主从复制配置

Posted on 2017-06-29 23:40  张鑫的园子  阅读(213)  评论(0编辑  收藏  举报

同步原理:

  1. 同步时从库由两个线程完成(IO,SQL),主库由一个线程完成(IO);
  2. 要把主库的数据先还原到从库,主库备份的点就是从库change master的点,或者使用参数--master-data=1
  3. 要在从库上配置连接主库的IP,用户名,账号,密码,文件名,位置,及pos点
  4. 在开启开关之前,在某一个时间点,主库和从库数据一致
  5. 要在主库建立专门用于从库同步的账号
  6. 主库要打开bin-log开关,否则没法同步
  7. 从库打开开关的过程(两个线程工作的过程)

配置过程:

  1. 准备两台数据环境,或者单台多实例环境,能否正常启动和登陆。
  2. 配置my.cnf文件,主库配置log-bin和server-id参数,从库配置server-id参数,不能和主库及其他从库一样,一般不开启从库bin-log,注意,配置参数后要重启生效。
  3. 登陆主库增加用于从库连接主库同步的账户,例如rep,并授权replication slave同步权限。
  4. 登陆主库,整库锁表,flush table with read lock(窗口关闭后即失效,超时参数到了也失效);然后show master status 查看binlog的位置状态
  5. 新开窗口,linux命令行备份或导出原有的数据库数据,并拷贝到从库所在的服务器目录。
  6. 如果数据量很大,并且允许停机,可以停机打包,而不用mysqldump。
  7. 解锁主库,unlock tables;
  8. 把主库导出的原有数据恢复到从库
  9. 根据主库的show master status查看binlog的位置状态,再从库执行change master to...语句
  10. 从库开启同步开关,start slave。

 

 


配置:
master 3307
slave 3308

==================================================主库============================================
1. 开启bin-log,保证主备库的server-id不相同
2. bin-log要放在[mysqld]模块下面
3. 检查主库的server_id

[root@MySQL ~]# mysql -uroot -proot -S /data/3307/mysql.sock -e "show variables like 'server_id'";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+

 

4. 检查主库是否开启log_bin,如果没有开启,修改my.cnf文件进行添加:

[root@MySQL ~]# mysql -uroot -proot -S /data/3307/mysql.sock -e "show variables like 'log_bin%'"; 
+---------------------------------+---------------------------------+
| Variable_name                   |          Value                  |
+---------------------------------+---------------------------------+
| log_bin                         | ON                              |
| log_bin_basename                | /data/3307/data/mysql-bin       |
| log_bin_index                   | /data/3307/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF                             |
| log_bin_use_v1_row_events       | OFF                             |
+---------------------------------+---------------------------------+

 

5. 连接到主库,创建用户,可以从备库登陆过来进行日志读取:
[root@MySQL ~]# mysql -uroot -proot -S /data/3307/mysql.sock

mysql> grant replication slave on *.* to 'rep'@'192.168.56.99' identified by 'abbott';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

 

6. 锁定主库的表,然后进行备份,否则备份集将不成功

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

 

7. 查看此时的position,确保备份完成之后不会改变

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

 

8. 查看log

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 | 727       |
| mysql-bin.000002 | 833       |
| mysql-bin.000003 | 600       |
+------------------+-----------+
3 rows in set (0.00 sec)

 

9. 对主库进行备份

[root@MySQL log]# mysqldump -uroot -proot -S /data/3307/mysql.sock -A -B --events --master-data=2 > /opt/rep.sql
[root@MySQL opt]# mysqldump -uroot -proot -S /data/3307/mysql.sock -A -B --master-data=1 --single-transaction >/opt/full.sql
企业推荐:不停库(使用这种方式,不需要指定binlog位置,也不需要锁表,--single-transaction参数可以屏蔽其余会话的增删改,即不需要加master_log_file和master_log_pos这两个参数)

 

10. 检查备份后position是否改变

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

 

11. 如果检查没有问题,那么就取消锁定

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

 

+++++++++++++++++++++++++++++++++++++++++++++++++++备库++++++++++++++++++++++++++++++++++++++++++++++++++++

12. 在备库恢复备份的文件

[root@MySQL opt]# mysql -uroot -proot -S /data/3308/mysql.sock </opt/rep.sql

 

13. 查看备份是否恢复成功

[root@MySQL opt]# mysql -uroot -proot -S /data/3308/mysql.sock
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ZX                 |
| abbott             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

 

14. 指定备库同步主库的信息

change master to
MASTER_HOST='192.168.56.99',
MASTER_PORT=3307,
MASTER_USER='rep',
MASTER_PASSWORD='abbott',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=600;
Query OK, 0 rows affected, 2 warnings (0.13 sec)

 

15. 查看master_info里记录的信息是否和指定同步的位置一样

[root@MySQL data]# cat master.info 
24
mysql-bin.000003
600
192.168.56.99
rep
abbott
3307

 

16. 备库开始进行同步

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

 

17. 查看同步情况,其中参数Slave_IO_Running和Slave_SQL_Running都是yes。

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.99
Master_User: rep
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 600
Relay_Log_File: MySQL-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-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: 
Last_Errno: 0
Last_Error: 
Skip_Counter: 0
Exec_Master_Log_Pos: 600
Relay_Log_Space: 527
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: 3b9f6e49-5a6b-11e7-b603-08002714955b
Master_Info_File: /data/3308/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB: 
Channel_Name: 
1 row in set (0.00 sec)

 

18. 测试,在主库创建一个库abc,可以看到在备库已经同步过来。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ZX                 |
| abbott             |
| abc                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

 


查看线程状态:
主库:

mysql> show processlist;
+----+------+---------------------+--------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host                | db     | Command     | Time | State                                                         | Info             |
+----+------+---------------------+--------+-------------+------+---------------------------------------------------------------+------------------+
| 9  | rep  | 192.168.56.99:38359 | NULL   | Binlog Dump | 215  | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 10 | root | localhost           | abbott | Query       | 0    | starting                                                      | show processlist |
+----+------+---------------------+--------+-------------+------+---------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)

 

备库:

mysql> show processlist;
+----+-------------+-----------+--------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db     | Command | Time | State                                                  | Info             |
+----+-------------+-----------+--------+---------+------+--------------------------------------------------------+------------------+
| 15 | root        | localhost | abbott | Query   | 0    | starting                                               | show processlist |
| 16 | system user |           | NULL   | Connect | 237  | Waiting for master to send event                       | NULL             |
| 17 | system user |           | NULL   | Connect | 144  | Slave has read all relay log; waiting for more updates | NULL             | 
+----+-------------+-----------+--------+---------+------+--------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

 

要点:

  1. 异步方式同步;
  2. 逻辑同步模式,多种模式,默认是通过SQL语句执行;
  3. 主库通过记录binlog实现对从库的同步,binlog记录数据库的更新语句;
  4. 主库1个IO线程,从库由1个IO线程和一个SQL线程来完成;
  5. 从库关键文件master.info,relay-log,relay-info功能;
  6. 如果从库还想级联从库,需要打开log-bin参数和log-slave-updates参数。


生产场景快速配置mysql主从复制方案

  1.  安装好要配置从库的数据库,配置好log-bin和server-id参数
  2.  无需配置主库my.cnf文件,主库的log-bin和server-id参数默认就是配置好的
  3.  登录主库增加用于从库连接主库同步的账户 例如:rep 并授权replication slave同步的权限
  4.  使用mysqldump带--master-data=1 备份的全备数据恢复到从库
  5. mysqldump -uroot -proot -S /data3307/mysql.sock -A -B --master-data=1 --single-transaction > /opt/full.sql
  6.  在从库执行change master to ..语句 无需binlog文件及对应位置点
  7.  从库开启同步开关,start slave
  8.  从库show slave status\G 检查同步状态,并在主库进行更新测试

 

mysql其他主从复制实现:

  1. NFS网络文件共享
  2. samba共享数据 http://taokey.blog.51cto.com/4633273/1203553
  3. 定时任务或守护进程结合rsync scp
  4. inotify+rsync触发式实时数据同步
  5. ftp数据同步
  6. ssh key+scp/rsync

主服务器挂了,人工或自动切换到从服务器:http://oldboy.blog.51cto.com/2561410/1240412

可以保证同步的方式:

  1. 主库挂了,把主库的bin-log拿过来应用;
  2. 双写;
  3. 通过应用程序写一分钟的日志;
  4. 把异步同步换成实时同步(即半同步);

读写分离:

  1. 中大型公司,通过程序(php,java)
  2. 测试环境:代理软件(mysql-proxy,Amoeba)
  3. 门户网站:分布式dbproxy(读写分离,hash负载均衡,健康检查)