MariaDB设置主从复制

主从复制包含两个步骤: 在 master 主服务器(组)上的设置,以及在 slave 从属服务器(组)上的设置.

配置主服务器 master

1、如果没有启用,则需要开启二进制日志.

给 master 设置唯一的  server_id ,所有的 slave 从属服务器也要设置  server_id; server_id值可以是整数型的数字(1 ~ 2^31-1), 在同一个复制组(replicating group)中的每台服务器的server_id都必须是唯一的.

[mysqld]

server-id=1

log-bin=mysql-bin

binlog_format=mixed

2、创建一个复制账号,并授予replication slave权限。slave 从属服务器需要有连接并从master复制的权限. 通常是为每一台slave 创建一个单独的用户(user),并且只授予复制的权限(REPLICATION SLAVE 权限).

示例

GRANT REPLICATION SLAVE ON *.* TO ‘replication_user‘@‘slave_host‘ IDENTIFIED BY ‘bigs3cret‘;

FLUSH PRIVILEGES;

MariaDB [(none)]> grant replication slave on *.* to ‘repl_user‘@‘192.168.1.53‘ identified by ‘pancou‘;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

需要注意,有一些系统配置选项可能会影响主从复制,查看下面的变量以避免发生问题:

skip-networking,如果 "skip-networking=1",则服务器将限制只能由localhost连接,阻止其他机器远程连到此服务器上。

bind_address,类似地,如果 服务器只监听 127.0.0.1(localhost)的TCP/IP连接,则远程的 slave也不能连接到此服务器.

3、在主库上,设置读锁定有效,这个操作是为了确保没有数据库操作,以便获得一个一致性的快照:

MariaDB [(none)]> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

4、然后得到主库上当前二进制文件名和偏移量值。这个操作的目的是在数据库启动以后,从这个点开始进行数据恢复。

MariaDB [(none)]> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000002 |      509 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

5、现在主数据已经停止了更新操作,需要进行主数据库备份。 如果主数据库可以停止那么直接复制数据文件应该是最快的方法。

[[email protected] ~]# mysqldump -uroot -p --quick --all-databases --lock-all-tables  --master-data=2 > /opt/data-all.sql

Enter password:

[[email protected] ~]# ll -h /opt/data-all.sql

-rw-r--r-- 1 root root 3.7M May  2 15:08 /opt/data-all.sql

6、主库备份完毕以后,可以恢复写操作,剩下的操作只需要在从库上操作。

MariaDB [(none)]> unlock tables;

Query OK, 0 rows affected (0.00 sec)

7、将主数据库的一致性备份数据传送到从库上。

[[email protected] ~]# rsync -avH --progress ‘-e ssh -p 22‘ /opt/data-all.sql [email protected]:/tmp/

The authenticity of host ‘192.168.1.53 (192.168.1.53)‘ can‘t be established.

RSA key fingerprint is 75:b3:14:47:e1:73:10:24:a8:8f:b8:05:29:3e:7d:30.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added ‘192.168.1.53‘ (RSA) to the list of known hosts.

reverse mapping checking getaddrinfo for bogon [192.168.1.53] failed - POSSIBLE BREAK-IN ATTEMPT!

[email protected]‘s password:

sending incremental file list

data-all.sql

