测试环境说明:
硬件 | 型号 | |||
---|---|---|---|---|
服务器 | Inspur NF5260M6 | |||
CPU | 2*Intel(R) Xeon(R) Gold 6348 CPU @ 2.60GHz | |||
内存 | 256G | |||
磁盘 |
无本地盘,挂载云盘系统盘和云盘数据盘 |
|||
网卡 |
业务网采用挂载1张主网卡,8张弹性网卡 管理网采用2张Intel Corporation I350 1G网卡 |
|||
智能网卡 | 紫金DPU2.0 |
1. 测试环境部署
# 一、前置准备
## 1、检查yum源
[root@host-92 secure]# yum info yum
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
Installed Packages
Name : yum
Arch : noarch
Version : 3.4.3
Release : 161.el7.centos
Size : 5.6 M
Repo : installed
.......
# 二、安装主节点MySQL
1、安装MariaDB
yum -y install mariadb mariadb-server
2. 修改MySQL挂载的数据盘等位置,单独格式化文件系统和挂载到相应的目录下面
## 2.1、检查挂载目标盘vdb
[root@huangtj-mysql-01 ~]# ps -ef|grep mysql
mysql 103619 1 0 15:46 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql 103816 103619 0 15:46 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock root 107843 102003 0 16:09 pts/1 00:00:00 grep --color=auto mysql
检查根目录的挂载盘
[root@huangtj-mysql-01 ~]# df /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda4 52927232 1924320 51002912 4% /
检查空闲的目标盘vdb
[root@huangtj-mysql-01 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1
├─sda2 ext4 e98107bb-38cd-4076-9ef5-362039dbf3f4
├─sda3 ext4 edd7e74d-d91a-43f1-af06-fd64413790f8
└─sda4
vda
├─vda1
├─vda2 vfat 4BF4-6DA4 /boot/efi
|─vda3 ext2 cfacfb0b-a2ca-43bc-b114-df92a7100e7b /boot
├─vda4 xfs 3d511aba-cf83-4b85-9f6b-ae217eec098a /
└─vda5 swap 20c68b57-cacc-48af-b29c-ff297b645197
vdb
[root@huangtj-mysql-01 ~]# df /var/lib/
Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda4 52927232 1924324 51002908 4% /
检查磁盘写入性能,检查磁盘不能限制数据库的写入能力
[root@host-92 secure]# dd if=/dev/zero of=/data/tmp/tmp.file bs=4k count=1000000
1000000+0 records in
1000000+0 records out
4096000000 bytes (4.1 GB) copied, 1.89153 s, 2.2 GB/s
## 2.2、修改数据盘并挂载
#格式化文件系统并挂载分区
mkfs.xfs /dev/vdb
mkdir -p /data/mysql
mount /dev/vdb /data/mysql
#修改/etc/fstab文件
[root@gaoji-zyk-10-8-93-105 mysql]# blkid /dev/vdb
/dev/vdb: UUID="36b8d09c-ae21-46ee-9c2c-3e12773cd696" BLOCK_SIZE="512" TYPE="xfs"
[root@gaoji-zyk-10-8-93-105 mysql]# cat /etc/fstab
UUID=D91D-49A3 /boot/efi vfat defaults 0 0
UUID=61596cb9-d7c9-40ae-9a03-a71f80462a25 /boot ext2 defaults 0 0
UUID=f38b9af0-5dfb-4261-94b7-f0cc0a7ba63f / xfs defaults 0 1
UUID=36b8d09c-ae21-46ee-9c2c-3e12773cd696 /data/mysql xfs rw,suid,dev,exec,auto,nouser,sync,relatime 0 1
#先停止mysql
systemctl stop mariadb
#移动数据:
mv /var/lib/mysql/* /data/mysql/
#创建软连接
ln -s /data/mysql/ /var/lib/mysql/
#修改my.conf
Under [mysqld]:
datadir=/data/mysql
socket=/data/mysql/mysql.sock
Under [client]:
port=3306
socket=/data/mysql/mysql.sock
#启动mysql,保证MySQL文件夹的权限足够 chown -R mysql:mysql /data/mysql/
### 常见问题处理:
- **保证MySQL文件夹的权限足够 chown -R mysql:mysql /data/mysql/**
3、启动
启动数据库
systemctl start mariadb
确保重启reboot后数据库能启动
systemctl enable mariadb
4、查看状态 ,需要是activing状态
service mariadb status
预期结果: Active: active (running)
[root@host-92 secure]# service mariadb status
Redirecting to /bin/systemctl status mariadb.service
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2022-08-09 17:47:54 CST; 20h ago
Process: 22365 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 22332 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 22364 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─22364 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─22920 /usr/libexec/mysqld --basedir=/usr --datadir=/data/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/data/...
Aug 09 17:47:23 host-92 systemd[1]: Starting MariaDB database server...
Aug 09 17:47:23 host-92 mariadb-prepare-db-dir[22332]: Database MariaDB is probably initialized in /data/mysql already, nothing is done.
Aug 09 17:47:23 host-92 mariadb-prepare-db-dir[22332]: If this is not the case, make sure the /data/mysql is empty before running mariadb-prepare-db-dir.
Aug 09 17:47:24 host-92 mysqld_safe[22364]: 220809 17:47:24 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Aug 09 17:47:24 host-92 mysqld_safe[22364]: 220809 17:47:24 mysqld_safe Starting mysqld daemon with databases from /data/mysql
Aug 09 17:47:54 host-92 systemd[1]: Started MariaDB database server.
5、开启MySQL二进制日志记录
执行vim /etc/my.cnf,向文件中写入下面的记录,要求主节点和从节点server-id不冲突且唯一
[mysqld]
log-bin=mysql-bin
server-id=24
6、重启mysql服务
systemctl restart mariadb
7、初始化数据库账户密码
1.进入mysql命令行
mysql -uroot
直接回车即可
2.执行
use mysql;
update user set Password=password('xxxxxxx') where User='root';
3.提交修改并检查
flush privileges;
select Host,User,password from user where user='root';
4.退出
quit;
5.重新登录数据库,此时需要密码
mysql -u root -p
8、检查日志记录
日志检查查看二进制日志是否开启 :
show global variables like '%log_bin%';
查看主节点二进制列表
SHOW MASTER LOGS;
查看主节点server id
SHOW GLOBAL VARIABLES LIKE '%server%';
MariaDB [(none)]> show global variables like '%log_bin%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin | ON |
| log_bin_trust_function_creators | OFF |
| sql_log_bin | ON |
+---------------------------------+-------+
3 rows in set (0.00 sec)
MariaDB [(none)]> SHOW MASTER LOGS;
+------------------+------------+
| Log_name | File_size |
+------------------+------------+
| mysql-bin.000001 | 351 |
| mysql-bin.000002 | 1073904425 |
| mysql-bin.000003 | 1074371223 |
| mysql-bin.000004 | 1074895548 |
.......
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%server%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| character_set_server | latin1 |
| collation_server | latin1_swedish_ci |
| server_id | 24 |
+----------------------+-------------------+
9、在主节点上创建有复制权限的用户
GRANT REPLICATION SLAVE ON *.* TO 'repl_master'@'xxxxx' IDENTIFIED BY 'xxxxxx';
update user set Password=password('xxxxxxx') where User='repl_master';
flush privileges;
select * from mysql.user;, 预期结果如下
*************************** 7. row ***************************
Host: xx.xx.xx.xx
User: repl_master
Password: ***********************
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: N
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string:
备注:repl_master, xx.xx.xx.xx, 是要给从节点使用并执行复制权限,分别表示从节点使用的账户、从节点IP、账户密码;当前用例的数据是mock的,不代表真实的IP、账号和密码
9、锁表
在从节点开始同步之前,对主节点数据库进行锁表操作,最好不要在源库持续大量写入数据的场景下建立主从连接!
此时,如果条件允许 为了同步前数据一致,建议锁定一下表 :
flush tables with read lock;
查看【Master(主库)】状态:
SHOW MASTER STATUS;
记录二进制文件名 (masterl-bin.00000*) 和位置 (Position) (如果有多个二进制文件名,取最后一个的名称和位置!!),确保同步点正常!
MariaDB [(none)]> SHOW MASTER STATUS;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000001 | 832 | | |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)
# 三、安装从节点
## 1-3、从节点安装数据库
先执行步骤二里面主节点安装顺序里的【1、2、3】,然后按照下面的顺序执行!
## 4、开启relay log
[mysqld]
server-id=23
relay-log=slave-relay-log
relay-log-index=slave-relay-log.index
## 5、同步binlog
**需要主服务器主机名,登陆凭据**,**二进制文件(最后一个)的名称和位置**
mysql -urepl_master -p -h10.0.1.3
停止从节点同步
stop slave;
reset slave;
关联主节点账户和binlog信息
CHANGE MASTER TO MASTER_HOST='xx.xx.xx.xx', MASTER_USER='repl_master', MASTER_PASSWORD='xxxxxxx', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=832;
开启主从同步
start slave;
show slave status;
## 6、检查主从同步状态
show slave status;
输出:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: ***********
Master_User: repl_master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000416
Read_Master_Log_Pos: 100147369
Relay_Log_File: slave-relay-log.000025
Relay_Log_Pos: 100147653
Relay_Master_Log_File: mysql-bin.000416
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 100147369
Relay_Log_Space: 100147990
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 24
1 row in set (0.00 sec)
## 7、解锁主库表
unlock tables;
# 四、Sysbench部署
## 1、安装压测工具
下载sysbench安装包
yum -y install make automake libtool pkgconfig libaio-devel
yum -y install mariadb-devel openssl-devel
yum -y install postgresql-devel
./autogen.sh
##运行autogen.sh
./configure --prefix=/usr --mandir=/usr/share/man
make
##编译
make install
## 2、安装完成后会有一些 lua 脚本
[root@mgr1 src]# ll /usr/share/sysbench/
total 132
-rwxr-xr-x. 1 root root 1452 May 8 2020 bulk_insert.lua
-rw-r--r--. 1 root root 14369 May 8 2020 oltp_common.lua
-rwxr-xr-x. 1 root root 1290 May 8 2020 oltp_delete.lua
-rwxr-xr-x. 1 root root 2415 May 8 2020 oltp_insert.lua
## 3、部分lua脚本说明
insert.lua 单值插入数据
bulk_inert.lua 批量插入数据
delete.lua 删除数据
select.lua 简单主键查询
oltp.lua 混合读写测试
## 4、sysbench参数
MySQL连接信息参数
--mysql-host:MySQL服务器主机名,默认localhost;如果在本机上使用localhost报错,提示无法连接MySQL服务器,改成本机的IP地址应该就可以了。
--mysql-port:MySQL服务器端口,默认3306
--mysql-user:用户名
--mysql-password:密码
MySQL执行参数
--oltp-test-mode:执行模式,包括simple、nontrx和complex,默认是complex。simple模式下只测试简单的查询;nontrx不仅测试查询,还测试插入更新等,但是不使用事务;complex模式下测试最全面,会测试增删改查,而且会使用事务。可以根据自己的需要选择测试模式。
--oltp-tables-count:测试的表数量,根据实际情况选择
--oltp-table-size:测试的表的大小,根据实际情况选择
--threads:客户端的并发连接数
--time:测试执行的时间,单位是秒,该值不要太短,可以选择120
--report-interval:生成报告的时间间隔,单位是秒,如10
测试前准备:
主库上创建database:create database sbtest;
在sysbench机器上跑下面测试:
准备数据:
sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=xx.xx.xx.xx --mysql-port=3306 --mysql-user=root --mysql-password='xxxxxxx' --oltp-test-mode=complex --oltp-tables-count=50 --oltp-table-size=100000 --threads=512 --time=300 --report-interval=1 prepare
跑测试:
sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=xx.xx.xx.xx --mysql-port=3306 --mysql-user=root --mysql-password='xxxxxxx' --oltp-test-mode=complex --oltp-tables-count=50 --oltp-table-size=100000 --threads=512 --time=36000 --report-interval=1 run
清理数据:
sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=xx.xx.xx.xx --mysql-port=3306 --mysql-user=root --mysql-password='xxxxxxx' --oltp-test-mode=complex --oltp-tables-count=50 --oltp-table-size=100000 --threads=512 --time=300 --report-interval=1 cleanup