生产环境 MySQL主从复制(同步)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生产环境 MySQL主从复制(同步)相关的知识,希望对你有一定的参考价值。

服务器信息

1、主服务器信息

[email protected]:~$ lsb_release -a

No LSB modules are available.

Distributor ID:  Ubuntu

Description:     Ubuntu 14.04.4 LTS

Release:   14.04

Codename:  trusty

[email protected]:~$ mysql -V

mysql  Ver 14.14 Distrib 5.5.50, for debian-linux-gnu (x86_64) using readline 6.3

[email protected]:~$ ip ro li

default via 192.168.119.1 dev eth0

192.168.119.0/24 dev eth0  proto kernel  scope linksrc 192.168.119.95 

技术分享

2、从服务器信息

[email protected]:~$ lsb_release -a

No LSB modules are available.

Distributor ID:  Ubuntu

Description:     Ubuntu 14.04.4 LTS

Release:   14.04

Codename:  trusty

[email protected]:~$ mysql -V

mysql  Ver 14.14 Distrib 5.5.47, for debian-linux-gnu (x86_64) using readline 6.3

[email protected]:~$ ip ro ls

default via 192.168.119.1 dev eth0

192.168.119.0/24 dev eth0  proto kernel  scope linksrc 192.168.119.137

技术分享

MySQL从复制示意图

技术分享

 

Master主服务器配置

1、修改my.cnf配置文件

vi /etc/mysql/my.cnf

在[mysqld]中添加:

bind-address            = 192.168.119.137

server-id              = 1

log_bin                        = /var/log/mysql/mysql-bin.log

binlog_do_db           = include_database_name

binlog_ignore_db       = include_database_name

 

注:

server-id 服务器唯一标识;

log_bin 启动MySQL二进制日志;

binlog_do_db 指定记录二进制日志的数据库;

binlog_ignore_db 指定不记录二进制日志的数据库。

技术分享

2、查看log_bin是否生效

[email protected]:~# mysql -uroot -p -e "show variables like ‘log_bin‘;"

Enter password:

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| log_bin       | ON    |

+---------------+-------+

技术分享

3、登陆主服务器MySQL创建从服务器用到的账户和权限

mysql> grant replication slave on *.* to ‘rep‘@‘192.168.119.137‘ identified by ‘ubuntu‘;

Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to ‘masterbackup‘ @‘ 192.168.119.137‘ identified by ‘masterbackup‘;

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

技术分享

4、重启mysql

[email protected]:~# /etc/init.d/mysql restart

技术分享

5、主库上做备份

锁定数据库

mysql> flush table with read lock;

Query OK, 0 rows affected (0.01 sec)

 

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000007 |      107 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.01 sec)

 

mysql> show master logs;

+------------------+-----------+

| Log_name         | File_size |

+------------------+-----------+

| mysql-bin.000001 |       126 |

| mysql-bin.000002 |       126 |

| mysql-bin.000003 |       126 |

| mysql-bin.000004 |       126 |

| mysql-bin.000005 |       126 |

| mysql-bin.000006 |       126 |

| mysql-bin.000007 |       107 |

+------------------+-----------+

7 rows in set (0.00 sec)

 

mysql>

技术分享

 

这个窗口保持不变,重新打开一个终端

[email protected]:~$ sudo su -

[sudo] password for solin:

[email protected]:~# mysqldump -uroot -p -A -B --events --master-data=2  >/opt/rep.sql

Enter password:

[email protected]:~# ls -l /opt/

总用量 4596

-rw-r--r-- 1 root root 4705979  9月  1 17:25 rep.sql

技术分享

 

备份成功,回到刚才终端,解锁数据库

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

 

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000007 |      107 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

mysql> show master logs;

+------------------+-----------+

| Log_name         | File_size |

+------------------+-----------+

| mysql-bin.000001 |       126 |

| mysql-bin.000002 |       126 |

| mysql-bin.000003 |       126 |

| mysql-bin.000004 |       126 |

| mysql-bin.000005 |       126 |

| mysql-bin.000006 |       126 |

| mysql-bin.000007 |       107 |

+------------------+-----------+

7 rows in set (0.00 sec)

技术分享

授权运程登陆

vi /etc/mysql/my.cnf

bind-address = 192.168.119.0

登陆MySQL数据库

grant all privileges on *.* to [email protected]"192.168.119.0" identified by "ubuntu" with grant option;

技术分享

6、把主库的备份迁移到从库(从服务器可以远程连接)

[email protected]:~# mysql -uroot -h192.168.119.137 -p </opt/rep.sql

技术分享

 

Slave服务器配置

1、修改my.cnf配置文件

vi /etc/mysql/my.cnf

bind-address = 0.0.0.0

server-id               = 2

技术分享

2、授权数据库运程连接

登陆MySQL数据库

mysql> grant all privileges on *.* to [email protected]"%" identified by "ubuntu" with grant option;

Query OK, 0 rows affected (0.01 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

技术分享

注:把主库的备份迁移到从库可以在此授权之后做

 

3、从库设置,登录Slave从服务器,连接Master主服务器:

mysql> CHANGE MASTER TO

    -> MASTER_HOST=‘192.168.119.95‘,

    -> MASTER_PORT=3306,

    -> MASTER_USER=‘rep‘,

    -> MASTER_PASSWORD=‘ubuntu‘,

    -> MASTER_LOG_FILE=‘mysql-bin.000007‘,

-> MASTER_LOG_POS=107;

注:

master_host对应主服务器的IP地址:192.168.119.95;

master_port对应主服务器的端口: 3306;

master_log_file对应show master status显示的File列:mysql-bin.000007;

master_log_pos对应Position列:107

技术分享

4、启动slave数据同步

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

技术分享


查看Slave信息

1、show slave status\G

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.119.95

                  Master_User: rep

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000008

          Read_Master_Log_Pos: 107

               Relay_Log_File: mysqld-relay-bin.000003

                Relay_Log_Pos: 253

        Relay_Master_Log_File: mysql-bin.000008

             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: 107

              Relay_Log_Space: 711

              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

1 row in set (0.00 sec)

技术分享

注:Slave_IO_RunningSlave_SQL_Running都为yes才表示同步成功。

 

2、对比数据库,数据同步成功

Master MySQL

技术分享

Slave MySQL

技术分享


本文出自 “运维小当家” 博客,请务必保留此出处http://solin.blog.51cto.com/11319413/1879642

以上是关于生产环境 MySQL主从复制(同步)的主要内容,如果未能解决你的问题,请参考以下文章

MySQL主从复制

MySQL主从复制

MySql主从同步配置及实现

MySQL 主从同步与分离

编写脚本实现MySQL主从复制状态监控

基于Centos 7搭建MySQL主从复制及读写分离