环境:Linux7
搭建MariaDB主从,装备两个干净的虚拟机,分别安装(不要安装一个后去克隆,会出现不可控的问题出现)
mysql主从大忌:从节点不允许修改数据(从节点只读不写)
卸载过程:
停止服务:systemctl stop mariadb
查询安装包:rpm -qa | grep mariadb
卸载:(查到什么,就卸载什么)
rpm -e mariadb-server
rpm -e mariadb
rpm -e --nodeps mariadb-libs
一、准备环境
1.1 查看磁盘挂载情况:df -h <如果没有则挂载系统盘:mount/dev/cdrom/media>
PS:在虚拟机设置里,对以下步骤进行操作:安装系统的ISO镜像文件
如果开机自动挂载到桌面上【带桌面的Linux系统】,那么需要卸载,然后再重新进行挂载
卸载:umonut /dev/cdrom
挂载:monut /dev/cdrom /media 【*中间是有空格的*】
1.2 使用本地yum源:
配置本地yum:
#cd /etc/yum.repos.d/
创建一个文件(以repo结尾),如:yum.repo,文件内容如下:进行配置:
#vim yum.repo
:wq 【保存退出】
二、开始安装
2.1 执行命令
#yum -y install mariadb mariadb-server
2.2 拷贝文件
#cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
2.3 设置表名不区分大小写
用root账号登录后,在/etc/my.cnf中的【mysql】后添加lower_case_table_names=1,重启MySQL服务,这时已经设置成功,不区分表名的大小写
2.4 启动MariaDB服务并开机自动运行,命令如下:
#systemctl start mariadb [启动]
#systemctl enable mariadb [开机自动启动]
2.5 防火墙命令
查看防火墙状态:systemctl status firewalld
停止防火墙:systemctl stop firewalld
设置开机不启用防火墙:systemctl disable firewalld
2.6 开始设置mariadb数据库
执行脚本:/usr/bin/mysql_secure_installation
[root@192 etc]# /usr/bin/mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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.
注释:安装后默认没有root密码,直接回车
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.
注释:设置root密码,Y
Set root password? [Y/n] y
注释:输入root新密码
New password:
注释:新密码确认
Re-enter new password:
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.
注释:关闭root远程登录[如果关闭,需要在配置中打开]
Disallow root login remotely? [Y/n] y
... Success!
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.
注释:删除test数据库
Remove test database and access to it? [Y/n] y
- 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!
三、配置MariaDB主从
3.1 修改 vim /etc/my.cnf配置文件
*主节点不需要修改
从节点进行修改server-id=2 [如果有多个从机,只要不是1的正整数,不重复]
PS:从节点修改后,需重启从节点(slave):systemctl restart mariadb
3.2 在主节点上建立账户(slave)并且授权
登录mariadb数据库:mysql -uroot -pxxx [-u用户名 -p密码 写用户名/密码前没有空格 ]
建立主从复制的用户并授权:
语法:
GRANT REPLICATION SLAVE ON *.*{所有权限} TO 'slave'@'%'{用户名为slave,%为任意地址} IDENTIFIED BY 'slave';
命令:GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'slave';
3.3 查询SQL(master的状态),命令:SHOW MASTER STATUS;
3.4 配置从节点SLAVE:(注意在从节点上执行)
登录从服务器:mysql -uroot -pxxx进行配置
语法(注意语法英文逗号(,)并且逗号前后没有空格):
CHANGE MASTER TO MASTER_HOST='主节点的IP地址',MASTER_USER='主节点授权的用户',MASTER_PASSWORD='主节点授权的用户密码',MASTER_LOG_FILE='主节点的File文件名',MASTER_LOG_POS=主机点的Position;
命令:CHANGE MASTER TO MASTER_HOST='192.168.0.10',MASTER_USER='slave',MASTER_PASSWORD='slave',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=1613;
再执行命令:
stop slave; //停止从服务
start slave;//启动从服务
3.5 查看主从状态验证:
命令:show slave status\G;
注:看到这两项都是Yes,说明主从已配置成功
问题:如果这两项显示No,并且保证了之前的配置是正常的,那就重启从节点MariDB(systemctl restart mariadb),再看下
3.6 授权远程用户root登录(主从都需要进行执行)
命令:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;
常见问题:
问题1:如果连接mariadb时,报错:1045-Access denied for user 'root'@'本机IP' (usering password : YES)
解决方案:
1、登录数据:mysql -uroot -pxxx
2、进入mysql库(切库):use mysql;
3、重新设置root用户的密码:update user set password=password("123") where user="root";
4、刷新权限:flush privileges;
5、退出:exit;
6、重启:systemctl restart mariadb
********* 问题解决*********
问题2:如果从节点被修改,造成了主从不能同步时,修复操作:
解决办法1:
Slave_SQL_Running:No,程序可能在从节点进行了写操作,也可能是slave机器重启后,事务回滚造成的。
一般事务回滚造成的:
mysql>stop slave;
mysql>set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql>start slave;
解决办法2:
1、停止slave服务:slave stop;
2、到主服务器查看主机状态:show mastar start;
3、记录File和Position对应的值
4、重复设置上面的从节点
注意:手动同步需要停止master的写操作!!