MySql主从配置记录

主服务器192.168.0.171
从服务器192.168.0.172
服务器环境 Centos6.0
mysql版本5.1.66

1.主MySql配置
登录到主服务器192.168.0.171的mysql,添加具有复制权限(REPLICATION)用户

mysql>GRANT REPLICATION SLAVE ON *.* TO 'backup'@'192.168.1.172' IDENTIFIED BY '1234';

赋予从机权限,有多台丛机,就执行多次
/etc/my.cnf
在[mysqld]段中添加

log-bin=mysql-bin 
server-id=1 #主机标示,整数
read-only=0 #主机,读写都可以
binlog-do-db=test #需要备份数据,多个写多
binlog-ignore-db=mysql  #不需要备份的数据库,多个写多行

2.从MySql配置
/etc/my.cnf
在[mysqld]段中添加

log-bin=mysql-bin
server-id=2
master-host=192.168.0.171
master-user=backup
master-pass=1234
master-port=3306
master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
replicate-do-db =test #只复制某个库
replicate-ignore-db=mysql #不复制某个库

3.重启mysql

检查下能否用backup账户从192.168.0.172登录到192.168.0.171
如果登录不了,可能的原因是iptables防火墙造成

查询主数据库状态

mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 106
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
 
ERROR:
No query specified

从服务器
检查主从同步
如果您看到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.0.171
                  Master_User: backup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 106
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql
           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: 106
              Relay_Log_Space: 407
              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:
1 row in set (0.00 sec)
 
ERROR:
No query specified

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注