mysql-5.7的主从配置

Posted 换你一世迷离

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql-5.7的主从配置相关的知识,希望对你有一定的参考价值。

mysql的主从配置

下载最新mysql 的yum源

1、wget https://dev.mysql.com/get/mysql57-community-release-el6-11.noarch.rpm

安装最新mysql

rpm -ivh mysql57-community-release-el6-11.noarch.rpm 
yum -y install mysql-server  

启动mysql数据库

service mysqld start

提示:由于5.7初始化会自动生成密码  :

cat /var/log/mysqld.log

2018-03-24T13:19:50.925206Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp 
server option (see documentation for more details). 2018-03-24T13:19:54.106661Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-03-24T13:19:54.588874Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-03-24T13:19:54.805786Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been
started. Generating a new UUID: 0abbeb4b-2f66-11e8-81f3-000c292c7cea. 2018-03-24T13:19:54.810834Z 0 [Warning] Gtid table is not ready to be used. Table mysql.gtid_executed cannot be opened. 2018-03-24T13:19:54.841042Z 1 [Note] A temporary password is generated for [email protected]: :kufY//k0zk& (这是随机生成的密码)

 登录mysql数据库并修改密码:

mysql    -uroot  -p
ALTER USER root@localhost IDENTIFIED BY [email protected]; 

1.配置master的配置文件my.cnf

#打开日志(主机需要打开)
log-bin=mysql-bin
##服务器id(这个是唯一的)
server-id=1
#给从机同步的库(可以写多个库)
binlog-do-db=mydb
binlog-do-db=mydb2
binlog-do-db=test

#自动清理5天前的log文件
expire_logs_days=5

2.修改从服务器的从数据库slave   /etc/my.cnf配置

#服务器id
server-id=2
##要从主机同步的库
replicate-do-db=mydb
replicate-do-db=mydb2
replicate-do-db=test

3.修改之后,重启MySQL主数据库和MySQL从数据库的服务

service mysqld restart

4.配置主服务器的主数据库

主数据库授权同步账户

mysql> GRANT REPLICATION SLAVE ON *.* TO root@192.168.10.115 IDENTIFIED BY [email protected];
Query OK, 0 rows affected, 1 warning (0.01 sec)

刷新权限

FLUSH PRIVILEGES;

查看主服务状态

mysql> show master status ;
+------------------+----------+-----------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB    | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+-----------------+------------------+-------------------+
| mysql-bin.000001 |      602 | mydb,mydb2,test |                  |                   |
+------------------+----------+-----------------+------------------+-------------------+
1 row in set (0.00 sec)

5.配置从服务器的从数据库

mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.10.116‘,    -> MASTER_USER=‘root‘,    -> MASTER_PASSWORD=‘[email protected]‘,    -> MASTER_LOG_FILE=‘mysql-bin.000001‘,\ (这个都是根据主服务器查询出的结果)
    -> MASTER_LOG_POS=602;(就是主服务器 show master status;)
Query OK, 0 rows affected, 2 warnings (0.13 sec)

开启SLAVE同步

start slave; 

查看下slave状态

show slave status \G;
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.116
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 602
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes (这个位置显示yes)
            Slave_SQL_Running: Yes (这个位置也显示yes表示主从复制配置成功)
              Replicate_Do_DB: mydb,mydb2,test
          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: 602
              Relay_Log_Space: 531
              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: 1
                  Master_UUID: 0abbeb4b-2f66-11e8-81f3-000c292c7cea
             Master_Info_File: /var/lib/mysql/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: 
1 row in set (0.00 sec)

当Slave_IO_Running和Slave_SQL_Running都为Yes,才说明主从复制成功

6.停止SLAVE同步

stop slave;

7.撤销已经赋予给MySQL同步账户的权限

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

GRANT REPLICATION SLAVE ON *.* TO ‘root‘@‘192.168.10.116‘ IDENTIFIED BY ‘XXXXXX‘;

REVOKE REPLICATION SLAVE ON *.* FROM ‘root‘@‘192.168.10.116‘;

8.授权账号可以远程登录

GRANT ALL ON *.* TO  用户名@‘%‘ IDENTIFIED BY ‘密码‘ WITH GRANT OPTION; 

 



以上是关于mysql-5.7的主从配置的主要内容,如果未能解决你的问题,请参考以下文章

mysql 5.7主从配置

mysql 5.7主从安装和配置

mysql 5.7 centos 7 主从安装配置详解 .md

centos 7 配置 mysql 5.7 主从复制

CentOS 7.2 MySQL 5.7 主从配置

Mysql 5.7主从