1:设主库允许远程连接(注意:设置账号密码必须使用的插件是mysql_native_password,其他的会连接失败)
mysql> use mysql;
mysql> update user set host = "%" where user = "root" ;
mysql> flush privileges;
2:主库开启bin-log日志在my.cnf的[mysqld]下添加
vim /etc/my.cnf
server-id= 1
log-bin= mysql-bin
2-1:重启主库mysql
[root@localhost ~]# systemctl retart mysql
2-2:查看主库server 相关信息
mysql> show global variables like '%server%' ;
+---------------------------------+--------------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------------+
| character_set_server | utf8mb4 |
| collation_server | utf8mb4_0900_ai_ci |
| innodb_dedicated_server | OFF |
| innodb_ft_server_stopword_table | |
| server_id | 1 |
| server_id_bits | 32 |
| server_uuid | b434ab49-df79-11ee-9c39-0242ac110002 |
+---------------------------------+--------------------------------------+
7 rows in set ( 0.00 sec) mysql>
2-2:查看主库是否开启bin-log日志
mysql> show global variables like '%log%' ;
2-3:查看主库的最新状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 157 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set ( 0.00 sec) mysql> FLUSH LOGS;
Query OK, 0 rows affected ( 0.01 sec) mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 157 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set ( 0.00 sec) mysql>
2-4:查看全部的二进制文件,可以选择一步一步恢复
mysql> SHOW BINARY LOGS;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 | 204 | No |
| mysql-bin.000002 | 204 | No |
| mysql-bin.000003 | 204 | No |
| mysql-bin.000004 | 204 | No |
| mysql-bin.000005 | 157 | No |
+------------------+-----------+-----------+
5 rows in set ( 0.00 sec) mysql>
3:从库开启bin-log日志在my.cnf的[mysqld]下添加
修改配置文件:vim /etc/my.cnf
server-id= 2
relay-log= mysql-relay
3-1:查看主库是否开启bin-log日志,从库也是一样
4:在主库创建一个账号slave,密码123456,并且授权连接
mysql> GRANT REPLICATION SLAVE ON *.* to 'slave' @'%' identified by '123456' ;
4-1:在从库指定主库(添加 主节点主机,访问主节点的用户名及密码,主节点二进制文件信息,注意:主节点的二进制文件一定要是二进制列表中的最后一个二进制文件。如果需要设置主主就是主库也执行如下的命令,ip,账号,密码换成从库的)
mysql> change master to master_host = '192.168.11.47' ,master_user= 'root' ,master_password= 'root' ,master_log_file= 'mysql-bin.000005' ,master_log_pos= 157 ;
Query OK, 0 rows affected, 8 warnings ( 0.01 sec)
4-2:主主设置
mysql> change master to master_host = '192.168.11.47' ,master_user= 'root' ,master_password= 'root' ,master_log_file= 'mysql-bin.000005' ,master_auto_position= 1 ;
Query OK, 0 rows affected, 8 warnings ( 0.01 sec)
## <font color=red>5:检查从库复制功能状态(注意:Slave_IO_Running,Slave_SQL_Running,两个线程必须为yes状态,否则主从复制搭建不成功)```bash
show slave status \G;
5-1:先停止从库的复制功能
mysql> stop slave;
5-2:再启动从库的复制功能
mysql> start slave;
5-3:再查看从库的状态
show slave status \ G;