CentOS7 部署主从复制MariaDB数据库

1.基础环境配置

|----------------|-----|-----|
| IP地址 | 主机名 | 节点 |
| 192.168.200.10 | db1 | 主节点 |
| 192.168.200.20 | db2 | 从节点 |

(1)创建两台虚拟机,节点为192.168.200.10(20),分别更改主机名,此处以db1为例

bash 复制代码
[root@localhost ~]# hostnamectl set-hostname db1
[root@localhost ~]# bash
[root@db1 ~]#

(2)两台虚拟机配置本地yum源,此处以db1为例

bash 复制代码
[root@db1 ~]# mkdir /opt/centos
[root@db1 ~]# mount /dev/cdrom /opt/centos
mount: /dev/sr0 is write-protected, mounting read-only
[root@db1 ~]# mv /etc/yum.repos.d/* /media/
[root@db1 ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[root@db1 ~]# yum clean all
[root@db1 ~]# yum repolist

(3)两台虚拟机关闭防火墙和selinux,此处以db1为例

bash 复制代码
[root@db1 ~]# systemctl stop firewalld
[root@db1 ~]# systemctl disable firewalld
[root@db1 ~]# setenforce 0

(4)两台虚拟机修改映射文件,此处以db1为例

bash 复制代码
[root@db1 ~]# vi /etc/hosts
192.168.200.10 db1
192.168.200.20 db2
2.初始化MariaDB数据库服务,此处以db1为例

(1)安装MariaDB数据库并启动

bash 复制代码
[root@db1 ~]# yum install -y mariadb mariadb-server
[root@db1 ~]# systemctl start mariadb
[root@db1 ~]# systemctl enable mariadb

(2)初始化MariaDB数据库

bash 复制代码
[root@db1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):	#默认回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y	#确定设置root密码
New password:			#输入数据库root密码000000
Re-enter new password:	#重复输入设置的root密码
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y	#确认删除匿名账户
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n   #允许root远程连接
 ... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y   #删除test数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y   #确认初始化
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

(3)登录MariaDB数据库

bash 复制代码
[root@db1 ~]# mysql -uroot -p000000
MariaDB [(none)]>

(4)使用Ctrl+C组合键退出MariaDB数据库

bash 复制代码
MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@db1 ~]#
3.部署主从复制数据库服务

(1)分别修改db1和db2的/etc/my.cnf数据库配置文件,在mysqld下增加三行配置

bash 复制代码
[root@db1 ~]# vi /etc/my.cnf
[mysqld]
log_bin = mysql-bin			#记录操作日志
binlog_ignore_db = mysql		#不同步MYSQL系统数据库
server_id = 1					#数据库集群中每个节点的id都要不同

[root@db2 ~]# vi /etc/my.cnf
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 2

(2)在db1上重启MariaDB数据库并设置主节点权限

bash 复制代码
[root@db1 ~]# systemctl restart mariadb
[root@db1 ~]# mysql -uroot -p000000
MariaDB [(none)]> grant all privileges on *.* to root@'localhost' identified by '000000'; #授权本地使用root用户登录到数据库
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '000000'; #授权任何客户端机器都可以使用root用户登录到数据库
MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '000000'; #创建user用户供从节点db2连接
MariaDB [(none)]> flush privileges; #刷新权限

(3)在db2上重启MariaDB数据库并设置从节点权限

bash 复制代码
[root@db2 ~]# systemctl restart mariadb
[root@db2 ~]# mysql -uroot -p000000
MariaDB [(none)]> change master to master_host='db1',master_user='user',master_password='000000'; #通过user用户连接主节点db1
MariaDB [(none)]> flush privileges; #刷新权限
MariaDB [(none)]> Ctrl-C -- exit!
[root@db2 ~]# systemctl restart mariadb #再次重启MariaDB数据库
[root@db2 ~]# mysql -uroot -p000000
MariaDB [(none)]> start slave; #开启从节点服务
MariaDB [(none)]> show slave status\G; #查看从节点服务状态

当Slave_IO_Running和Slave_SQL_Running的状态都为YES,则说明主从数据库搭建成功

4.验证主从数据库功能

(1)在主节点db1上创建库test

bash 复制代码
[root@db1 ~]# mysql -uroot -p000000
MariaDB [(none)]> create database test;
MariaDB [(none)]> use test
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
MariaDB [test]> insert into company values(1,"baidu","china");
MariaDB [test]> select * from company;
+----+-------+-------+
| id | name  | addr  |
+----+-------+-------+
|  1 | baidu | china |
+----+-------+-------+

(2)在从节点db2上查看,其会自动同步db1上创建的库

bash 复制代码
[root@db2 ~]# mysql -uroot -p000000
MariaDB [(none)]> use test;
MariaDB [test]> select * from company;
+----+-------+-------+
| id | name  | addr  |
+----+-------+-------+
|  1 | baidu | china |
+----+-------+-------+
相关推荐
KIDULT°3 分钟前
K8s 网络进阶实战:Ingress-Nginx、Calico 与 NetworkPolicy 实验 + 排坑记录
网络·nginx·云原生·kubernetes
syagain_zsx8 分钟前
IT 疑难杂症诊疗室:从现象到根因的排障方法论
1024程序员节
倔强的石头10613 分钟前
【Linux指南】进程通信系列(四):命名管道 FIFO —— 让陌生进程按名字见面
linux·服务器·网络
readhere27 分钟前
从ZYNQ7000开发板 学会启动嵌入式Linux系统
1024程序员节·嵌入式linux系统
updayday85427 分钟前
电脑换了硬盘、加了内存,资产台账却没更新 - Ping64 硬件资产管理
大数据·网络·数据库·安全·智能路由器
Solange__32 分钟前
城市更新方案怎么用AI生成3D街区预演?从底图、体块到公众网页展示
人工智能·游戏·3d·ai·自然语言处理·1024程序员节·关卡设计
深圳老胡43 分钟前
STM32C562RET6 外部中断详解:从 EXTI 原理到 GPIO 外部中断实战
笔记·stm32·单片机·代码规范·1024程序员节
2601_966949658 小时前
如何根据研究的起止时间截取分钟级行情?Python 实战与 QuantDash 数据处理指南
开发语言·python·pandas·量化交易·1024程序员节·股票数据·quantdash
淡海水12 小时前
07-02-源码-.NET Core GC分配路径
算法·unity·c#·.netcore·gc·1024程序员节
在世修行12 小时前
智学文枢:页面切换与跨页数据预填
python·桌面应用·signal·1024程序员节·pyside·跨页跳转·状态预填