0%

mysql数据库主从复制

需要两个服务器或者两个虚拟机都可以,一个是主数据库一个是从数据

1.第一步需要主服务器修改 /etc/my.conf

1
2
3
4
5
6
7
8
9
10
11
#日志名称
log_bin=master-a-bin

#日志文件格式
binlog-format = Row

#服务器id
server-id=1

#主从复制的库
binlog_do_db=tea

2.修改完需要给重服务器授权(在主服务器的mysql里面执行下面这段代码授权)

1
grant replication slave on *.* to '主服务器mysql账号'@'从服务器地址' identified by '主服务器mysql账号';

当我修改的时候提示了报修改密码错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Your password does not satisfy the current policy requirements

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.01 sec)

经过查看是mysql的密码规则用简单的密码不行,提供一下解决方案(如果上面修改没有报错请忽略这一步)

mysql 5.7解决方案

1
2
set global validate_password_policy=0;
set global validate_password_length=1;

mysql 8.0解决方案

1
2
set global validate_password.policy=0;
set global validate_password.length=1;

再次执行,就成功了

1
2
mysql> grant replication slave on *.* to 'robert'@'2.56.184.38' identified by 'robert&tea321';
Query OK, 0 rows affected, 1 warning (0.00 sec)

3.现在来修改从服务器的mysql

1
2
3
4
5
6
7
8
9
10
11
修改mysql配置文件
vim /etc/my.cnf

#日志名称
log_bin=master-a-bin

#日志文件格式
binlog-format = Row

#服务器id(必须唯一,主服务器是1,这个id不能重复)
server-id=2

重启两个mysql

1
service mysqld restart

4.验证是否设置成功

主服务器mysql

1
2
3
4
5
6
7
mysql> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| master-a-bin.000001 | 4635 | tea | | |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

解释一下上面这几个字段什么意思,这几个字段下面用得到的
file 生成的日志文件名
position 文件名所在的位置简称偏移量
Binlog_Do_DB 需要实现主重复制的数据库

5.设置从服务器找到主服务器

1
2
mysql> change master to master_host='主服务器ip地址', master_user='root', master_password='root', master_port=3306, master_log_file='master-a-bin.000001', master_log_pos= 4635, master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

解释一下上面几个参数的意思
master_host :Master的地址
master_port:Master的端口号
master_user:用于数据同步的用户
master_password:用于同步的用户的密码
master_log_file:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值
master_log_pos:从哪个 Position 开始读,即上文中提到的 Position 字段的值
master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒

6.启动slave的数据同步

1
2
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

停止同步命令
stop slave;

查看slave配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 154.17.0.136
Master_User: robert
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: master-a-bin.000001
Read_Master_Log_Pos: 4635
Relay_Log_File: mysqld-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-a-bin.000001
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: 4635
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: 1045
Last_IO_Error: error connecting to master 'robert@154.17.0.136:3306' - retry-time: 30 retries: 5
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: 200704 03:52:46
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.01 sec)

ERROR:
No query specified

出现上面Slave_IO_State: Connecting表示没链接上然后排查一下问题什么情况,建议检查3个点,可以用可以尝试使用navicat链接一下,看看能不能连接上,如果能连接应该就是账号密码没填对检查一下

1。主服务器防火墙问题
2,端口问题
3,账号密码地址问题

最后我发现是我的账号密码不正确导致没链接上

停止slave然后重新修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
mysql> stop slave;

mysql> change master to master_host='主服务器ip地址', master_user='root', master_password='root', master_port=3306, master_log_file='master-a-bin.000001', master_log_pos= 4635, master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> start slave;

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 154.11.0.116
Master_User: root
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: master-a-bin.000001
Read_Master_Log_Pos: 34730
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 30384
Relay_Master_Log_File: master-a-bin.000001
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: 34730
Relay_Log_Space: 30558
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
Master_UUID: 3becbc01-aaf9-11ea-8f6d-52e142c79032
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)

然后再次查看链接上了,这样就配置完成了!