mariadb/mysql主从复制

node1: 172.16.92.1/16 mariadb主服务器
node2: 172.16.92.2/16 mariadb从服务器
以上节点均为CentOS 7.1

配置环境
1. 配置好光盘yum源
2. 关闭selinux和iptables

node1: mariadb主服务器

[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

#######以下的内容为添加########
#二进制变更日志
log-bin=mysql-bin
#二进制日志格式为混合模式
binlog_format=mixed
#为主服务器node1的ID值
server-id = 1
port = 3306
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
innodb_file_per_table = on
skip_name_resolve = on
###############################

###### 以下的内容可选 ########
[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
#############################

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
############### End for my.cnf #################

[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# mysql
MariaDB [(none)]> grant replication client,replication slave on *.* to ‘repluser‘@‘172.16.92.2‘ identified by ‘replpass‘;
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: mysql-bin.000003
        Position: 497
    Binlog_Do_DB:
Binlog_Ignore_DB:

##### 记下mysql-bin.000003 和 497 , 设置从服务器中继日志时有用 ####

node2: mariadb从服务器

[[email protected] ~]# yum -y install mariadb-server
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

########## 添加以下内容 ##########
log-bin=mysql-bin
binlog_format=mixed
server-id = 2
relay-log = relay-bin
log_slave_updates = 1
read_only = on

port = 3306
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
innodb_file_per_table = on
skip_name_resolve = on
###################################

######### 以下内容可选 ############
[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
####################################

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

############# End of my.cnf ###############

[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# mysql
MariaDB [(none)]> show global variables like ‘%read_only%‘;
| read_only     | ON    |

MariaDB [(none)]> show global variables like ‘%read_only%‘\G
*************************** 1. row ***************************
Variable_name: read_only
        Value: ON

MariaDB [(none)]> change master to master_host=‘172.16.92.1‘,master_user=‘repluser‘,master_password=‘replpass‘,master_log_file=‘mysql-bin.000003‘,master_log_pos=497,master_connect_retry=5,master_heartbeat_period=2;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 172.16.92.1
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 5
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 497
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.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: 497
              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

MariaDB [(none)]> start slave;

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.92.1
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 5
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 497
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
    ........ 其余信息略 ........

MariaDB [(none)]> show processlist\G
*************************** 3. row ***************************
      Id: 4
    User: system user
    Host:
      db: NULL
 Command: Connect
    Time: 144
   State: Slave has read all relay log; waiting for the slave I/O thread to update it
    Info: NULL
Progress: 0.000
#说明: 从节点已经接收到所有的中继日志

node1 主节点上可查看到此进程
MariaDB [(none)]> show processlist\G
*************************** 2. row ***************************
      Id: 4
    User: repluser
    Host: 172.16.92.2:56821
      db: NULL
 Command: Binlog Dump
    Time: 212
   State: Master has sent all binlog to slave; waiting for binlog to be updated
    Info: NULL
Progress: 0.000

在主节点上创建数据库测试是否能主从同步
MariaDB [(none)]> create database testdb;

在从节点上可看到testdb数据库, 说明主从同步成功!
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+

再来看一下 从节点 的状态
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.92.1
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 5
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 584        #497->584
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 616            #529->616
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

############# mysql主从复制结束 ##############

复制过滤器
(1) 基于库的白名单的实现
在从节点上设置
MariaDB [(none)]> stop slave;
MariaDB [(none)]> set global replicate_do_db=‘testdb‘;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show global variables like ‘%replicat%‘;
| replicate_do_db                  | testdb    |    #只同步该数据库的数据

在主节点上创建新数据库, 查看是否能同步过来
MariaDB [(none)]> create database mydb;

从节点上并未看到mydb数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+

在主节点的testdb中插入数据, 查看是否能同步过来
MariaDB [(none)]> use testdb;
MariaDB [testdb]> create table t1(id int);
MariaDB [testdb]> desc t1\G
*************************** 1. row ***************************
  Field: id
   Type: int(11)
   Null: YES
    Key:
Default: NULL
  Extra:

从节点上查看一样的数据, 说明从服务器现在只能同步一个数据库
MariaDB [(none)]> use testdb;
MariaDB [testdb]> show tables\G
*************************** 1. row ***************************
Tables_in_testdb: t1

MariaDB [testdb]> desc t1\G
*************************** 1. row ***************************
  Field: id
   Type: int(11)
   Null: YES
    Key:
Default: NULL
  Extra:

时间: 2024-10-17 01:10:28

mariadb/mysql主从复制的相关文章

5分钟了解与部署mariaDB(mysql)主从复制

一  复制概述 mariaDB内建的复制功能是构建大型,高性能应用程序的基础.将mariaDB的数据分布到多个系统上去,这种分布的机制,是通过将mariaDB的某一台主机的数据复制到其它主机(slaves)上,并重新执行一遍来实现的.复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环.这些日志可以记录发送到从服务器的更新.当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置

MySQL 主从复制、主主复制、半同步复制

MySQL 复制 =============================================================================== 概述: =============================================================================== MySQL Replication:   1.主从复制的目的和架构 ★Master/Slave(主/从) Master: write/read Slave

42-4 mysql主从复制

04 mysql主从复制架构及实现 实战:主主复制 [[email protected] ~]# systemctl stop mariadb.service  [[email protected] ~]# systemctl stop mariadb.service [[email protected] ~]# rm -rf /var/lib/mysql/* [[email protected] ~]# rm -rf /var/lib/mysql/* [[email protected] ~]

MySQL主从复制、读写分离、高可用集群搭建

MySQL主从复制.读写分离.高可用集群搭建  一.服务介绍   1.1 Keepalived     Keepalived,见名知意,即保持存活,其目的是解决单点故障,当一台服务器宕机或者故障时自动切换到其他的服务器中.Keepalived是基于VRRP协议实现的.VRRP协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成虚拟设备,可以对外提供虚拟路由器IP(一个或多个),即漂移IP(VIP). 1.2 ProxySQL ProxySQL是一个高性能,高可用性的MySQL

MySQL主从复制(二)

主从架构中:从node是不接受w操作的,否则可能会导致数据不一致. 一.复制架构中应该注意的问题: 1.限制slave为只读模式 可以设置在启动参数中. > show global variables like 'read_only'; 此限制对拥有SUPER权限的用户都无效. 阻止所有用户: mysql> flush tables with read lock; //将阻塞所有w操作. //但是中继日志的重放是可以的,不会被阻塞 2.如何保证主从复制的事务安全? master在执行事务后,应

linux笔记 第四十课 mysql主从复制

1.MYSQL复制的基础概念 2.MYSQL复制的实现 3.MYSQL复制架构及双主模型演示 4.MYSQL复制监控/常见问题及解决方案 5.MariaDB  GTID及多源复制 6.MariaDB  GTID读写分离及mysql-proxy的使用 一.MySQL主从复制的基础知识 二.MySQL主从复制实现(以mariadb 5.5.36为例) 实验环境:主服务器(node1)172.16.100.7 从服务器(node2)172.168.100.8 软件:mariadb-5.5.36-lin

MySQL主从复制原理及配置详细过程以及主从复制集群自动化部署的实现

Technorati 标签: 那你魔鬼 一.复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重新执行一遍来实现的.复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环.这些日志可以记录发送到从服务器的更新.当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读

实现MySQL主从复制、双主模型的简单案例

写在前面:如果此文有幸被某位朋友看见并发现有错的地方,希望批评指正.如有不明白的地方,愿可一起探讨. MySQL复制的基本原理 MySQL复制解决的基本问题 让一台MySQL服务器的数据与其他MySQL服务器的数据保持同步. MySQL复制的工作原理 MySQL复制的工作原理图如下所示(图来自高性能MySQL第3版) MySQL主从复制的基本步骤: 1.启动主库上的二进制文件,并把数据更改记录到二进制日志中: 2.备库将主库上的二进制日志复制到自身的中继日志中: 3.备库读取自身的中继日志中的事

mysql 主从复制配置详解

主从复制模型配置过程: 备注: 主节点IP地址是192.168.1.106   从节点的ip地址是192.168.1.107: 主节点: (1)启动二进制日志: 1-1 编辑配置文件 vim /etc/my.cnf配置如下所示: [mysqld] log-bin=master-bin server-id=1 innodb-file-per-table=ON skip_name_resolve=ON systemctl start mariadb.service  1-2 进入mysql使用 my