vlambda博客
学习文章列表

Mysql主从服务搭建

1. 下载mysql的linux版本

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




2. CentOS操作系统配置

修改操作系统参数以最大性能发挥操作系统能力,优化mysql运行环境

2.1  sysctl.conf 优化

# /etc/sysctl.conf#以下参数根据需求调整# 端口最大的监听队列长度net.core.somaxconn=65535net.core.netdev_max_backlog=65535net.ipv4.tcp_max_syn_backlog=65535



net.ipv4.tcp_tw_recycle=1net.ipv4.tcp_tw_reuse=1net.ipv4.tcp_fin_timeout=10

#调整tcp连接缓冲区的默认值和最大值net.core.wmem_default=87380net.core.rmem_default=87380net.core.rmem_max=16777216net.core.wmem_max=16777216

#用于减少失效tcp连接占用的资源,加快资源回收的效率net.ipv4.tcp_keepalive_time=120net.ipv4.tcp_keepalive_intvl=30net.ipv4.tcp_keepalive_probes=3

#Linux内核中最重要的参数之一,用于定义单个共享内存段的最大值#注意:#1、这个参数应该设置为足够大,以便能在一个共享内存段下容纳下整个Innodb缓冲池的大小#2、这个值的大小对于64位Linux系统,可取的最大值为物理内存值减 -1 byte,建议值为大于物理内存的一半,一般取值大于Innodb缓冲池的大小即可,可以取物理内存减 -1 bytekernel.shmmax=4294967295

#这个参数当内存不足时,会对性能产生比较明显的影响#设置为0表示:告诉Linux内核除非虚拟内存完全占满了,否则不要使用交换分区swapvm.swappiness=0

2.2  limit.conf参数优化


增加资源限制(`/etc/security/limits.conf`),这个文件实际上是Linux PAM,也就是插入式认证模块的配置文件。打开文件数的限制`vim /etc/security/limits.conf`


#加到/etc/security/limits.conf 文件末尾 * soft nofile 65535 * hard nofile 65535 * soft nproc 65535 * hard nproc 65535


 2.3  磁盘调度策略

查看当前使用的调度策略

磁盘IO调度策略有很多NOOP调度策略 ,Deadline调度策略, anticipatory调度策略, mq-deadline等。Deadline,mq-deadline适合数据库

cat /sys/block/sda/queue/scheduler # vm虚化的系统cat /sys/block/vda/queue/scheduler # kvm虚化的系统


2.4 文件系统XFS适合数据库

[root@zhdc-zht-mysql-master support-files]# fsck -N /datafsck,来自 util-linux 2.23.2[/sbin/fsck.xfs (1) -- /data] fsck.xfs /dev/mapper/datavg-datalv



lsblk 命令显示块设备信息,当使用-f选项时,会打印分区的文件系统类型

`lsblk -f` 适合数据库

NAME FSTYPE LABEL UUID MOUNTPOINTsr0vda├─vda1 xfs 7840c331-cc73-4f1e-869b-7db63a0a6343 /boot└─vda2 LVM2_member SufBpu-svy9-3abr-DGVC-c5Xb-5I1j-pfkH8M ├─centos-root xfs 47351f56-9b32-430f-b27f-ecf55b90dc96 / ├─centos-swap swap 01111d19-4ecc-4c5e-a099-8bffa16b700f └─centos-home xfs 37a2e559-996d-4e7f-a433-dff66dd57aca /homevdb└─vdb1 vfat config-2 07CB-3E11vdc LVM2_member feDD65-vsf0-CQ95-oAfM-p2gr-jmbb-zppuJk└─datavg-datalv xfs 58125523-299a-450d-8c6f-8d8116e0c7c4 /datavdd LVM2_member d0KxOn-6SDT-X7Hz-B5Ng-O3aQ-nXEm-FT9VYw└─centos-root xfs 47351f56-9b32-430f-b27f-ecf55b90dc96 /


2.5 关闭防火墙

systemctl status firewalld.service




3. mysql安装

账户管理

groupadd mysqluseradd -r -g mysql mysqlchown -R mysql.mysql /usr/local/mysql


mysql安装

# 解压tar -xzvf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz /usr/local/# 用户groupadd mysqluseradd -r -g mysql mysqlchown -R mysql.mysql /usr/local/mysql


mysql初始化

su mysql # 切换mysql用户/usr/local/mysql/bin/mysqld --user=mysql --basedir=/data/mysql/ --datadir=/data/mysql/data --initialize



mysql快捷方式

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqlcp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld



配置`/etc/my.cnf`文件

[mysqld]

datadir=/data/mysql/databasedir=/usr/local/mysqlsocket=/tmp/mysql.sockuser=mysqlport=3306character-set-server=utf8max_connections=2000max_connect_errors=10000

sql_mode='NO_AUTO_CREATE_USER'# 取消密码验证# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid

## include all files from the config directory#!includedir /etc/my.cnf.d



启动mysql

chkconfig mysql on



查看是否启动成功

netstat-lntup|grep 3306

[root@mysql init.d]# netstat -lntup|grep 3306tcp6 0 0 :::3306 :::* LISTEN 18030/mysqld


前端启动(可选)

mysqld_safe --defaults-file=/etc/my.cnfservice mysqld start

在Unix和NetWare中推荐使用mysqld_safe来启动mysqld服务器。mysqld_safe增加了一些安全特性,例如当出现错误时重启服务器并向错误日志文件写入运行时间信息。


配置root密码

mysqladmin -S /tmp/mysql.sock password
# 无需密码提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)# 需要密码提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)



忘记密码:

修改:/etc/my.cnf

