MySQL的主从同步

       下面就针对mysql的主从同步做一总结性梳理,有些内容是从官网或者其他人博客里面找来的,有些是根据自己本地测试总结的。如有部分纰漏,烦请告知,同时,也作为学习笔记。

Mysql安装及主从复制部署(Master和salve两台机器上操作),操作环境均为Centos 7

       安装Mysql,安装过程要在两个Mysql节点机器上都要操作。安装过程参考:请戳我~。里面详细介绍mysql安装和权限修改,密码修改。此处就不详细介绍了。接下来开始配置mysql的主从:服务器master为10.200.132.52 服务器slave为10.200.132.45

[root@Mysql-node1 ~]# cp /etc/my.cnf /etc/my.cnf.bak     # 先备份
[root@Mysql-node1 ~]# vim /etc/my.cnf        # 在[mysqld]区域添加下面内容
......
[mysqld]
server-id=1    
# binlog文件名字   
log-bin=mysql-bin 
#需要同步的数据库。如果是多个同步库,就以此格式另写几行即可。如果不指明对某个具体库同步,
#就去掉此行,表示同步所有库(除了ignore忽略的库)
#binlog-do-db=kevin       
#排除哪些数据库表进行同步          
binlog-ignore-db = mysql,information_schema 
sync_binlog = 1     
binlog_checksum = none 
binlog_format = mixed  


重启mysql服务
[root@Mysql-node1 ~]# service mysqld restart;
[root@Mysql-node1 ~]# service mysqld status; #可以查看mysql启动状态
登录mysql,授予slave从机复制权限
在此之前 ,需要先创建用来进行主从的账户,比如slave
mysql> CREATE USER 'slave'@'10.200.132.45' IDENTIFIED BY 'slave';
mysql> grant replication slave,replication client on *.* to slave@'10.200.132.45' identified by "slave";  
#上面配置的ip地址是限定哪一台账户才能访问
mysql> flush privileges;
 
授权之后,要保证10.200.132.45这台slave节点机器能使用上面的权限信息登录到本机的mysql
将数据库锁住,仅仅允许读,以保证数据一致性;
mysql> FLUSH TABLES WITH READ LOCK;                 #注意,锁定后,如果自己同步对方数据,同步前一定要记得先解锁!
mysql> flush privileges;

查看主节点的master复制信息
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000003 |     1302 |              | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+

====================================================
接着是slave从节点操作
[root@Mysql-node2 ~]# cp /etc/my.cnf /etc/my.cnf.bak
[root@Mysql-node2 ~]# vim /etc/my.cnf
.......
[mysqld]
.......
server-id=2  
log-bin=mysql-bin  
#需要同步的数据库名。如果不指明同步哪些库,就去掉这行,表示所有库的同步(除了ignore忽略的库)。 
#replicate-do-db=kevin   
replicate-ignore-db=mysql 
slave-skip-errors = all
 
重启mysql服务
[root@Mysql-node2 ~]# service mysqld restart;
 
登录slave节点的mysql,进行主从同步设置
mysql> stop slave; #先暂时关闭从节点 
mysql> change  master to master_host='10.200.132.51',master_user='slave',master_password='slave',master_log_file='mysql-bin.000003',master_log_pos=1302; 
mysql> start slave;
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.200.132.51
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 1302
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          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: 1349
              Relay_Log_Space: 456
              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: 747977ea-8fba-11e8-86c0-525400b19c93
             Master_Info_File: /data/mysql/data/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)
 
ERROR:
No query specified
 
=========================================================
通过上面的信息,可知主从复制环境已经OK(Slave_IO_Running和Slave_SQL_Running状态均为YES),下面验证下主从复制是否正常?
 
在master主节点上操作
mysql> unlock tables;
mysql> CREATE DATABASE kevin CHARACTER SET utf8 COLLATE utf8_general_ci;      
mysql> use kevin;
mysql> create table if not exists haha (id int(10) PRIMARY KEY AUTO_INCREMENT,name varchar(50) NOT NULL);

在slave从节点上查看(保证从节点上查看slave状态时,Slave_IO_Running和Slave_SQL_Running状态均为YES,这样就能保证主从复制在进行中)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kevin              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
 
mysql> use kevin; 
mysql> show tables;
+-----------------+
| Tables_in_kevin |
+-----------------+
| haha            |
+-----------------+
1 row in set (0.00 sec)
 
接着在Master主节点插入数据
mysql> insert into kevin.haha values(1,"wangshibo"),(2,"linan"),(3,"zhangminmin");      

然后再在slave从节点查看,如下发现已经同步过来了!
mysql> use kevin;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_kevin |
+-----------------+
| haha            |
+-----------------+
1 row in set (0.00 sec)
 
mysql> select * from haha;
+----+-------------+
| id | name        |
+----+-------------+
|  1 | wangshibo   |
|  2 | linan       |
|  3 | zhangminmin |
+----+-------------+
3 rows in set (0.00 sec)
 
由此可见,Mysql的主从复制已经实现!

 

你可能感兴趣的:(数据库)