Linux-mysql主从复制

sunnyxuebuhui 2020-04-27

mysql

mariadb 是mysql的分支

可以直接在官方网站下载

下载

```
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
```

解压

```
tar xf MySQL-5.6.44-1.el7.x86_64.rpm-bundle.tar
```

安装

```
yum install -y *.rpm
```

默认安装位置:/var/lib/mysql

报错信息:
```
2019-08-30T11:18:22.976635Z 0 [Warning] Can‘t create test file /mydata/mysql/localhost.lower-test
2019-08-30T11:18:22.976687Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.27) starting as process 2788 ...
2019-08-30T11:18:22.980289Z 0 [Warning] Can‘t create test file /mydata/mysql/localhost.lower-test
2019-08-30T11:18:22.980338Z 0 [Warning] Can‘t create test file /mydata/mysql/localhost.lower-test
解决办法:setenforce 0  #关闭防火墙

```

重置密码

```
默认密码:
grep ‘pass‘ /var/log/mysqld.log
mysql_secure_installation
输入root密码
是否要修改密码
是否要修改root密码(大小写、数字、特殊字符)
是否要删除匿名用户
是否禁止root远程登录
是否要删除test数据库
是否要刷新表的权限
```

密码校验规则

```
设置密码的校验规则
mysql> set global validate_password_policy=0;
0 校验级别最低,只校验密码的长度,长度可以设定
1 必须包括大写字母、小写字母、数字、特殊字符
2 必须满足上面两条,并追加,对于密码中任意连续的4个(或者4个以上) 字符不能是字典中的单词
mysql> set global validate_password_length=3; 修改密码的最短长度
```

创建用户

```
create user ‘username‘@‘ip‘ identified by ‘password‘;
全部ip的话则是%
```

查看权限

```
show grants;
```

用户授权

```
mysql> grant all on *.* to ‘root‘@‘%‘ identified by ‘1234‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
```

 

mysql主从

主服务器上配置如下(/etc/my.cnf)

```
server-id=1 设置id
log-bin=/mydata/log/master-bin 启动binlog日志
sync_binlog = 1 确保主从复制事务安全
```

主服务器执行如下sql

```
mysql> grant replication slave on *.* to ‘slave‘@‘192.168.21.131‘ identified by ‘1234‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
show master status\G
```

从服务配置如下:

```
server-id =12
relay_log =/mydata/log/slave-log
sync_binlog = 1
read-only=ON
```

从服务器执行如下sql

```
CHANGE MASTER TO
    MASTER_HOST=‘master2.example.com‘,
    MASTER_USER=‘replication‘,
    MASTER_PASSWORD=‘password‘,
    MASTER_PORT=3306,
    MASTER_LOG_FILE=‘master2-bin.001‘,
    MASTER_LOG_POS=4,
    MASTER_CONNECT_RETRY=10; #监控主服务器的时间
# 连接主库
change master to master_host=‘192.168.21.128‘,master_user=‘slave‘,master_password=‘1234‘;
# 启动进程
start slave;
# 查看状态
show slave status\G
```

问题:

```
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
解决办法:
rm -rf /mydata/mysql/auto.cnf
systemctl restart mysqld

```

相关推荐