2024年11月21日 星期四
通知

Centos7.2.1511安装MySQL5.6.28设置主从复制

0)环境说明

System : Centos 7.2.1511
 MySQL : mysql 5.6.28
Master : 192.168.32.128
 Slave : 192.168.32.129

1)YUM安装MySQL5.6.28

shell > wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
shell > rpm -ivh mysql-community-release-el7-5.noarch.rpm
shell > yum install mysql-community-server
shell > service mysqld restart

2)设置MySQL密码

mysql > mysql -uroot
mysql > set password for ‘root’@‘localhost’ = password('newpassword');
mysql > exit

3)设置主从服务器防火墙

//关闭SELINUX
shell > vi /etc/selinux/config
SELINUX=disabled

//禁止开机启动firewalld
shell > systemctl disable firewalld.service

//设置iptables允许3306端口通过放过防火墙
shell >  vi /etc/sysconfig/iptables 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

重启主从系统

设置Master

4)编辑my.cnf

shell >  vi /etc/my.cnf
server-id = 1
log-bin = mysql-bin
binlog-do-db = data1      //备份data数据库
binlog_ignore_db = mysql  //不备份mysql数据库

5)重启MySQL服务

shell > systemctl restart  mysqld.service

6)进入主MySQL中

shell > mysql -uroot -pnewpassword

7)在主服务器上建立同步账户(tongbu)只允许在从服务器(192.168.32.129)上登录并授权为replication

mysql > create user 'tongbu'@'192.168.32.129' identified by 'tb123456';
mysql > grant replication slave on *.* to 'tongbu'@'192.168.32.129' identified by 'tb123456';

8)锁定数据库表

mysql > flush privileges;

9)导出数据库

  
shell > mysqldump -u root -pnewpasword -all-databases > /root/data1.sql

10)查看Master状态

mysql > show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 120
     Binlog_Do_DB: data1
 Binlog_Ignore_DB: mysql
Executed_Gtid_Set: 
1 row in set (0.00 sec)
这里记住file和Position的值,在从服务器上使用

11)把导出的数据库传到从服务器上

shell > scp /root/data1.sql root@192.168.32.129:/root

到这Master设置完了,开始设置Slave

12)导入数据库

 
shell > mysqldump -u root -pnewpasword data1 > /root/data1.sql

13)编辑my.cnf

shell > vi /etc/my.cnf
server-id = 2
replicate-do-db = data1
replicate-ignore-db = mysql
read_only = 1

14)重启MySQL服务

shell > systemctl restart  mysqld.service

15)停止Slave

mysql > stop slave;

16)从服务器执行同步位置

mysql > change master to 
     -> master_host='192.168.32.128',
     -> master_user='tongbu',
     -> master_password='tb123456',
     -> master_log_file='mysql-bin.000001', 
     -> master_log_pos=120;

17)启动从服务器线程

mysql > start slave;

18)回到主服务器上执行解锁

mysql > unlock tables;

19)返回到从服务器上查看从库状态

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.32.128
                  Master_User: tongbu
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 120
               Relay_Log_File: mysqld-relay-bin.000001
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: data1
          Replicate_Ignore_DB: mysql
           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: 120
              Relay_Log_Space: 457
              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: 3d67d04c-a543-11e5-832d-000c2948b215
             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 the slave I/O thread to update it
           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
1 row in set (0.00 sec)
显示两个YES表示状态正常

至此教程结束,有啥问题可留言

原创文章,转载请注明: 转载自荣耀博客
本文链接地址: Centos7.2.1511安装MySQL5.6.28设置主从复制

关于 荣耀博客

好记性,不如烂笔头。

发表回复