主机配置:
1.首先通过ifconfig拿到主机ip地址,我这里是20.18.7.188
2.关闭防火墙
systemctl stop firewalld(默认)
systemctl disable firewalld.service(设置开启不启动)
3.修改my.cnf配置文件
vim /etc/my.cnf
#启用二进制日志
log-bin=mysql-bin
#服务器唯一ID,一般取IP最后一段
server-id=188
4.重启服务
systemctl restart mysqld
5.给从机授权使从机可以从主机复制数据
mysql -uroot -p
登录
GRANT REPLICATION SLAVE ON . TO ‘从机MySQL用户名’@‘从机IP’ identified by ‘从机MySQL密码’;
我的:
`GRANT REPLICATION SLAVE ON *.* TO 'root'@'%' identified by '123456';`
如果有如下报错:
mysql> GRANT REPLICATION SLAVE ON *.* TO 'root'@'%' identified by '123456';
ERROR 1819 (HY000): 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.00 sec)
mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
6.刷新然后查看master状态
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 589 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
主机配置完毕。
从机配置:
1.修改my.ini
把server-id加上
server-id=2
2.以管理员权限打开cmd重启MySQL
mysql -uroot -p
登录
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)
3.停止从机同步
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
4.给从机配置主机信息
mysql> change master to
-> master_host='20.18.7.188',
-> master_user='root',
-> master_password='123456',
-> master_port=3306,
-> master_log_file='mysql-bin.000001',//这是主机查master status 的File的值
-> master_log_pos=760,//这是主机查master status 的Position的值
-> master_connect_retry=10;//连接失败后等待的秒数
Query OK, 0 rows affected, 1 warning (0.07 sec)
5.启动从机同步
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status \G
*************************** 1. row ***************************
》》》》》》》》》》》》》省略
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
》》》》》》》》》》》》》》》》》》省略
1 row in set (0.02 sec)
至此就配置成功了。
测试:
左面主机,右面从机。