MariaDB主从复制(一主一从)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: MariaDB主从复制(一主一从)

mysql主从

主机清单:image.png

1、基础配置

1、关闭防火墙

#两台都关
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld

2seliunx 关闭

root@localhost ~]# setenforce 0
[root@localhost ~]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are
protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

3、配置yum

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# rm -rf *
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo
https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache

2、配置:

1、主服务器(master

1、安装mysql

#1、安装mysql:
[root@localhost ~]# yum -y install mariadb-server
#2、重启mysql:
[root@localhost ~]# systemctl restart mariadb
#3、修改MySQL密码(000000)
[root@localhost ~]# mysql_secure_installation

2、配置mysql文件

编辑主服务器的 MySQL 配置文件(一般是 my.cnf 或 my.ini),启用二进制日志(binary logging)
功能。
[root@localhost ~]# 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
server-id=1
#这将启用二进制日志,并为主服务器指定一个唯一的 server-id。
#重启从服务器的 MySQL 服务,使配置生效
[root@localhost ~]# systemctl restart mariadb

3、创建复制用户,并授予复制权限

#在主服务器上创建用于复制的用户:
[root@localhost ~]# mysql -uroot -p
Enter password:
#连接到主服务器的 MySQL 命令行界面。
#执行以下 SQL 语句创建复制用户,并授予复制权限:
用户名 从服务器的IP 密码
MariaDB [(none)]> CREATE USER 'rep'@'192.168.8.24' IDENTIFIED BY '000000';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.8.24';
Query OK, 0 rows affected (0.00 sec)

4、获取当前的二进制日志位置

#在主服务器上获取当前的二进制日志位置:
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 488 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
#记下结果中的 File 和 Position 值,这将在后续的步骤中使用.

2、从服务器(slave

1、安装mysql

#1、安装mysql:
[root@localhost ~]# yum -y install mariadb-server
#2、重启mysql:
[root@localhost ~]# systemctl restart mariadb
#3、修改MySQL密码(000000)
[root@localhost ~]# mysql_secure_installation

2、配置mysql文件

编辑从服务器的 MySQL 配置文件,启用中继日志(relay logging)功能
[root@localhost ~]# 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
#添加内容
relay-log=mysql-relay-bin
server-id=2
#5、重启从服务器的 MySQL 服务,使配置生效
[root@localhost ~]# systemctl restart mariadb

3、设置主服务器的信息

设置主服务器的信息
[root@localhost ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> CHANGE MASTER TO
MASTER_HOST='192.168.8.23',
MASTER_USER='rep',
MASTER_PASSWORD='000000',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=488;
# MASTER_HOST为主库master的ip地址,MASTER_USER、MASTER_PASSWORD为1.3当中创建的用户名和
密码,MASTER_LOG_FILE、 MASTER_LOG_POS为步骤1.4当中查询到的两个值

4、启动从服务器的复制进程

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

5、 检查复制状态:


MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.8.23
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 488
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes #状态为yes
Slave_SQL_Running: Yes #状态为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: 488
Relay_Log_Space: 823
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)
#检查输出中的 Slave_IO_Running 和 Slave_SQL_Running 字段,确保两者的值都为 Yes,表示主从
搭建成功。

:3、测试:

#从服务器
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| t1 |
+--------------------+
4 rows in set (0.00 sec)
#主服务器创建一个数据库
MariaDB [(none)]> create database t2;
Query OK, 1 row affected (0.00 sec)
#从服务器查看
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| t1 |
| t2 |
+--------------------+
5 rows in set (0.00 sec)
能看见已经有't2'这个数据库了,就代表成功了
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
9月前
|
关系型数据库 MySQL 网络安全
mariadb主从复制
使用阿里云产品简单实现mariadb主从复制
|
存储 Oracle 关系型数据库
MariaDB主从复制虚拟机实战
MariaDB主从复制虚拟机实战
229 0
|
SQL 关系型数据库 MySQL
MariaDB主从复制搭建
我的github 安装MySQL服务器 安装数据库 yum install -y mariadb-server 初始化数据库 mysql_secure_installation #MySql初始化脚本,以下为每一项的翻译 是否设置root密码 输入密码 确认密码 ...
4236 0
|
关系型数据库 测试技术 数据库
|
监控 关系型数据库 数据库
|
MySQL 关系型数据库 网络安全
|
监控 关系型数据库 MySQL
|
关系型数据库 数据库 安全
|
监控 关系型数据库 网络安全

推荐镜像

更多