Linux7/Centos7 Mariadb主从配置过程

本文介绍如何在无网络环境下于RedHat7系统中部署Mariadb主从服务,包括卸载Mysql防止冲突、使用本地yum源安装Mariadb、配置防火墙、设置数据库权限及主从复制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:RedHat7 同样适用于Centos7

(本文是在无网络环境部署mariadb主从)

卸载Mysql 

(防止mysql和mariadb冲突 )

停止服务:systemctl stop mysqld

查询安装包:rpm -qa | grep mysql

卸载:

rpm -e mysql-server

rpm -e --nodeps mysql-libs

准备环境

 查看磁盘挂载情况:df –h < 如果没有则挂载系统盘:mount/dev/cdrom /media >

PS: 在虚拟机设置里对以下步骤进行操作 如果有网络yum源就不需要挂载系统盘

(如果开机自动挂载到桌面上[带桌面的Linux系统],那么需要卸载,然后再进行重新挂载)

卸载:umonut/dev/cdrom

挂载:mount/dev/cdrom /media

使用本地yum源:

配置本地yumcd /etc/yum.repos.d/

创建一个文件(repo结尾),如:yum.repo,文件内容如下:进行配置:

开始安装

 执行命令:yum -y install mariadb mariadb-server

 拷贝文件:cp /usr/share/mysql/my-huge.cnf  /etc/my.cnf

启动mariadb服务并开机自动运行。命令如下:

systemctl start mariadb

systemctl enable mariadb

查看防火墙状态:systemctl status firewalld

停止防火墙:systemctl stop firewalld

设置开机不启用防火墙:systemctldisable firewalld

开始设置Mariadb数据库, 执行脚本:/usr/bin/mysql_secure_installation

按照下面提示进行操作即可:

NOTE:RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

SERVERS IN PRODUCTIONUSE!  PLEASE READ EACH STEP CAREFULLY!

In order to log intoMariaDB to secure it, we'll need the current

password for the rootuser.  If you've just installed MariaDB,and

you haven't set the rootpassword yet, the password will be blank,

so you should just pressenter here.

Enter current passwordfor root (enter for none): 安装后默认没有root密码,直接回车

OK, successfully usedpassword, moving on...

Setting the rootpassword ensures that nobody can log into the MariaDB

root user without theproper authorisation.

Set root password? [Y/n]Y

New password: 输入root的新密码

Re-enter new password: 新密码确认

Password updatedsuccessfully!

Reloading privilegetables..

 ... Success!

By default, a MariaDBinstallation has an anonymous user, allowing anyone

to log into MariaDBwithout having to have a user account created for

them.  This is intended only for testing, and tomake 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 shouldonly be allowed to connect from 'localhost'. This

ensures that someonecannot guess at the root password from the network.

Disallow root loginremotely? [Y/n] 关闭root远程登录 Y

 ... Success!

By default, MariaDBcomes with a database named 'test' that anyone can

access.  This is also intended only for testing, andshould be removed

before moving into aproduction environment.

Remove test database andaccess to it? [Y/n] 删除test数据库 Y

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!

Reloading the privilegetables will ensure that all changes made so far

will take effectimmediately.

Reload privilege tablesnow? [Y/n] 确定以上所有操作 Y

 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps,your MariaDB

installation should nowbe secure.

Thanks for usingMariaDB!

配置MariaDB主从

 修改vim /etc/my.cnf配置文件:

主节点不需要进行修改

从节点进行修改 server-id=2

PS:进行重启从节点(slave):systemctl restartmariadb

在主节点上建立账户并且授权Slave

登录MariaDB数据库:mysql -uroot –proot

建立主从复制用户并授权:

语法:

GRANT REPLICATION SLAVE ON *.*{所有权限} TO'slave'@'%'{用户名为slave%为任意地址} identified by 'slave';

命令:

GRANT REPLICATION SLAVE ON *.* TO'slave'@'%' identified by 'slave';

 

  查询SQLMaster的状态)命令:SHOW MASTER STATUS;

配置从节点SLAVE:(注意在从节点上执行)

登录从服务器:mysql -uroot –proot进行配置:

语法:

CHANGE MASTER TO

MASTER_HOST='主节点的IP地址', MASTER_USER='主节点授权的用户', MASTER_PASSWORD='主节点授权的用户的密码',MASTER_LOG_FILE='mysql-bin.000007',MASTER_LOG_POS=2197;

命令:

CHANGEMASTER TO MASTER_HOST='192.168.1.31',MASTER_USER='slave',MASTER_PASSWORD='slave',MASTER_LOG_FILE='mysql-bin.000007',MASTER_LOG_POS=2197;

PS:注意语法逗号前后不要用空格。

查看主从状态验证:

命令:show slave status\G;

授权远程用户root登录:(主从都需要进行执行)

 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITHGRANT OPTION; 

FLUSH PRIVILEGES;

 

 

 如果对此有兴趣,请扫下面二维码免费获取更多详情,如果文章对您有帮助,请打赏博主一两毛钱。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值