码迷,mamicode.com
首页 > 数据库 > 详细

mysql-5.7的主从配置

时间:2018-03-24 23:50:21      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:blog   retrieve   oca   top   none   lease   test   first   null   

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 root@localhost: :kufY//k0zk& (这是随机生成的密码)

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

mysql    -uroot  -p
ALTER USER root@localhost IDENTIFIED BY Xu19920615!@; 

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 Xu19920615!@;
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=‘Xu19920615!@‘,    -> 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的主从配置

标签:blog   retrieve   oca   top   none   lease   test   first   null   

原文地址:https://www.cnblogs.com/xu743876685/p/8641772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!