Linux7/Centos7 Mariadb主从配置过程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux7/Centos7 Mariadb主从配置过程相关的知识,希望对你有一定的参考价值。
环境: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源:
配置本地yum:cd /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
设置开机不启用防火墙:systemctl disable 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 updated successfully!
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 restart mariadb
在主节点上建立账户并且授权Slave
登录MariaDB数据库:mysql -uroot –proot
建立主从复制用户并授权:
语法:
GRANT REPLICATION SLAVE ON .{所有权限} TO‘slave‘@‘%‘{用户名为slave,%为任意地址} identified by ‘slave‘;
命令:
GRANT REPLICATION SLAVE ON . TO‘slave‘@‘%‘ identified by ‘slave‘;
查询SQL(Master的状态)命令: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 statusG;
授权远程用户root登录:(主从都需要进行执行)
GRANT ALL PRIVILEGES ON . TO ‘root‘@‘%‘ IDENTIFIED BY ‘root‘ WITHGRANT OPTION;
FLUSH PRIVILEGES;
如果对此有兴趣,请扫下面二维码免费获取更多详情
以上是关于Linux7/Centos7 Mariadb主从配置过程的主要内容,如果未能解决你的问题,请参考以下文章