3863888 100%   23.88MB/s    0:00:00 (xfer#1, to-check=0/1)

sent 3864436 bytes  received 31 bytes  594533.38 bytes/sec

total size is 3863888  speedup is 1.00

配置从属服务器 slave

1、给 slave 指定唯一的 server_id. 所有服务器,不管是主服务器,还是从服务器,都要设置 server_id. server_id值可以是整数型的数字(1 ~ 2^31-1), 在同一个复制组(replicating group)中的每台(/个)服务器的server_id都必须是唯一的.

要让此配置项生效,需要重新启动服务.

[mysqld]

server-id=2

2、在从库上恢复数据

[[email protected] ~]# mysql </tmp/data-all.sql

3、在从库上使用--skip-salve-start选项启动从服务器,这样不会立即启动从数据库服务器上的复制进程,方便我们对数据库的服务进

行进一步的设置。

[[email protected] ~]# /usr/local/mysql/bin/mysqld_safe --skip-slave-start &

4、对从数据库做相应的设置,指定复制使用的用户,指定复制使用的用户,主数据库服务器的IP、端口以及开始执行复制的日志文件和

位置等。

CHANGE MASTER TO

MASTER_HOST=‘master.domain.com‘,

MASTER_USER=‘replication_user‘,

MASTER_PASSWORD=‘bigs3cret‘,

MASTER_PORT=3306,

MASTER_LOG_FILE=‘mariadb-bin.000096‘,

MASTER_LOG_POS=568,

MASTER_CONNECT_RETRY=10;

MariaDB [(none)]> change master to master_host=‘192.168.1.78‘,master_user=‘repl_user‘,master_password=‘pancou‘,master_log_file=‘mysql-bin.000002‘,master_log_pos=509;

Query OK, 0 rows affected (0.79 sec)

5、在从库上,启动slave进程

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.00 sec)

6、这时在salve上,执行show processlist命令将显示类似如下进程:

MariaDB [(none)]> show processlist\G

*************************** 1. row ***************************

Id: 5

User: root

Host: localhost

db: NULL

Command: Query

Time: 0

State: init

Info: show processlist

Progress: 0.000

*************************** 2. row ***************************

Id: 8

User: system user

Host:

db: NULL

Command: Connect

Time: 10

State: Waiting for master to send event

Info: NULL

Progress: 0.000

*************************** 3. row ***************************

Id: 9

User: system user

Host:

db: NULL

Command: Connect

Time: 4

State: Slave has read all relay log; waiting for the slave I/O thread to update it

Info: NULL

Progress: 0.000

3 rows in set (0.00 sec)

这表明slave已经连接上master了,并开始接收日志。

主库上的进程:

MariaDB [(none)]> show processlist\G

*************************** 1. row ***************************

Id: 7

User: root

Host: localhost

db: NULL

Command: Query

Time: 0

State: init

Info: show processlist

Progress: 0.000

*************************** 2. row ***************************

Id: 9

User: repl_user

Host: 192.168.1.53:57532

db: NULL

Command: Binlog Dump

Time: 183

State: Master has sent all binlog to slave; waiting for binlog to be updated

Info: NULL

Progress: 0.000

2 rows in set (0.00 sec)

7、查看从库复制状态

MariaDB [(none)]> show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.78

Master_User: repl_user

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000003

Read_Master_Log_Pos: 479

Relay_Log_File: localhost-relay-bin.000004

Relay_Log_Pos: 767

Relay_Master_Log_File: mysql-bin.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: 479

Relay_Log_Space: 1112

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

Master_SSL_Crl:

Master_SSL_Crlpath:

Using_Gtid: No

Gtid_IO_Pos:

Replicate_Do_Domain_Ids:

Replicate_Ignore_Domain_Ids:

Parallel_Mode: conservative

1 row in set (0.00 sec)

验证复制服务的正确性,在主数据库上执行一个更新操作,观察是否在从库上的是否同步。

MariaDB [(none)]> create database pancou;

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use database pancou;

ERROR 1049 (42000): Unknown database ‘database‘

MariaDB [(none)]> use pancou;

Database changed

MariaDB [pancou]> create table rpel_table(id int(3));

Query OK, 0 rows affected (0.39 sec)

MariaDB [pancou]> insert rpel_table value(1),(2),(3),(4),(5);

Query OK, 5 rows affected (0.04 sec)

Records: 5  Duplicates: 0  Warnings: 0

MariaDB [pancou]> select * from rpel_table;

+------+

| id   |

+------+

|    1 |

|    2 |

|    3 |

|    4 |

|    5 |

+------+

5 rows in set (0.00 sec)

MariaDB [pancou]> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 |      913 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

查看从库:

MariaDB [(none)]> select * from pancou.rpel_table;

+------+

| id   |

+------+

|    1 |

|    2 |

|    3 |

|    4 |

|    5 |

+------+

5 rows in set (0.00 sec)

MariaDB [(none)]> show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.78

Master_User: repl_user

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000003

