5
在主上增加复制用户
mysql> grant replication slave on *.* to ‘repl‘@‘%‘ identified by ‘123456‘;
flush privileges;
192.168.18.67是从服务器,就通过repl用户密码为空来同步复制
mysql> select host,user,Repl_slave_priv from mysql.user where user=‘repl‘;
+---------------+------+-----------------+
| host | user | Repl_slave_priv |
+---------------+------+-----------------+
| 192.168.18.67 | repl | Y |
+---------------+------+-----------------+
1 row in set (0.00 sec)
37 --
38 -- Dumping routines for database ‘reptest‘
39 --
40 /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
41 /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
42 /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
43 /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
44 /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
45 /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
46 /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
47 /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
从:
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.18.66
Master_User: usrep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 1264
Relay_Log_File: mysql-relay-bin.000021
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000004
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: 1264
Relay_Log_Space: 1075
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: 10
Master_UUID: c03d6252-2a2f-11e4-9b48-000c291888ce
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)
主:可以看到下面内容
mysql> show processlist \G
*************************** 2. row ***************************
Id: 11
User: usrep
Host: 192.168.18.67:48746
db: NULL
Command: Binlog Dump
Time: 179
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
2 rows in set (0.00 sec)
从服务器:
数据文件中相关文件如下
[root@host2 ~]# ll /var/lib/mysql/
-rw-rw----. 1 mysql mysql 128 Aug 28 11:32 master.info
-rw-rw----. 1 mysql mysql 59 Aug 28 11:32 relay-log.info
-rw-rw----. 1 mysql mysql 792 Aug 28 11:32 mysql-relay-bin.000020
-rw-rw----. 1 mysql mysql 283 Aug 28 11:32 mysql-relay-bin.000021
-rw-rw----. 1 mysql mysql 50 Aug 28 11:32 mysql-relay-bin.index
主服务器:
数据文件中相关文件如下
-rw-rw----. 1 mysql mysql 1036 Aug 28 09:32 mysql-bin.000003
-rw-rw----. 1 mysql mysql 1264 Aug 28 11:04 mysql-bin.000004
-rw-rw----. 1 mysql mysql 76 Aug 28 09:32 mysql-bin.index
到这儿就ok了
下面说一下在配置过程中遇到的问题:
问题1
1
刚搭完跑起来一看,有问题,Slave_IO_Running: Connecting,IO线程链接主服务进程没有成功
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.18.66
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 401
Relay_Log_File: host2-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Connecting
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: 401
Relay_Log_Space: 120
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2003
Last_IO_Error: error connecting to master ‘repl@192.168.18.66:3306‘ - retry-time: 60 retries: 1
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 140825 14:29:05
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
问题1
2
查看日志有如下内容
[root@host2 ~]# tail -n 30 /var/lib/mysql/host2.err
2014-08-27 17:04:37 2384 [ERROR] Slave I/O: error connecting to master ‘repl@192.168.18.66:3306‘ - retry-time: 60 retries: 1, Error_code: 2003
2014-08-27 17:04:37 2384 [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
2014-08-27 17:04:37 2384 [Note] Slave SQL thread initialized, starting replication in log ‘mysql-bin.000003‘ at position 120, relay log ‘./mysql-relay-bin.000001‘ position: 4
2014-08-27 17:05:12 2384 [Note] Error reading relay log event: slave SQL thread was killed
2014-08-27 17:05:12 2384 [Note] Slave I/O thread killed while connecting to master
2014-08-27 17:05:12 2384 [Note] Slave I/O thread exiting, read up to log ‘mysql-bin.000003‘, position 120
问题1
3
在主上新建一个全权用户,在从上用这个用户做复制,结果一致
主:
mysql> grant all on *.* to ‘usrep‘@‘%‘ identified by ‘123456‘;
从
mysql> Change master to Master_host = ‘192.168.18.66‘, Master_port = 3306, Master_user = ‘repl‘, Master_password = ‘123456‘, Master_log_file = ‘mysql-bin.000002‘, Master_log_pos = 401;
用usrep在从上起slave复制线程,问题依旧
问题1
4
在主上mysql -uusrep -p直接登录主数据库,成功。
在从上mysql -h 192.168.18.67 -uusrep -p登录主数据库,失败。
[root@host2 ~]# mysql -h 192.168.18.67 -uroot -psystem
Warning: Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘192.168.18.66‘ (113)
问题1
5
查看主的iptable
[root@host3 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
从从服务器恢复
如果主机挂了,可以把从服务器提升为主机,把原主服务器作为备机。
先在从服务器停止同步线程:
stop slave;
在从服务器上添加同步用户:
grant replication slave on *.* to repl@‘从服务器ip‘ identified by ‘123456‘;
flush privileges;
配置文件中my.cnf的server-id可以不修改,只要保证id不冲突就行了。