Administrator
Published on 2023-02-13 / 32 Visits
0
0

mysql主从搭建

mysql安装步骤

1.自己找个位置下载安装包
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar

#下面两个为补丁包,根据安装情况决定
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-client-8.0.30-1.el7.x86_64.rpm
https://downloads.mysql.com/archives/get/p/23/file/mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm

2.解压
tar -xf mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar 

3.卸载Linux自带的mariadb,避免碰上依赖关系等问题错误
yum remove mysql-libs -y

3.安装rpm包,请按照顺序安装下,如下#号的是补丁包
rpm -ivh mysql-community-common-8.0.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.30-1.el7.x86_64.rpm  --nodeps --force
# rpm -Uvh mysql-community-client-plugins-8.0.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.30-1.el7.x86_64.rpm
# rpm -Uvh mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.30-1.el7.x86_64.rpm

#可参考如下链接去自己下载补丁包,本次安装的补丁包是我个人缺失的,每个人可能不固定
https://blog.csdn.net/jake_xiao/article/details/127975371

4.更换mysql数据存储目录(可选)
mkdir /data/mysqldata/ -p 
touch /data/mysqldata/mysql.sock
#修改 /etc/my.cnf ,注意在[mysqld]配置下
datadir=/data/mysqldata
socket=/data/mysqldata/mysql.sock
#并新增如下
plugin-load-add=validate_password.so
validate-password=FORCE_PLUS_PERMANENT

chown mysql -R /data/mysqldata
systemctl restart mysqld #重启
systemctl enable mysqld  #设置开启自启,可不操作

5.查看mysql生成的默认密码
grep "password" /var/log/mysqld.log 

6.登入操作
mysql -h 127.0.0.1 -u root -p

#修改密码,注意自己修改成自己的密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '*********';	
#增加root用户远程连接
update user set host = '%'  where user = 'root';	

主节点添加主从同步账户

1.在主节点进入mysql命令端操作
mysql> 	CREATE USER 'replicate_user'@'%' IDENTIFIED BY '*********';				#创建用户,实际密码替换*
mysql>	alter user 'replicate_user'@'%' identified with mysql_native_password by '*********'; #修改密码
mysql>	grant all privileges on *.* to 'replicate_user'@'%' with grant option;	#修改权限
mysql>	flush privileges;	#刷新权限

2.在主从节点添加配置文件,注意如下有做区分 /etc/my.cnf
#主节点
log-bin=mysql-bin
server-id=xxx
#从节点
server-id=xxx
#注解:只有主节点需要配置log-bin;主从节点配置的server-id必须配置且数值不同;

#重启mysql
systemctl restart mysqld

#重置主节点MySQL偏移量
mysql> reset master;				#重置偏移量,如果不重置,从节点也会创建replicate_user同步用户
mysql> show master status;			#查看master信息
#运行下方命令会得到bin-log文件还有Position ,等下到salve节点会用得到记得保存一下

image.png

注册从节点

mysql> 	stop slave;
mysql>	reset slave;
mysql>	change master to master_host='192.168.2.21',master_user='replicate_user',master_port=3306,master_password='********',master_log_file='mysql-bin.000004',master_log_pos=143934320;
#参数解释:
master_host : 主节点IP
master_user : 主从同步账户
master_port : 主节点mysql服务的端口号
master_password : 主从同步账户的密码
master_log_file : master节点获取的二进制文件名字
master_log_pos : master节点获取的Position值

#启动从节点slave
mysql> 	start slave;
mysql>	show slave status \G

#检查如下状态是yes则部署完成

image.png

配置主从(可选)

在从节点运行如下sql更改权限
mysql> 	set global read_only=1;						#read_only=1,普通用户只读,超级用户可读写
或者
mysql> 	set global super_read_only=1;				#super_read_only=1,超级用户只读
mysql>	show global variables like '%read_only%';	#查询只读状态

Comment