美文网首页
mysql主从复制

mysql主从复制

作者: ALuckyLive | 来源:发表于2017-09-14 16:14 被阅读0次
1、将yum源、firewalld、Selinux、同步时间,相关设置配置完成;
2、安装mysql(mariadb);
3、准备两个虚拟机(note1,note2);
4、配置主从配置文件/etc/my.cnf.d/server.cnf
5、在note1主服务器上创建MYSQL账户;
6、note2从服务器开启复制功能,在note2从服务器上连接至主服务器;
7、在 note1主服务器上创建数据库mydb及表格;
8、查看从服务器是否已同步;
编辑/etc/my.cnf.d/server.cnf
note1;
[mysqld]
Server-id=1
Log-bin=master-log
Skip_name_resolve=ON
note2;
【mysqld】
Server-id=2
Relay-log=relay-log
Skip_name_resolve=ON

主note1 ;创建Mysql账户
[root@note1 mysql]# 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 |
+-------------------+----------+--------------+------------------+
| master-log.000003 |      245 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'reluser'@'172.16.253.%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.00 sec)

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

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


MariaDB [(none)]> SHOW BINLOG EVENTS IN 'master-log.000003';
+-------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------------------------------------------+
| Log_name          | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                                    |
+-------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------------------------------------------+
| master-log.000003 |   4 | Format_desc |         1 |         245 | Server ver: 5.5.52-MariaDB, Binlog ver: 4                                                               |
| master-log.000003 | 245 | Query       |         1 |         423 | GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'reluser'@'172.16.253.%' IDENTIFIED BY 'replpass' |
| master-log.000003 | 423 | Query       |         1 |         498 | FLUSH PRIVILEGES                                                                                        |
+-------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

note2;开启从服务器复制功能;
[root@note2 mysql]# 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)]> help CHANGE MASTER TO
Name: 'CHANGE MASTER TO'
Description:
Syntax:
CHANGE MASTER TO option [, option] ...
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.253.10',MASTER_USER='reluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master_log.000003',MASTER_LOG_POS=496; 
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> SHOW SLAVE STATUS\G ---查看从服务器状态;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.16.253.10
                  Master_User: reluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master_log.000003
          Read_Master_Log_Pos: 496
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master_log.000003
             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: 496
              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
    -> ;         ------------------启动复制;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> SHOW SLAVE STATUS\G ---再一次查看状态;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 172.16.253.10
                  Master_User: reluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master_log.000003
          Read_Master_Log_Pos: 496
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master_log.000003
             Slave_IO_Running: Connecting------
            Slave_SQL_Running: Yes-----已开启
              Replicate_Do_DB: 



[root@note2 ~]# cd /var/lib/mysql/
[root@note2 mysql]# ls
aria_log.00000001  ib_logfile0  mysql               relay-log.000001  test
aria_log_control   ib_logfile1  mysql.sock          relay-log.index
ibdata1            master.info  performance_schema  relay-log.info
[root@note2 mysql]# cat master.info 
18
master-log.000003
496
172.16.253.10
repluser
replpass
3306
60
0





0
1800.000

0
[root@note2 mysql]# cat relay-log.info 
./relay-log.000001
4
master-log.000003
496
note1;主服务器创建数据库;
 MariaDB [(none)]> CREATE DATABASE mydb
    -> ;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use mydb;
Database changed
MariaDB [mydb]> CREATE TABLE tbl1 (id INT,name CHAR(50));
Query OK, 0 rows affected (0.01 sec)
MariaDB [mydb]> INSERT INTO tbl1 VALUES (1,'WU MING');  
Query OK, 1 row affected (0.00 sec)
note2;从服务器查看;
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 172.16.253.10
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-log.000003
          Read_Master_Log_Pos: 496
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-log.000003
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
           
#SHOW TABLES;
#SELECT *FROM tbl1;

相关文章

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

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

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

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

  • Mysql 主从复制

    Mysql 主从复制 MySQL Replication 主从复制(也称 AB 复制)允许将来自一个MySQL数据...

  • 主从复制 & MHA

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

  • mysql主从复制

    构建MySQL主从复制 MySQL的主从复制和mysql的读写分离两者有着紧密联系,数据的读写分离实在主从复制的基...

  • Spring Data JPA 使用主从数据源

    Mysql 配置主从复制 参考:Mysql主从复制-半同步复制[https://www.jianshu.com/p...

  • MySQL集群篇

    1 集群之主从复制 1.1 主从复制概述 MySQL主从复制也可以称为MySQL主从同步,它是构建数据库高可用集群...

  • 深度探索MySQL主从复制原理

    概要 MySQL Replication (MySQL 主从复制) 是什么? 为什么要主从复制以及它的实现原理是什...

  • 【转】MySQL 8.0复制改进

    1 - MySQL主从复制模型 我们从最基本的主从复制模型开始说起,下图是最经典的MySQL主从复制架构图: My...

  • mysql主从代理

    mysql主从复制+读写分离 环境: mysql主:193.168.1.1 mysql从:193.168.1.2 ...

网友评论

      本文标题:mysql主从复制

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