Read_Master_Log_Pos: 913

Relay_Log_File: localhost-relay-bin.000004

Relay_Log_Pos: 1201

Relay_Master_Log_File: mysql-bin.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: 913

Relay_Log_Space: 1546

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

Master_SSL_Crl:

Master_SSL_Crlpath:

Using_Gtid: No

Gtid_IO_Pos:

Replicate_Do_Domain_Ids:

Replicate_Ignore_Domain_Ids:

Parallel_Mode: conservative

1 row in set (0.00 sec)

时间: 2024-08-01 22:36:57

MariaDB设置主从复制的相关文章

centos7 mariadb 设置root密码

centos7 mariadb 设置root密码 修改root密码1.以root身份在终端登陆,必须2.输入 mysqladmin -u root -p password root后面的 root 是要设置的密码3.回车后出现 Enter password  输入就密码,如果没有,直接回车 创建用户//创建用户mysql> insert into mysql.user(Host,User,Password) values("localhost","admin"

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

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

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

MySQL/MariaDB数据库主从复制

MySQL数据库复制概述 MySQL的主从复制是指从服务器向主服务器获取二进制日志文件,然后在从服务器上对这些日志重新执行,从而使从服务器和主服务器保持同步.但由于是异步的复制,从服务器在一定程度上落后于主服务器,刚写入到主服务器上的数据可能服务在从服务器上查询得到. MySQL的复制原理: (1)从服务器创建I/O线程连接主数据库,向主数据库请求二进制日志文件. (2)主库上启动Binlog Dump,将二进制日志文件发送给I/O线程,I/O线程获取数据后将数据写在从库的中继日志中(relay

MariaDB数据库主从复制、双主复制、半同步复制、基于SSL的安全复制实现及其功能特性介绍

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

MariaDB数据库主从复制实现步骤

一.MariaDB简介 MariaDB数据库的主从复制方案,是其自带的功能,并且主从复制并不是复制磁盘上的数据库文件,而是通过binlog日志复制到需要同步的从服务器上. MariaDB数据库支持单向.双向.链式级联等不同业务场景的复制.在复制的过程中,一台服务器充当主服务器(Master),接收来自用户的内容更新,而一个或多个其他的服务器充当从服务器(slave),接收来自Master上binlog文件的日志内容,解析出SQL,重新更新到Slave,使得主从服务器数据达到一致. 主从复制的逻辑

mariadb配置主从复制

实验1: 主从复制( 一主 一从 ) 实验前:关闭selinux和防火墙 主服务器:192.168.21.104 yum -y install mariadb-server //安装mariadb [[email protected] ~]#vim /etc/my.cnf [mysqld] log_bin= mysql-bin //启用二进制日志 server_id=1 //为当前节点设置一个全局惟一的ID号 systemctl start mariadb //启动mariadb 创建有复制权限

Redis——安全设置&amp;主从复制

一.    安全设置 我们前面的所有操作都没有进行安全认证,即,只要你连上Redis的server你就可以为所欲为.这样显然是不合理的.下面我们就设置客户端连接server后进行任何操作都需要密码验证. 注意,因为Redis速度相当快,前面我们说过它的set操作每秒钟可达110000(11万)次,get操作每秒钟可达81000次(当然不同的服务器配置性能不同).如果在一台比较好的服务器下,一个外部用户一秒钟就可以进行15K次的密码尝试,这意味着你需要指定非常非常强大的密码来防止暴力破解. 1.1

MySQL设置主从复制

主从复制: 单向,双向,环形,级联,一主多从 双机复制的5种情形1.异步主从(默认常规)2.双写(前段程序对两个数据库同时写,必须两边都落实,程序才返回成功)3.利用外挂软件实现实时主库Binlog日志抓取,从而可以在当机的时候补全从库4.谷歌开发的半同步插件5.DRBD 主从读写分离1.通过程序实现(性能,效率最佳,推荐)php,java等程序可以通过设置多个连接文件轻松实现主从读写分离.2.通过软件实现读写分离MySQL-proxy, Amoeba等代理软件也可以实现读写分离功能,但是最好还