1、搭建两套linux服务器并搭载mysql服务。
建议双方mysql版本相同,如不相同应
主版本低于从版本。
我用的是vmw虚拟机操作,可以对主服务器进行复制克隆。(克隆的机器由于网卡的原因要设置
网络,没采用,我使用iso镜像自己重新安装的服务器。)
设置好从服务器的网络之后互通。本机(主:192.168.125.128 从:192.168.125.129)
把主服务器的cnf文件和安装压缩包拷贝到从服务器。安装mysql(和主服务器版本一致);
2、配置完成后进行相关的cnf文件配置,mysql服务自带复制功能。
主服务器(3306):
改服务器id: server-id=1
启用二进制日志: log-bin=/database/3306/binlog/3306_bin
关闭中继日志: relay-log =OFF
创建有复制权限的账号:GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO
'repluser'@'192.168.%.%' identified by '123456';
从服务器(3307):
改服务器id: server-id=11(和主服务器不同即可)
关闭二进制日志: log-bin=OFF
开启中继日志: relay-log = /database/3307/relaylog/3307-relay-log.info
3、连接主服务器:
change master to master_host='192.168.125.128',master_user='repluser',master_password='123456'
本次测试的是完全复制,并不是从某个日志节点中间开始的。所以只需要指定主机用户名密码就好,其他的会自动默认。
相关的查看命令:
show processlist:查看mysql服务器中所有的链接线程。
mysql> show processlist;
+----+-----------------+-----------+-------+---------+------+------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+-------+---------+------+------------------------+------------------+
| 3 | event_scheduler | localhost | NULL | Daemon | 8965 | Waiting on empty queue | NULL |
| 25 | xuning | localhost | mysql | Query | 0 | starting | show processlist |
+----+-----------------+-----------+-------+---------+------+------------------------+------------------+
2 rows in set (0.00 sec)
show master status\G;
show master status\G;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.125.128
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: 3306_bin.000013
Read_Master_Log_Pos: 2896
Relay_Log_File: 3307-relay-log.000017
Relay_Log_Pos: 3107
Relay_Master_Log_File: 3306_bin.000013
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
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: 2896
Relay_Log_Space: 3841
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: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 2c3e663b-f680-11e7-b5df-000c29cd8688
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
和主服务器做好连接后并没有开始复制,因为这两个选项是NO
Slave_IO_Running: No
Slave_SQL_Running: No
变成ON状态即为复制开始状态
开启和关闭复制进程:start slave; && stop slave;
使用show processlist
mysql> show processlist;
+----+-----------------+-----------+-------+---------+------+--------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+-------+---------+------+--------------------------------------------------------+------------------+
| 3 | event_scheduler | localhost | NULL | Daemon | 9339 | Waiting on empty queue | NULL |
| 25 | xuning | localhost | mysql | Query | 0 | starting | show processlist |
| 26 | system user | | NULL | Connect | 9 | Waiting for master to send event | NULL |
| 27 | system user | | NULL | Connect | 9 | Slave has read all relay log; waiting for more updates | NULL |
+----+-----------------+-----------+-------+---------+------+--------------------------------------------------------+------------------+
4 rows in set (0.00 sec)
多了一个读取中继日志并应用的进程。
查看错误日志发现会刷这种报错:
2018-03-06T01:58:13.393151Z 10 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-03-06T01:58:13.395272Z 10 [ERROR] Slave I/O for channel '': error connecting to master '
[email protected]:3306' - retry-time: 60 retries: 1, Error_code: 2003
2018-03-06T01:58:13.397137Z 11 [Note] Slave SQL thread for channel '' initialized, starting replication in log 'FIRST' at position 0, relay log '/database/3307/relaylog/3307-relay-log.000001' position: 4
2018-03-06T01:59:14.398628Z 10 [ERROR] Slave I/O for channel '': error connecting to master '
[email protected]:3306' - retry-time: 60 retries: 2, Error_code: 2003
2018-03-06T02:00:14.400085Z 10 [ERROR] Slave I/O for channel '': error connecting to master '
[email protected]:3306' - retry-time: 60 retries: 3, Error_code: 2003
2018-03-06T02:01:04.688209Z 11 [Note] Error reading relay log event for channel '': slave SQL thread was killed
2018-03-06T02:01:04.689185Z 10 [Note] Slave I/O thread for channel '' killed while connecting to master
2018-03-06T02:01:04.689214Z 10 [Note] Slave I/O thread exiting for channel '', read up to log 'FIRST', position 4
2018-03-06T02:02:00.880235Z 12 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-03-06T02:02:00.881161Z 12 [ERROR] Slave I/O for channel '': error connecting to master '
[email protected]:3306' - retry-time: 60 retries: 1, Error_code: 2003
2018-03-06T02:02:00.883062Z 13 [Note] Slave SQL thread for channel '' initialized, starting replication in log 'FIRST' at position 0, relay log '/database/3307/relaylog/3307-relay-log.000001' position: 4
一直报这个错,百度了一下错误码 Error_code: 2003 发现可能其原因可能是
1、密码不对
2、网络不通
3、防火墙问题
排除了前两个之后于是尝试着去看是否为防火墙的原因:
chkconfig iptables off
service iptables off
setenforce 0
输入这三个命令关闭了防火墙
重新stop slave 和 start slave 之后
再次查看错误日志发现没有了error报错
[root@localhost relaylog]# cat /database/3307/data/3307.err
2018-03-06T02:04:53.932707Z 16 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-03-06T02:04:53.938867Z 17 [Note] Slave SQL thread for channel '' initialized, starting replication in log 'FIRST' at position 0, relay log '/database/3307/relaylog/3307-relay-log.000001' position: 4
2018-03-06T02:04:53.957996Z 16 [Note] Slave I/O thread for channel '': connected to master '[email protected]:3306',replication started in log 'FIRST' at position 4
2018-03-06T02:05:02.656178Z 17 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2018-03-06T02:05:02.656242Z 17 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2018-03-06T02:05:02.656263Z 17 [Warning] 'user' entry 'xuning@localhost' ignored in --skip-name-resolve mode.
2018-03-06T02:05:02.656293Z 17 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2018-03-06T02:05:02.656315Z 17 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2018-03-06T02:05:02.656431Z 17 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
4、查看了mysql的user表,发现主库里面的复制用户也被同步过来了:
mysql> select user,host,authentication_string from user;
+-----------+-------------+-------------------------------------------+
| user | host | authentication_string |
+-----------+-------------+-------------------------------------------+
| root | localhost | |
| mysql.sys | localhost | |
| xuning | localhost | |
| repluser | 192.168.%.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| abc | % | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-----------+-------------+-------------------------------------------+
5 rows in set (0.00 sec)
测试结束