【Linux】【MySQL】MySQL主从数据库

系统环境:Centos7

主:192.168.8.162

从:192.168.8.127

前提条件

a.关闭防火墙 

  • systemctl stop firewalld

关闭防火墙开机自启

  • systemctl disable firewalld

b.关闭selinux

  • vi /etc/selinux/config

将SELINUX=enforcing改为SELINUX=disabled

设置后需要重启才能生效

 

本文参考:CSDN“”博主,原文链接:https://blog.csdn.net/qq1311256696/article/details/90747435

首先两台数据库安装mysql8.0

1.查看本机是否安装mariadb

  • rpm -qa|grep mariadb 
  • rpm -e --nodeps 文件名 
2.查看是否已经安装了mysql
  • rpm -qa | grep -i mysql
如果存在则通过 rpm -ev 包名 --nodeps 卸载
再执行 rpm -qa | grep -i mysql 看是否卸载完成
查找mysql文件,使用rm -rf mysql文件路径删除
  • find / -name mysql
 
删除配置文档
  • rm -rf /etc/my.cnf

3.安装mysql8.0的yum源

  • yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

执行 yum search mysql  如果出现 mysql-community-server.x86_64 则安装源成功

4.安装mysql8.0

  • yum -y install mysql-community-server.x86_64 

5.启动mysql

  • systemctl start mysqld

开机自启:systemctl enable mysqld

6.查看mysql版本

  • mysql -V

7.设置mysql密码

  • cat /var/log/mysqld.log

 !/dChNphO81E  mysql随机生成的密码

修改密码

  • mysqladmin -u root -p password

9.主库配置

创建一个复制用户,授予用户 slave REPLICATION SLAVE权限和REPLICATION CLIENT权限,用于在主从库之间同步数据。

  • mysql -uroot -p
  • USE mysql;
  • CREATE USER slave IDENTIFIED BY 'Slave@123';
  • GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';

  • FLUSH PRIVILEGES;

vi /etc/my.cnf

添加

server-id=162
log-bin=/var/lib/mysql/mysql-bin

重启数据库

  • systemctl restart mysqld

连接数据库

  • mysql -uroot -p

查看日志情况

  • SHOW MASTER STATUS;

记录下File和Position的值

10.从库配置

  • vi /etc/my.cnf

添加 server-id=127

重启数据库

  • systemctl restart mysqld

连接数据库

  • mysql -uroot -p

停止主从

  • stop slave

从数据连接主数据库:

  • change master to master_host='192.168.8.162', master_user='slave', master_password='Slave@123', master_port=3306, master_log_file='mysql-bin.000002', master_log_pos= 1013, master_connect_retry=30;
  • host、user、password、port为主库的ip、用户名和密码、端口
  • master_log_file为从主库哪个日志读数据即上文中的File

  •  

    master_log_pos为从哪里开始读即上文中的Position

  •  

    master_connect_retry为超时重试时间

开始主从

  • start slave

查看同步状态

  • show slave status \G;

SlaveIORunning 和 SlaveSQLRunning 都是Yes说明主从复制已经开启。

 若 Slave_IO_Running为Connecting可能是mysql8的密码规则问题

主数据库修改slave的密码规则即可:

  • ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'Slave@123';

若Slave_SQL_Running为NO,可能是

1.主从数据不同步,备份主库,同步从库

2.

  • stop slave;
  • set global sql_slave_skip_counter =1 ;
  • start slave;
  • show slave status \G;

 

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_host='192.168.8.162', master_user='slave', master_password='Slave@123', master_port=3306, master_log_file='mysql-bin.000002', master_log_pos= 1013, master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> show slave statu \G;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'statu' at line 1
ERROR: 
No query specified

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.8.162
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1013
               Relay_Log_File: localhost-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1013
              Relay_Log_Space: 155
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2061
                Last_IO_Error: error connecting to master 'slave@192.168.8.162:3306' - retry-time: 30 retries: 5 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 190912 11:02:03
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

ERROR: 
No query specified

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.8.162
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1013
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1013
              Relay_Log_Space: 534
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 162
                  Master_UUID: 0d26dbd8-d501-11e9-9f3d-000c29e24dcf
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

ERROR: 
No query specified

11.测试主从数据库

 主库创建test数据库

  • create database test;

 

 测试完成!

 

如果想只同步某一个数据库

可以在主库的my.cnf

添加

binlog-do-db:binlog日志记录哪个db

或者在从库的my.cnf添加

replicate-do-db:需要复制的数据库名

replicate-ignore-db:不需要复制的数据库名

posted @ 2019-09-11 17:47    阅读(433)  评论(0编辑  收藏  举报