美文网首页
主从复制

主从复制

作者: 尘曦的雨 | 来源:发表于2017-09-14 11:28 被阅读31次

主节点的设定

[root@centos7 /]# cat /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   #设定serverID的值
log-bin = mysql.log  #启用二进制日志并以文件
skip_name_resolve = NO #关闭名称解析功能
# this is only for embedded server
[embedded]

# This group is only read by MariaDB-5.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mysqld-5.5]

# These two groups are only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

[mariadb-5.5]

启动主节点
[root@centos7 /]# systemctl restart mariadb
备节点的设置

[root@localhost ~]# cat /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]
server-id=2    #定义服务器ID
relay-log=chenxi.log  #启用中继日志
read_only=ON   #关闭名称解析功能
# this is only for the mysqld standalone daemon
[mysqld]

# this is only for embedded server
[embedded]

# This group is only read by MariaDB-5.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mysqld-5.5]

# These two groups are only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

[mariadb-5.5]

启用备节点
[root@localhost ~]# systemctl restart mariadb
启用主节点后连入

[root@centos7 /]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show master status;  查看二进制日志
+--------------+----------+--------------+------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000002 |      245 |              |                  |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'172.16.%.%' IDENTIFIED BY 'replpass'; 创建用户


MariaDB [(none)]> flush privileges;  刷新授权表
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show master status; 查看二进制日志
+--------------+----------+--------------+------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000002 |      496 |              |                  |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> show binlog events in 'mysql.000002';  查看
+--------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------------------+
| Log_name     | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                                  |
+--------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------------------+
| mysql.000002 |   4 | Format_desc |         1 |         245 | Server ver: 5.5.52-MariaDB, Binlog ver: 4                                                             |
| mysql.000002 | 245 | Query       |         1 |         421 | GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'172.16.0.%' IDENTIFIED BY 'replpass' |
| mysql.000002 | 421 | Query       |         1 |         496 | flush privileges                                                                                      |
+--------------+-----+-------------+-----------+-------------+-------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

主节点准备好了

备节点准备

[root@localhost ~]# mysql  连接服务器
\Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> change master to master_host='172.16.251.203' ,master_user='repluser',master_password='replpass',master_log_file='mysql.000002',master_log_pos=421;
Query OK, 0 rows affected (0.09 sec)   
定义完成后数据文件目录会成几个文件
[root@localhost ~]# ls /var/lib/mysql/
aria_log.00000001  chenxi.000001  ibdata1      ib_logfile1  mysql       performance_schema  test
aria_log_control   chenxi.index   ib_logfile0  master.info 被节点  mysql.sock  relay-log.info备节点


MariaDB [(none)]> show slave status\G 查看从服务器的状态
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.16.251.203
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql.000002
          Read_Master_Log_Pos: 421
               Relay_Log_File: chenxi.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql.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: 421
              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)
MariaDB [(none)]> start slave;  打开IO线程与sql线程sql负责重放事件IO线程负责把从主节点读取事件
Query OK, 0 rows affected (0.09 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.251.203
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql.000003
          Read_Master_Log_Pos: 421
               Relay_Log_File: chenxi.000005
                Relay_Log_Pos: 701
        Relay_Master_Log_File: mysql.000003
             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: 421
              Relay_Log_Space: 1341
              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)
备节点配置成功

主节点创建库测试

MariaDB [(none)]> create database mysdb;
Query OK, 1 row affected (0.04 sec)

MariaDB [(none)]> show master status; 检查日志的信息
+--------------+----------+--------------+------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------+----------+--------------+------------------+
| mysql.000003 |      506 |              |                  |
+--------------+----------+--------------+------------------+
1 row in set (0.00 sec)
从节点查看信息
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.251.203
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql.000003
          Read_Master_Log_Pos: 506 变化
               Relay_Log_File: chenxi.000005
                Relay_Log_Pos: 786
        Relay_Master_Log_File: mysql.000003
             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: 506 从节点的变化
              Relay_Log_Space: 1426
              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)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysdb              |   从节点复制成功
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.08 sec)

相关文章

  • mysql 主从复制

    mysql 主从复制 网易数据库 石勇 提纲 什么是主从复制 主从复制的原理 主从复制的用途 主从复制的搭建 主从...

  • MySQL-主从复制&读写分离

    零、本文纲要 一、MySQL主从复制 主从复制 主从复制过程 配置主从复制 二、MySQL读写分离 读写分离 Sh...

  • DBA(MySQL)学习-主从复制基础

    1. 主从复制 1.1 介绍 1.2 主从复制的前提(如何搭建主从复制) 2. 搭建主从复制 2.1 搭建多实例 ...

  • MySQL如何配置主从复制,如何修复主从复制出现的异常?

    MySQL如何配置主从复制,如何修复主从复制出现的异常? 一、什么是Mysql主从复制 MySQL主从复制是其最重...

  • Redis的高可用

    Redis主从复制 什么是主从复制 Redis有三种集群方案,主从复制,哨兵,cluster集群,主从复制是指将一...

  • Day011-MySQL主从复制

    1. 主从复制介绍 2. 主从复制的前提(搭建主从复制) 3. 搭建主从复制步骤 3.1 准备多实例 3.2 检查...

  • MySQL主从复制(二)

    主从复制原理 主从复制的前提 主从复制涉及到的文件和线程 主库: 从库: 原理 主从复制实践(生产实践) 主库有数...

  • 八,主从复制

    1,主从复制简介 2,高可用架构方案 3,主从复制前提 4,主从复制搭建 准备多实例 检查配置文件 创建主从复制用...

  • 6. MySQL 主从架构

    7 MySQL 集群Cluster 7.1 主从复制 7.1.1 主从复制原理 主从复制过程: 主节点收到用户请求...

  • 主从复制 & MHA

    一,mysql主从复制 (1)场景一(主从复制 _ 全新环境下) (2)场景二(主从复制 _ mysql已经使用一...

网友评论

      本文标题:主从复制

      本文链接:https://www.haomeiwen.com/subject/abirsxtx.html