- 实现Mysql 主从数据库同步
主从数据库的的同步,最好使用相同版本的,避免在解决兼容方面下功夫
数据库版本 :
win10:mysql-5.7.29-winx64
centos7:mysql57-community-release-el7-10.noarch.rpm
- 原理
参考:https://www.cnblogs.com/fxmemory/p/7198663.html
https://blog.csdn.net/vipbupafeng/article/details/80272946
准备工作
- 关闭 centos7的:关闭 firewalld.service,关闭SELiunx
1. 主 master (win10)
1.1 文件配置 my.ini
(注意放在 [mysqld]
标签下)
[mysqld]
server-id=1 # master服务唯一标识
# 二进制日志
log-bin=master-bin
log-bin-index=master-bin.index
binlog-do-db=shop # 同步的数据库:shop
保存后重启服务 (cmd)
net stop mysql #停止
net start mysql # 启动
- 查看master状态信息:
show master status;
1.2 添加用户 (执行 主从同步)
- 查看当前授权用户
mysql> use mysql;
mysql> select user,host from user;
- 创建 用户 repl
mysql> create user repl;
- 授予 权限 和设置登陆密码
# 仅仅主从复制的权限(特定ip访问)
mysql> grant replication slave on *.* to 'repl'@'192.168.137.1' identified by '123456';
# 或者设置 所有权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'repl'@'%' IDENTIFIED BY '123456';
- 刷新权限:
flush privileges;
- 查询用户权限
mysql> show grants for 'repl'@'%';
mysql> show grants for 'repl'@'192.168.137.1';
2. 从 slave(centos7)
(注意关闭防火墙或开放3306端口)
2.1 文件配置 my.cnf
vim /etc/my.cnf
文件中添加
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
重启 mysqld
2. 主从设置
先关闭
mysql> stop slave;
mysql> change master to
-> master_host='192.168.137.1',
-> master_port=3307,
-> master_user='repl',
-> master_password='123456',
-> master_log_file='master-bin.000003',
-> master_log_pos=0;
参数详解:
master_host: 主服务器的IP
master_user: 主服务器上新创建的用户名
master_password: 用户的密码
master_port: 主服务器的端口,如果未曾修改,默认即可。
master_log_file: 主服务器二进制日志文件的名称,填写查看主服务器的master状态时显示的File的值
master_log_pos: 日志的位置,填写查看主服务器的master状态时显示的Position的值
参考 https://blog.csdn.net/langqingj/article/details/80924251
- 启动从服务:
mysql> start slave;
3. 同步完成
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.137.1
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 1665
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 1880
Relay_Master_Log_File: master-bin.000003
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: 1665
Relay_Log_Space: 2087
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: b971a2e9-81e3-11ea-811b-00ffd98c9741
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 more updates
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)
参考 https://blog.csdn.net/langqingj/article/details/80924251
备注
1. 在 win10 中 的mysql 安装 validate_password插件
- 安装插件:
INSTALL PLUGIN validate_password SONAME 'validate_password.dll';
- 查看插件 validate_password 的相关信息
validate_password_dictionary_file :验证密码的字典文件,与之相同的不可使用
validate_password_length :密码最少长度
validate_password_number_count :最少数字字符数
validate_password_mixed_case_count :最少大写和小写字符数(同时有大写和小写)
validate_password_special_char_count :最少特殊字符数
validate_password_policy :密码安全策略:
low: 只限制长度
MEDIUM: 限制长度、数字、字母、特殊字符
STRONG: 限制长度、数字、字母、特殊字符、字典
show global variables like '%validate_password%';
参考:https://www.cnblogs.com/hsjava1/p/9789549.html
2. 修改密码
修改一个用户的密码
mysql> update user set password=password(“新密码”) where user=”用户名”;
执行后报错
ERROR 1054(42S22) Unknown column ‘password’ in ‘field list’
错误的原因是 5.7版本下的mysql数据库下已经没有password这个字段了,password字段改成了authentication_string
所以请使用一下命令:
update mysql.user set authentication_string=password('***') where user='***'; #修改密码成功
参考:https://www.cnblogs.com/wangbaobao/p/7087032.html
3. ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
- 属于密码策略问题
- 查看 mysql 初始的密码策略,
输入语句 “SHOW VARIABLES LIKE 'validate_password%';
” 进行查看, - 设置密码的验证强度等级,设置 validate_password_policy 的全局参数为 LOW 即可,
输入设值语句 “set global validate_password_policy=LOW;
” 进行设值, - 设置为 6 位的密码,设置 validate_password_length 的全局参数为 6 即可,
输入设值语句 “set global validate_password_length=6;
” 进行设值, - ··· ···
参考:https://blog.csdn.net/shujuelin/article/details/81261731