例题:搭建MariaDB数据库服务器,并实现主主复制。
1.在二台服务器中分别MariaDB安装。
2.在二台服务器中分别配置my.cnf文件,开启log_bin。
3.在二台服务器中分别创建专用于数据库同步的用户replication_user,并授权SLAVE。)
4.同步配置,并启用SLAVE复制
5.完成并测试,要求创建“自已姓名的完整汉语拼音”的数据库。
注意:本文中省略了虚拟机的创建和克隆,同时运用了外接软件finalshell来进行远程操作,可通过点击链接来进行finalshell远程连接。
希望能对各位it人士有所帮助,话不多说,我们直接进入主题!!!
一、主服务器(第一台虚拟机)
a.安装mariadb-server
yum install mariadb-server -y
b.安装mariadb
yum install -y mariadb
c.启动mariadb服务并设置为开机自启动
systemctl start mariadb
systemctl enable mariadb
d.进行mariadb数据库登录,并进行密码设置
mysql
set password=password('1234')
e.启动mariadb并设置为开机自启动
systemctl start mariadb
sysetmctl enable mariadb
f.利用密码进行再次登录,并展示所有数据库
mysql -u root -p
1234
show databases;
g.退出数据库,再对my.cnf文件进行修改
eixt
vim /etc/my.cnf
binlog-do-db =pengyuyi
[mariadb]
log-bin
server-id = 1
log-basename = master1
binlog-format = mixed
h.对防火墙进行关闭,并重启mariadb
systemctl stop firewalld
systemctl disable firewalld
systemctl restart mariadb
i.登录数据库,进行一系列相关操作
mysql
show variables like %log_bin%;
如果出现两个value都为on的话,则运行成功
create user 'replication_user'@'%' identified by '1234';
grant replication slave on *.* to 'replication_user'@'%';
show master status;
stop slave;
展示出来的表格为下方这样即可
二、从虚拟机(第二台虚拟机)
操作基本上和上方一致
yum install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
mysql
set password=password('1234');
exit
mysql -u root -p
1234
vim /etc/my.cnf
binlog-do-db = pengyuyi
[mariadb]
log-bin
server-id = 2
log-basename = master2
binlog-format = mixed
systemctl stop firewalld
systemctl disabel firewalld
systemctl restart mariadb
show variables like '%log_bin';
create user 'replication_user'@'%' identified by '1234';
grant replication slave on *.* to 'replication_user'@'%';
show master status;
stop slave;
三、切换回主虚拟机(第一台)
change master to
master_host = '192.168.85.145',
master_user='replication_user',
master_password='1234',
master_port=3306,
master_log_file='master2-bin.000001',
master_log_pos=490,
master_connect_retry=10;
需要根据从虚拟机提供的表格内容进行相对应的更改
start slave;
show slave status \G
出现下面内容中有两个YES,则配置成功
四、切换回从虚拟机(第二台虚拟机)
change master to
master_host = '192.168.85.149',
master_user='replication_user',
master_password='1234',
master_port=3306,
master_log_file='master1-bin.000002',
master_log_pos=245,
master_connect_retry=10;
注意:如果出现下面这个问题
stop slave;
reset slave;
change master to
master_host = '192.168.85.149',
master_user='replication_user',
master_password='1234',
master_port=3306,
master_log_file='master1-bin.000002',
master_log_pos=245,
master_connect_retry=10;
show slave status \G
create database pengyuyi;
show databases;
五、最后再换回主服务器来进行验证
show databases;
如果有出现pengyuyi这个数据库,则证明MariaBD数据库服务器搭建成功!!!
好了,文章到此就告一段落了,如果本篇博客对您有一定的帮助,麻烦大家记得留言+点赞+收藏喔。创作不易,转载请联系作者!