1.需要两台服务器,如我的分别是:
1. -》 192.168.197.146---主库
2. -》 192.168.197.147---从库
2.分别关闭防火墙或者放行mysql端口号3306
如: systemctl stop firewalld
systemctl disable firewalld
1.在mysql的配置文件 /etc/my.cnf中添加:
server-id=1
read-only=0
2.重启mysql
systemctl restart mysql
如果重启中没有报错,就说明没有问题
3.登录mysql,创建远程连接的账号,并授予主从复制权限
mysql> create user 'redhat'@'%' identified with mysql_native_password by 'Root@123456';
Query OK, 0 rows affected (0.01 sec)mysql> grant replication slave on *.* to 'redhat'@'%';
Query OK, 0 rows affected (0.00 sec)ps:
如果 show master status;没有东西,那么可能是你的二进制文件没有开启开关,5.6版本以上要手动指定。
如: 在/etc/my.cnf中,添加:log-bin=mysqld-binlog
这样主库就配置好了
1.在my.cnf中
server-id=2
read-only=1
#supper-read-only 指像超级管理员那样的权限
2.重启mysqld服务
systemctl restart mysqld
3.登陆从库的mysql,设置登陆配置:
mysql> change master to
-> master_host='192.168.197.146',
-> master_user='redhat',
-> master_password='Root@123456',
-> master_log_file='mysqld-binlog.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
4.开启同步操作:
start replica --8.0.22版本之后
start slave 8.0.22版本之前
5.查看同步操作:主要看下面两个是否为Yes,状态正常
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.197.146
Master_User: bak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqld-binlog.000001
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysqld-binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes.......
在主库中创建数据库和表,并插入数据:
mysql> create database db01;
Query OK, 1 row affected (0.01 sec)mysql> use db01;
Database changedmysql> create table tb_user(
id int(11) primary key not null auto_increment,
name varchar(50) not null,
sex varchar(1)
)engine=innodb default charset=utf8mb4;
Query OK, 0 rows affected (0.01 sec)mysql> insert into tb_user values(null,'Tom','1'),(null,'Tigger','0'),(null,'Dawn','1');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql> show tables;
+----------------+
| Tables_in_db01 |
+----------------+
| tb_user |
+----------------+
1 row in set (0.00 sec)mysql> select * from tb_user;
+----+--------+------+
| id | name | sex |
+----+--------+------+
| 1 | Tom | 1 |
| 2 | Tigger | 0 |
| 3 | Dawn | 1 |
+----+--------+------+
3 rows in set (0.00 sec)
从库:
你可以进入从库里面去查看,是否有数据库在,如果有,你再 在主库里面进行update改数据操作,然后看从库里面的数据是否同步更新,如果能达到这个效果,那就成功了