[mysqld]skip-grant-tables


重启mysql即可无密码进入系统,

修改密码:

update mysql.user set authentication_string='zhdc530' where user='root';update mysql.user set authentication_string=PASSWORD('zhdc530') where user='root';-- 刷新数据库权限flush privileges;



第一次登录执行sql需要修改密码

alter user user() identified by "XXXXXX";


4. mysql优化

4.1 慢sql

/etc/my.cnf 加上

slow_query_log = ONslow_query_log_file = /usr/local/mysql/data/slow.loglong_query_time = 1

4.2 参数

/etc/my.cnf 加上

sql_mode='NO_AUTO_CREATE_USER'

innodb_buffer_pool_size=8Gtransaction_isolation = READ-COMMITTEDinnodb_flush_method = O_DIRECT

open_files_limit = 65535back_log = 1024

interactive_timeout = 28800wait_timeout = 3600

tmp_table_size = 32Mmax_heap_table_size = 32M

5. binlog开启

/etc/my.cnf 加上

# binloglog-bin=coli-office-binlogbinlog-format=ROWserver_id=1binlog_cache_size = 4Mmax_binlog_cache_size = 2Gmax_binlog_size = 1Gexpire_logs_days = 5master_info_repository = TABLErelay_log_info_repository = TABLE


简要命令[4]

show binary logs #获取binlog文件日志列表show master status # 查看当前正在写入的binlog文件show master logs # 查看master上的binlog文件show binlog events #查看第一个binlog文件内容show binlog events 'mysql-bin.000002' # 查看指定binlog文件内容, 如:查看mysql-bin.000002文件内容


6. 主从同步[5]

6.1 主库修改:

-   开启二进制日志

-   配置唯一的server-id

-   获得master二进制日志文件名及位置

-   创建一个用于slave和master通信的用户账号

前面三步都做了,直接做第四步即可。

创建用于同步的用户账号

创建用户并授权:用户:rel1密码:colioffice@slave@123

CREATE USER 'repl'@'10.6.32.140' IDENTIFIED BY 'colioffice@slave@123';#创建用户
update mysql.user set authentication_string=PASSWORD('colioffice@slave@123'where User='repl';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.6.32.140';#分配权限flush privileges; #刷新权限

检查状态

SHOW MASTER STATUS;



6.2 从服务器slave修改:

-   从服务器:

    -   配置唯一的server-id

-   使用master分配的用户账号读取master二进制日志

-   启用slave服务

执行下命令:

CHANGE MASTER TO MASTER_HOST='10.6.32.152', MASTER_USER='repl', MASTER_PASSWORD='colioffice@slave@123', MASTER_LOG_FILE='coli-office-binlog.000004', MASTER_LOG_POS=154;
start slave# 启动slave同步进程


查看主从复制状态是否成功

show slave status\G

7. mysql的dump文件恢复

20220223030001.sql.gz 文件解压 `gzip -d zht_20220223030001.sql.gz`, 可获得

`zht_20220223030001.sql`。

master上执行

mysql -u root -p < /data/zht_20220223030001.sql# 输入密码

开始数据备份的还原。


8. mysql监控

8.1 查看cpu架构[7]

[root@mysql-master ~]# cat /proc/versionLinux version 3.10.0-1062.4.3.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Wed Nov 13 23:58:53 UTC 2019[root@mysql-master ~]# uname -aLinux zhdc-zht-mysql-master 3.10.0-1062.4.3.el7.x86_64 #1 SMP Wed Nov 13 23:58:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux[root@mysql-master ~]# file /bin/bash/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=7e644dee920bc3ba797c38e05383286563712b49, stripped


aarch64, arm64 – ARM架构  

x86_64,x64,AMD64 – X86架构  

mips – MIPS架构


说明当前系统的cpu是x86架构


 8.2 explorer安装

https://github.com/prometheus/mysqld_exporter/releases/tag/v0.13.0

解压

mv mysqld_exporter-0.11.0.linux-amd64 /usr/local/mysql_exportercd /usr/local/mysql_exporter

登录mysql为exporter创建账号并授权

CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'hello@exporter';GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';flush privileges; #刷新权限

创建mysql配置文件、运行时可免密码连接数据库:`vim /usr/local/mysql_exporter/.my.cnf

[client]user=exporterpassword=hello@exporter


启动exporter客户端


`./mysqld_exporter --config.my-cnf=.my.cnf`


启动常用参数

# 选择采集innodb--collect.info_schema.innodb_cmp # innodb存储引擎状态--collect.engine_innodb_status # 指定配置文件--config.my-cnf=".my.cnf"

添加系统服务:vi /usr/lib/systemd/system/mysql_exporter.service

[Unit]Description=https://prometheus.io

[Service]Restart=on-failureExecStart=/usr/local/mysql_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysql_exporter/.my.cnf

[Install]WantedBy=multi-user.target


启动添加后的系统服务


systemctl daemon-reloadsystemctl restart mysql_exporter.service


网站查看捕获mysql数据


访问:http://localhost:9104/metrics



[参考]

- 1. https://blog.csdn.net/everything1209/article/details/54098362

- 2. https://blog.csdn.net/zoucui/article/details/96996554

- 3. https://blog.csdn.net/wwwdc1012/article/details/88373440

- 4. https://blog.csdn.net/weixin_42071874/article/details/88935346

- 5. https://www.cnblogs.com/gl-developer/p/6170423.html

- 6. https://www.cnblogs.com/linuxk/p/9371475.html

- 7. https://blog.csdn.net/raoxiaoya/article/details/109130613

- 8. https://www.cnblogs.com/xiangsikai/p/11289675.html