mariadb/mysql数据库主从同步架构搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chao821/article/details/86670527

MySQL数据库支持同步复制、单向、异步复制集群,在集群一个服务器作为主服务,而一个或多个服务器充当从服务器。用户向主服务器写入数据的操作,主服务执行操作写入数据并将操作指令记录于二进制日志文件中。从服务器向主服务器发出请求,获取主服务器的二进制日志的内容,然后把获取的内容纪录到自己的中继日志中并按照中继进行重放操作。

使用主从复制时,所有对表的写入必须在主服务器上进行。在从服务器上面的写入操作是无法同步到其他服务器上面的,为了避免用户将数据写入从服务器,需要在前端进行调度实现读写分离。

一、实验环境

1)两台centos7.2系统的虚拟机,网络互通,且主库所在虚拟机的防火墙关闭(防火墙可能影响从库所在的云主机访问主库数据库)

2)两台虚拟机上安装的数据库版本均一致,本实验为mariadb-server-5.5.56-2。

主库所在的虚拟机ip地址:192.168.2.205

从库所在的虚拟机ip地址:192.168.2.150

二、搭建步骤

1、对于主库所在的虚拟机操作

1)修改mariadb数据库配置,修改配置后需重启服务

[[email protected] ~]#vim /etc/my.cnf.d/server.cnf 

[mysqld]

server_id=1                 #设置当前服务器的ID号
log_bin=log-bin             #启动二进制日志并指定文件名
skip_name_resolve=on        #跳过主机名解析。在CentOS 6自带的mysql后面的=on不用写
innodb_file_per_table=on    #innodb的每个表是用单独的文件

2)创建一个用于从库连接复制主库的用户

[[email protected] ~]#mysql
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave_account'@'192.168.2.205' IDENTIFIED BY '123123';
Query OK, 0 rows affected (0.00 sec)            #创建一个用于从服务器连接复制主服务器的用户

MariaDB [(none)]> FLUSH PRIVILEGES; 
Query OK, 0 rows affected (0.00 sec)

2、对于从库所在的虚拟机操作

1)修改mariadb数据库配置,重启生效。

[[email protected] ~]#vim /etc/my.cnf.d/server.cnf 
[mysqld]
server_id=2
relay_log=relay-log         #启用中继日志。在数据目录下有一个relay-kog.info里面保存了当前的中继日志和位置会主节点二进制文件的名字和位置。
read_only=on                #禁止用户写入数据,这一项的管理员和复制重放无效。

2)添加全局读锁

阻止所有用户:
    mysql> FLUSH TABLES WITH READ LOCK;     添加全局读锁

3)启动服务:

设置主服务器的地址和用于连接主服务器的用户名和密码并指定从服务器的那个二进制文件的那个位置开始复制。

CHANGE MASTER TO MASTER_HOST='192.168.2.205',MASTER_USER='slave_account',MASTER_PASSWORD='123123',MASTER_LOG_FILE='log-bin.000004',MASTER_LOG_POS=2726;

注:获取主库当前所在二进制文件和位置需在主库所在的虚拟机上执行下面这条指令。

MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| log-bin.000004    |      2726|              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

这设置会保存在数据目录下的一个加master.info的文件内

MariaDB [(none)]> START SLAVE;        #启动SLAVE线程
                        #从服务器有两个线程,IO_THREAD是复制线程,SQL_THREAD是重放线程,不指定全部开启。
MariaDB [(none)]> SHOW SLAVE STATUS\G;      #查看从服务器的状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.205
                  Master_User: slave_account
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: log-bin.000004
          Read_Master_Log_Pos: 2726
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 727
        Relay_Master_Log_File: log-bin.000004
             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: 2726
              Relay_Log_Space: 1015
              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
1 row in set (0.00 sec)

注:上图中Slave_IO_Running是复制线程,Slave_SQL_TRunning是重放线程。这两线程须处于RUNNING状态。

三、测试

主库操作:

MariaDB [mydb]> create database mydb;           #在主库上面添加数据
MariaDB [mydb]> use mydb;
MariaDB [mydb]> create table test1 (id int,neme char(100));
MariaDB [mydb]> insert into test1 values (1,'Xiao XXXX');
MariaDB [mydb]> select * from test1;
+------+-----------+
| id   | neme      |
+------+-----------+
|    1 | Xiao XXXX |
+------+-----------+
1 row in set (0.00 sec)

观察从库:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> select * from mydb.test1;
+------+----------+
| id   | neme     |
+------+----------+
|    1 | Xiao XXX |
+------+----------+

测试证明mariadb数据库主从架构搭建成功!

补充说明---遇到的坑及解决办法

1、在从库设置同步的时候,报如下错误:

ERROR 1201 (HY000): Could not initialize master info structure .

出现这个错误的原因是因为从库之前已经做过主从复制,所以需要先停止从库,再进行从库同步设置。

MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec) 
MariaDB [(none)]> reset slave;  
Query OK, 0 rows affected, 1 warning (0.00 sec) 

2、删除mariadb数据库用户

MariaDB [(none)]> SELECT User, Host, Password FROM mysql.user;

MariaDB [(none)]> drop user zhangsan@'%';  

猜你喜欢

转载自blog.csdn.net/chao821/article/details/86670527