Mysql主从部署和配置调优

2023年 9月 23日 54.7k 0

一、单节点部署Mysql5.7

1、下载libaio依赖包

wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-devel-0.3.109-13.el7.x86_64.rpm
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-0.3.109-13.el7.x86_64.rpm

2、下载最新5.7二进制安装包

wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

3、创建用户和用户组

groupadd mysql
useradd -M -g mysql -s /sbin/nologin mysql

4、创建数据目录和日志目录

mkdir -p /opt/mysql/data
mkdir -p /opt/mysql/logs/{binlogs,relaylogs}

5、安装依赖包

rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
rpm -ivh libaio-devel-0.3.109-13.el7.x86_64.rpm

6、解压5.7二进制安装包

tar -xzvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
mv ./mysql-5.7.35-linux-glibc2.12-x86_64  /usr/local/mysql5.7

7、创建配置文件

cat   /usr/local/mysql5.7/my.cnf
[mysqld_safe]
log-error=/opt/mysql/logs//mysql-error.log
[mysqld]
default_authentication_plugin=mysql_native_password
default-time-zone='+8:00'
user=mysql
port=3310

# 做主从同步时需要添加如下配置,server-id不能重复。
server-id=$(date +%s)
binlog_format = ROW
binlog_row_image = full
max_binlog_size = 1G
expire_logs_days = 7
log-bin=/opt/mysql/logs/binlogs/mysql-bin
relay-log=/opt/mysql/logs/relaylogs/slave-relay-bin
# end

# 免密登录,用于安装后设置密码时使用
skip-grant-tables
# end

log-error=/opt/mysql/logs/mysql-error.logdatadir=/opt/mysql/data
basedir=/usr/local/mysql5.7
socket=/usr/local/mysql5.7/mysql.sockpid-file=/usr/local/mysql5.7/mysql.pid
skip-name-resolve
symbolic-links = 0
lower_case_table_names = 1
character-set-server = utf8mb4
default-storage-engine = InnoDB
innodb_file_per_table = 1
max_connections = 5000
max_allowed_packet = 1G
interactive_timeout = 120
wait_timeout = 864000
sort_buffer_size=2097152
sql_mode=NO_AUTO_VALUE_ON_ZERO
[mysql]
socket=/usr/local/mysql5.7/mysql.sockdefault-character-set=utf8mb4
EOF

8、授予运行用户目录权限

chown -R mysql:mysql /usr/local/mysql5.7
chown -R mysql:mysql /opt/mysql
chmod -R 770 /usr/local/mysql5.7
chmod -R 770 /opt/mysql

9、初始化mysql数据库

/usr/local/mysql5.7/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7 --datadir=/opt/mysql/data

10、支持SSL安全连接

yum -y install openssl
/usr/local/mysql5.7/bin/mysql_ssl_rsa_setup --user=mysql --datadir=/opt/mysql/data

11、修改启动脚本

cd /usr/local/mysql5.7/support-files
sed   -i   "/^basedir/c\basedir=/usr/local/mysql5.7"   mysql.server
sed   -i   "/^datadir/c\datadir=/opt/mysql/data"   mysql.server

12、设置开机自启

ln -s /usr/local/mysql5.7/support-files/mysql.server  /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
chkconfig --list

13、启动mysql数据库

systemctl restart mysqld && systemctl status mysqld

14、设置root用户密码

/usr/local/mysql5.7/bin/mysql -S /usr/local/mysql5.7/mysql.sock
update mysql.user set authentication_string=password('WuAi@3030') where user='root';
flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'WuAi@3030';exit

15、去除免密登录

sed -i 's/^skip-grant-tables/#&/'  /usr/local/mysql5.7/my.cnf
systemctl restart mysqld

16、设置允许远程连接

/usr/local/mysql5.7/bin/mysql -S /usr/local/mysql5.7/mysql.sock -uroot -p
grant all privileges on *.* to 'root'@'%' identified by 'WuAi@3030' with grant option;
flush privileges;
select host,user from mysql.user;
exit

17、添加环境变量

cat > /etc/profile
export MYSQL_HOME=/usr/local/mysql5.7/mysql
export PATH=\$MYSQL_HOME/bin:\$PATH
EOF
source /etc/profile

18、防火墙放行mysql端口

firewall-cmd --permanent --add-port=3310/tcp
firewall-cmd --reload

二、配置Mysql主从关系

1、设置从库为只读模式

/usr/local/mysql5.7/bin/mysql -S /usr/local/mysql5.7/mysql.sock -uroot -p
set global read_only=off;
show variables like 'read_only';
exit

2、重启从库

systemctl restart mysqld

3、在主库上创建同步账号

CREATE USER 'repl'@'%' IDENTIFIED BY 'iN@EhGm@xk9B';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'
FLUSH PRIVILEGES;

4、在主库上查看binlog文件名和位置

SHOW MASTER STATUS;

比如看到的是:mysql-bin.000002 | 1465

5、在从库上清除主从关系

STOP SLAVE;
RESET SLAVE ALL;

6、在从库上添加主从关系

CHANGE MASTER TO
MASTER_HOST='192.168.1.1',
MASTER_PORT=3310,
MASTER_USER='repl',
MASTER_PASSWORD='iN@EhGm@xk9B',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=1465;

7、在从库上开启主从同步

START SLAVE;

8、在从库上查看同步状态

SHOW SLAVE STATUS\G

如果为双yes表示主从同步状态正常。

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论