MySQL8.0主从

目录

  • 1. 环境说明
  • 3. 在主数据库里创建一个同步账号授权给从数据库使用
  • 4 配置主数据库
  • 5. 配置从数据库
  • 6. 配置并启动主从复制
  • 7. 测试主从复制
  • 8. 问题集锦
    • 8.1 为什么开启主从复制 Slave_IO_Running不是YES?
    • 8.2 主库授权的用户为什么从库登不上?

1. 环境说明

数据库角色 IP 应用与系统版本
主数据库 192.168.5.55 CentOS7.8
mysql8.0
从数据库 192.168.5.56 CentOS7.8
mysql8.0

3. 在主数据库里创建一个同步账号授权给从数据库使用

//IP填写的是从库IP

mysql> CREATE USER 'bzm'@'192.168.5.56' IDENTIFIED WITH mysql_native_password BY '1';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'bzm'@'192.168.5.56';
Query OK, 0 rows affected (0.00 sec)

mysql> 

4 配置主数据库

[root@localhost ~]# cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
socket=/usr/local/mysql/mysql.sock	
pid-file=/usr/local/mysql/mysql.pid
port=3306
lower_case_table_names=1
log_bin_trust_function_creators=1
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect = 'SET NAMES utf8mb4'
max_connect_errors=1000
max_connections=1510
skip_ssl #会禁用sll,导致客户端报错:Public Key Retrieval is not allowed

skip-name-resolve #可以禁用dns解析,但是,这样不能在mysql的授权表中使用主机名了,只能使用IP。
default-time-zone = '+08:00'
 
log-error=/opt/error_log/mysql.log

server-id=10        //追加以下两行
log-bin=mysql-bin
 
long_query_time=3
slow_query_log=on
slow_query_log_file=/opt/slow_log/slow.log
 
[client]
default-character-set = utf8mb4
socket=/usr/local/mysql/mysql.sock
 
[mysql]
default-character-set = utf8mb4
[root@localhost ~]# 

//重启数据库、查看状态

[root@localhost ~]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL... SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@localhost ~]# mysql -uroot -p1 -e "show master status;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      156 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
[root@localhost ~]# 

5. 配置从数据库

[root@localhost mysql]# cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
socket=/usr/local/mysql/mysql.sock	
pid-file=/usr/local/mysql/mysql.pid
port=3306
lower_case_table_names=1
log_bin_trust_function_creators=1
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect = 'SET NAMES utf8mb4'
max_connect_errors=1000
max_connections=1510
skip_ssl #会禁用sll,导致客户端报错:Public Key Retrieval is not allowed

skip-name-resolve #可以禁用dns解析,但是,这样不能在mysql的授权表中使用主机名了,只能使用IP。
default-time-zone = '+08:00'
 
log-error=/opt/error_log/mysql.log

server-id=20            //追加以下两行
relay-log=mysql-relay
 
long_query_time=3
slow_query_log=on
slow_query_log_file=/opt/slow_log/slow.log
 
[client]
default-character-set = utf8mb4
socket=/usr/local/mysql/mysql.sock
 
[mysql]
default-character-set = utf8mb4

[root@localhost mysql]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@localhost mysql]# 

6. 配置并启动主从复制

//此处的IP为主库
mysql> change master to master_host='192.168.5.55',master_user='bzm',master_password='1',master_log_file='mysql-bin.000001',master_log_pos=156;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql>  show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.55
                  Master_User: bzm
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 679
               Relay_Log_File: mysql-relay.000003
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes     //此两处必须为yes
            Slave_SQL_Running: Yes

7. 测试主从复制

//在主库创建一个bzm数据库
mysql> create database bzm;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| bzm                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

//登录从库观察
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| bzm                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)


8. 问题集锦

8.1 为什么开启主从复制 Slave_IO_Running不是YES?

//在主库上执行刷新日志

mysql> flush logs;
Query OK, 0 rows affected (0.15 sec)

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000002
         Position: 156
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

//停止从库主从复制,并设置change
mysql> stop slave;
mysql> change master to master_log_file='mysql-bin.000002',master_log_pos=156;
Query OK, 0 rows affected (0.00 sec)

//启动从库后,检查主从状态,发现恢复
mysql> start slave;
mysql>  show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.55
                  Master_User: bzm
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 679
               Relay_Log_File: mysql-relay.000003
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

8.2 主库授权的用户为什么从库登不上?

检查主库和从库的防火墙,一定要关闭,或者设置防火墙规则放行

你可能感兴趣的:(MySQL,数据库,mysql,服务器)