124 lines
3.4 KiB
Markdown
124 lines
3.4 KiB
Markdown
|
## mysql8.0 主从配置
|
|||
|
|
|||
|
### 一、主库master配置
|
|||
|
|
|||
|
#### 1、修改配置文件开启bin-log
|
|||
|
|
|||
|
```sh
|
|||
|
#mysql8.0版本
|
|||
|
vim /etc/mysql/mysql.conf.d/mysqld.cnf
|
|||
|
#在mysqld.cnf最下面添加
|
|||
|
#开启binlog 备份文件名称
|
|||
|
log-bin = mysql-bin
|
|||
|
#server-id在主从复制链路中服务id不能相同
|
|||
|
server-id = 101
|
|||
|
#不开启binlog的数据库
|
|||
|
binlog-ignore-db=information_schema
|
|||
|
binlog-ignore-db=sys
|
|||
|
binlog-ignore-db=performance_schema
|
|||
|
binlog-ignore-db=mysql
|
|||
|
#只对一下数据库输出binlog 可以写多个,不写则对所有未忽略的输出binlog
|
|||
|
binlog-do-db = test
|
|||
|
```
|
|||
|
|
|||
|
#### 2、创建master用户,并且设置复制权限
|
|||
|
|
|||
|
```mysql
|
|||
|
#创建用户 用户名可以随意命名,访问地址设置从库ip
|
|||
|
created user 'master'@'从库ip' identified with mysql_native_password by 'your password';
|
|||
|
#赋予权限
|
|||
|
grant replication slave on *.* to 'master'@'从库ip';
|
|||
|
#刷新使刚才操作生效
|
|||
|
flush privileges;
|
|||
|
```
|
|||
|
|
|||
|
#### 3、查看配置是否生效
|
|||
|
|
|||
|
```mysql
|
|||
|
#获取server_id 判断是否和配置文件中server_id相同
|
|||
|
show variables like 'server_id';
|
|||
|
#不相同则设置和配置文件相同
|
|||
|
show variables like 'server_id';
|
|||
|
#刷新后再次查看
|
|||
|
flush privileges;
|
|||
|
show variables like 'server_id';
|
|||
|
|
|||
|
#查看master配置 重启mysql服务生效 sudo service mysql resatrt;
|
|||
|
show master status;
|
|||
|
```
|
|||
|
|
|||
|
### 二、从库slave配置
|
|||
|
|
|||
|
#### 1、修改配置文件
|
|||
|
|
|||
|
```shell
|
|||
|
#server-id 不能和主库相同
|
|||
|
server-id=102
|
|||
|
#忽略下方所有表操作
|
|||
|
replicate_wild_ignore_table= information_schema.%
|
|||
|
replicate_wild_ignore_table= sys.%
|
|||
|
replicate_wild_ignore_table= performance_schema.%
|
|||
|
replicate_wild_ignore_table= mysql.%
|
|||
|
```
|
|||
|
|
|||
|
#### 2、设置主节点参数
|
|||
|
|
|||
|
```mysql
|
|||
|
mysql> CHANGE MASTER TO
|
|||
|
MASTER_HOST='主服务器id', -- 主库(master)IP
|
|||
|
MASTER_USER='master', -- 主库(master)用户
|
|||
|
MASTER_PASSWORD='123456', -- 主库(master)用户密码
|
|||
|
MASTER_LOG_FILE='mysql-bin.000001', -- 主库(master)地址
|
|||
|
MASTER_LOG_POS=856; -- 主库(master)位置 在主库中输入 show master status; 可以查看binlog地址 和 position位置
|
|||
|
```
|
|||
|
|
|||
|
#### 3、查看配置是否生效
|
|||
|
|
|||
|
```mysql
|
|||
|
#获取server_id 判断是否和配置文件中server_id相同
|
|||
|
show variables like 'server_id';
|
|||
|
#不相同则设置和配置文件相同
|
|||
|
show variables like 'server_id';
|
|||
|
#刷新后再次查看
|
|||
|
flush privileges;
|
|||
|
show variables like 'server_id';
|
|||
|
```
|
|||
|
|
|||
|
#### 4、开启主从同步
|
|||
|
|
|||
|
```mysql
|
|||
|
#开启主从
|
|||
|
start slave;
|
|||
|
#查看主从同步状态
|
|||
|
show slave status\G;
|
|||
|
#状态如下则表示同步成功
|
|||
|
#Slave_IO_State: Waiting for source to send event
|
|||
|
#Master_Host: 192.168.3.50
|
|||
|
#Master_User: master50
|
|||
|
#Slave_IO_Running: Yes
|
|||
|
#Slave_SQL_Running: Yes
|
|||
|
```
|
|||
|
|
|||
|
### 三、注意
|
|||
|
|
|||
|
#### 1、跨库操作不同补问题
|
|||
|
|
|||
|
我们在主库中添加了忽略库,这里在mysql中 使用 database.table 进行跨库操作就会拦截跨库的操作,所以建议不要使用跨库操作,建议使用use database 切换操作库或者去掉主库忽略配置;
|
|||
|
|
|||
|
```mysql
|
|||
|
use mysql;
|
|||
|
delete test.user where id = 1
|
|||
|
#因为所在库是mysql,mysql是我们的忽略库 他不会生成binlog 所以不会向从库同步
|
|||
|
```
|
|||
|
|
|||
|
#### 2、从库不向主库同步
|
|||
|
|
|||
|
对从库进行操作不会对主库更新,如果想要从库向主库同步,将住服务器当从服务器,从服务器当主服务器,在执行一遍流程
|
|||
|
|
|||
|
#### 3、配置注意事项
|
|||
|
|
|||
|
(1) 主从服务器操作系统保持一致
|
|||
|
|
|||
|
(2) 主从数据库版本一致
|
|||
|
|
|||
|
(3) 主库开启bin-log,主库和从库的server_id 唯一
|