MySQL5.5配置主从复制_CentOS6.5下进行

来源:互联网 发布:苏联援助国民党 知乎 编辑:程序博客网 时间:2024/06/07 07:57

MySQL5.5配置Master-Slave复制

 

1. 环境

操作系统 VMWare 10 CentOS6.5

Master192.168.52.128

Slave192.168.52.129

2. 配置过程

ps: 主从配置必须确保MasterSlave都运行bin-log

mysql> show variables like 'log_bin';

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

| Variable_name | Value |

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

| log_bin       | ON    |

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

1 row in set (0.00 sec)

 

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000011 |      348 |              |                  |

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

1 row in set (0.05 sec)

 

2.1 登录Master上的MySQL Server

mysql -u root -proot

2.2 创建Slave上连接Master进行主从复制的用户

grant replicationslave on *.* to ‘slaveone’ @ ‘192.168.52.129’ identified by ‘slaveone’

2.3 关闭Master上的MySQL服务,对my.cnf进行修改

service mysql stop

 

( 配置文件my.cnf的获取方法 cp/usr/share/mysql/my-huge.cnf  /etc/my.cnf)

 

vim /etc/my.cnf 在该配置文件的最末尾处插入下面语句

# Master Config

server-id = 1

log-bin = mysql-bin

binlog-do-db = test

binlog-ignore-db = mysql

 

2.4 关闭Slave MySQL服务,修改对应的my.cnf文件

service mysql stop

 

vim /etc/my.cnf 在该配置文件的最末尾处插入下面语句

# Slave Config

server-id = 2

replicate-do-db = test

replicate-ignore-db = mysql

 

2.5 重启服务

service mysqlstart

2.6 登录Slave上的MySQL Server,进行主从配置

mysql -uroot -proot

Change master tomaster_host=192.168.52.128,

master_user=’slaveone’,

master_password=’slaveone’,

master_log_file=’mysql-bin.000011’ #这个二进制日志文件名需要查看Master当前binlog状态

master_log_pos=348#这个二进制日志文件位置需要查看Master当前binlog状态

 

2.7 进行主从同步操作

这里可以使用mysqldump工具将Master数据库(表)数据导出为.sql文件,然后在Slave上执行,以达到Master和Slave的同步。关于mysqldump的用法请自行百度或查看mysql官方文档。

2.8 测试是否达到同步状态

mysql> show slave status\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.52.128

                  Master_User: slaveone

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000011

          Read_Master_Log_Pos: 348

               Relay_Log_File: localhost-relay-bin.000022

                Relay_Log_Pos: 494

        Relay_Master_Log_File: mysql-bin.000011

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

              Relay_Log_Space: 800

              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.06 sec)

 

ERROR:

No query specified

 

如果查询结果中Slave_IO_Running:YesSlave_SQL_Running: Yes那么证明已经达到主从同步状态,如果没有,请确保是否开启slave:

mysql > start slave;

 

如果还没有达成两个参数均为Yes,请进行以下检查:

ü        检查两台服务器是否能够正确连接网络。

ü        检查Slave服务器是否能够ping通Master服务器。

ü        检查Slave服务器上show slavestatus\G 命令中的Last_IO_Error:和 Last_SQL_Error:  检查出现Error的原因,自行上网搜索进行解决。

 

0 0
原创粉丝点击