Linux下配置MySQL主从复制

一、环境准备

本次准备两台Linux主机,操作系统都为CentOS6.8, 都安装了相同版本的MySQL.(MySQL5.7)。

主从服务器的防火墙都开启了3306端口。


相关信息如下:

【主服务器】

IP: 192.168.236.100


【从服务器】

IP: 192.168.236.101


二、配置主从复制


主从复制原理:MySQL做主从复制,主要依靠二进制日志。主服务器做什么,从服务器就跟着做什么。

主服务器有一个日志功能,把自己所做的增删改查操作都记录在日志中。从服务器拿到这份日志,根据日志的动作自己做一遍就行了。这样就实现了主从复制。


1.首先修改配置/etc/my.cnf,使其支持二进制日志功能

【主服务器】

在配置文件中添加如下三行代码:

log-bin=mysql-bin
binlog_format=mixed
server-id=100
server-id中的100为该主机IP的末位,方便区分。当然也可以设置其他值,但不要和从服务器相同。


添加完成后保存,并重启MySQL.


参数解释:

log-bin=mysql-bin          //将MySQL二进制日志取名为mysql-bin

binlog_format=mixed    //二进制日志的格式,有三种:statement/row/mixed, 具体分别不多做解释。这里设置mixed.

server-id =100               //为服务器设置独立无二的ID便于区分,这里使用IP地址的最后一位充当server-id.




【从服务器】

从服务器上也按上述操作添加代码,只是server-id不同而已。

log-bin=mysql-bin
binlog_format=mixed
server-id=101

设置完成后保存,并重启MySQL.


2. 在主服务器上为从服务器分配一个账号,就像一把钥匙,从服务器拿着这把钥匙,才能到主服务器上来共享主服务器的日志文件。

【主服务器】

  • 使用root用户登录主服务器的MySQL数据库,执行如下命令来创建一个账号。

GRANT replication slave ON *.* TO 'slave'@'%' IDENTIFIED BY '1234'; 



  • 查看主服务器的BIN日志信息(执行完之后记录下File和Position的值,然后在配置完从服务器之前不要做任何操作,因为每次操作服务器的这两个值都会发生变化)



3. 设置从服务器

【从服务器】

使用root用户登录从服务器的MySQL. 

  • 关闭slave(如果之前设置过主从复制的话)
            
stop slave;

  • 执行如下代码
CHANGE MASTER TO  MASTER_HOST="192.168.236.100",MASTER_USER="slave",MASTER_PASSWORD="1234",MASTER_LOG_FILE="mysql-bin.000001",MASTER_LOG_POS=438;

参数解释:

MASTER_HOST:   设置要连接的主服务器的IP地址
MASTER_USER:  设置要连接的主服务器的用户名
MASTER_PASSWORD:  设置要连接的主服务器的密码
MASTER_LOG_FILE:  设置要连接的主服务器的BIN日志的日志名称,即第2步得到的信息
MASTER_LOG_POS:  设置要连接主服务器的BIN日志的记录位置,即第2步得到的信息。注意:不要加引号,否则配置失败。

  • 启动从服务器
start slave;
  • 查看从服务器状态,如果Slave_IO_Running和Slave_SQL_Running的值都为YES,说明配置成功。不成功的话,重复上述步骤。
mysql> show slave status;
+----------------------------------+-----------------+-------------+-------------+---------------+------------------+---------------------+--------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-----------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State                   | Master_Host     | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File           | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File                  | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------------------------+-----------------+-------------+-------------+---------------+------------------+---------------------+--------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-----------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | 192.168.236.100 | slave       |        3306 |            60 | mysql-bin.000001 |                 438 | CentOS6-relay-bin.000002 |           320 | mysql-bin.000001      | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 438 |             529 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |              100 | 087d3459-2c02-11e7-a39a-000c29073d16 | /usr/local/mysql/data/master.info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------------------------+-----------------+-------------+-------------+---------------+------------------+---------------------+--------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-----------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.01 sec)


4. 至此,主从复制配置完毕!


三、测试


1.首先查看主从服务器上有哪些数据库

【主服务器】
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+



【从服务器】
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+




2.在主服务器上新建一个数据库monkey
create database monkey;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| monkey             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

3.查看从服务器上是否也有了monkey这个数据库,如果有,说明主从复制正常。大功告成!

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| monkey             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+










posted @ 2017-05-11 16:05  foreverfriends  阅读(193)  评论(0编辑  收藏  举报