Mariadb简单(主从)复制、

实验环境:centos7.3

centos7自带的mariadb,yum安装

[root@centos7 ~]# rpm -qa mariadb
mariadb-5.5.52-1.el7.x86_64
[root@centos7 ~]# uname -r
3.10.0-514.el7.x86_64
ip 地址:

master:192.168.23.148
slave:192.168.23.149

一.主从复制

主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库;主数据库一般是实时的业务数据库,从数据库的作用和使用场合一般有几个:
一是作为后备数据库,主数据库服务器故障后,可切换到从数据库继续工作;
二是可在从数据库作备份、数据统计等工作,这样不影响主数据库的性能;
数据分布:能够将数据分散到多个位置,可以实现异地灾备
负载均衡:读操作,适用于读密集型的应用
备份:备份的时候可以停止从节点进行备份,备份更安全
高可用和故障切换:主节点故障,可以将从节点提升为主节点(需要手动实现,或者脚本监控实现,不如corosync或者heartbeat之类高可用方案)

主从原理图:
这里写图片描述

从库生成两个线程,一个I/O线程,一个SQL线程;
i/o线程去请求主库 的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中;
主库会生成一个 log dump 线程,用来给从库 i/o线程传binlog;
SQL 线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,而最终数据一致;

1.主从节点的配置

1.1 master节点的配置:
(1)启动二进制日志log-bin
(2)以及唯一的 server-id

[root@centos7 ~]# vim /etc/my.cnf.d/server.cnf 

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) 

should see
#
# See the examples of server my.cnf files in 

/usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]
server_id = 1
log_bin = master-bin
skip_name_resolve = ON
当然如果使用绝对路径如:、/mydata/data
必须所属组和所属都必须是mysql,如果不是用chown -R mysql.mysql /mydata/data  修改

完成后重启服务:查看文件

[root@centos7 ~]# systemctl restart mariadb.service 
[root@centos7 ~]# cd /var/lib/mysql/
[root@centos7 mysql]# ls
  master-bin.000001  master-bin.000002 master-bin.index              

创建一个有复制权限的账号:(REPLICATION SLAVE,REPLICATION CLIENT)

MariaDB [mysql]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.%.%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.09 sec)

MariaDB [mysql]> FLUSH PRIVILEGES; #刷新,生效
Query OK, 0 rows affected (0.00 sec)

1.2 从节点的配置:

[root@cento7 ~]# vim /etc/my.cnf.d/server.cnf 

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
server_id= 2
relay_log= relay-log #开启中继日志
read_only= ON #开启只读 关闭从服务器的写入功能

先查看一下主服务器的二进制日志位置,确定从服务器的那个位置复制

MariaDB [mysql]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000002 |      507 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

启动服务:从服务器使用CHANGE MASTER连接主服务器进行复制

MariaDB [mysql]> change master to master_host='192.168.23.148', master_user='repluser', master_password='replpass', master_log_file='master-bin.000002', master_log_pos=507;
Query OK, 0 rows affected (0.05 sec)

查看IO线程与SQL线程

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.23.148
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 507
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000002
             Slave_IO_Running: No  #未启动
            Slave_SQL_Running: No  #未启动
              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: 507
              Relay_Log_Space: 245
              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: NULL
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: 0
1 row in set (0.00 sec)

手动开启slave 并查看状态:

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

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.23.148
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 850
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 873
        Relay_Master_Log_File: master-bin.000002
             Slave_IO_Running: Yes #yes 表示开启
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 


查看中继日志:

[root@cento7 ~]# cd /var/lib/mysql/
[root@cento7 mysql]# ls
aria_log.00000001  binlog.index  ib_logfile0                relay-log.000001 
aria_log_control          ib_logfile1         relay-log.index
binlog.000001      ibdata1       master.info  performance_schema  relay-log.info

验证主从复制:
在主节点上创建一个数据库;

MariaDB [mysql]> create database tb1;
Query OK, 1 row affected (0.03 sec)

MariaDB [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| han                |
| mydb               |
| mysql              |
| performance_schema |
| tb1                |
| test               |
| wordpress          |
+--------------------+

从服务器上查看:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| blog               |
| mysql              |
| performance_schema |
| tb1                |
| test               |
+--------------------+

在查看一下从服务器的状态日志的记录已经发生了改变

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.23.148
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 850 
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 873
        Relay_Master_Log_File: master-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值