MySQL系列文章目录
使用dbops搭建MySQL8.4.3 一主一备
文章目录
前言
使用dbops搭建MySQL8.4.3 一主一备
以 MySQL8.4.3 一主一备为例
一、dbops是什么?
dbops 是一套高效的 ansible playbook 集合,目前是一个可以自动化安装和部署生产级别的 MySQL、openGauss 及周边生态的工具。dbops 正在持续迭代开发中,未来将支持更多功能。你可以查看 issue 以了解 dbops 已经支持的和未来将要支持的功能。开发历史请查看 releasehistory。目前,我们支持了 MySQL5.7、8.0、8.4,openGauss5.0、6.0。
二、部署步骤
2.1 下载和解压 dbops
如果您的服务器可联网
# 下载
dbops_version="1.10.20250329"
wget https://gitee.com/fanderchan/dbops/releases/download/dbops.${dbops_version}/dbops.${dbops_version}-Linux-x86_64.tar.gz
# 解压
tar -zxvf dbops.${dbops_version}-Linux-x86_64.tar.gz -C /usr/local/
2.2 安装 ansible
- dbops 自带绿色版 ansible,dbops 的核心依赖于 ansible,执行此命令用于注册 ansible 命令。
cd portable-ansible-v0.5.0-py3
sh setup_portable_ansible.sh
source ~/.bashrc
- 检查ansible是否成功
ansible --version
3.安装 MySQL8.4
3.1 下载 MySQL 8.4 安装包
- 请访问 MySQL 官方下载页面: https://dev.mysql.com/downloads/mysql/
- 选择适合您操作系统的版本(例如 Linux - Generic),下载 tar 包(mysql-8.4.3-linux-glibc2.17-x86_64.tar.xz)。
- 如果服务器可以联网,使用以下命令将安装包下载到服务器正确的位置:
cd /usr/local/dbops/mysql_ansible/
cd downloads
mysql_version="8.4.3"
wget https://dev.mysql.com/get/Downloads/MySQL-8.4/mysql-${mysql_version}-linux-glibc2.17-x86_64-minimal.tar.xz
- 此步骤不是必须的,因为如果服务器可以联网,dbops会自动下载。如果自己下载,请务必保证是下载glibc2.17、x86_64、minimal版本。
- 如有报错,可手工提前下载
3.2 配置mysql安装
- 编辑 inventory/hosts.ini 文件,添加 MySQL 主机信息:
cd /usr/local/dbops/mysql_ansible/
vi inventory/hosts.ini
#修改以下内容
[dbops_mysql]
172.16.2.192 ansible_user=root ansible_ssh_pass='rootroot'
172.16.2.193 ansible_user=root ansible_ssh_pass='rootroot'
[all:vars]
#ansible_python_interpreter=/usr/bin/python3
- 修改 playbooks/common_config.yml 文件中的 MySQL 相关配置:
cd /usr/local/dbops/mysql_ansible/
cd playbooks
vi common_config.yml
.....
mysql_version: "8.4.3" # 这也是dbops1.5的默认值
mysql_port: 3306 # 这也是dbops1.5的默认值
.....
- 修改 playbooks/vars/var_master_slave.yml 文件,配置 MySQL 主从信息
cd /usr/local/dbops/mysql_ansible/
cd playbooks
vi vars/var_master_slave.yml
# vars loading order: common_config -> this file
master_ip: 172.16.2.192 # 主库IP
slave_ips:
- 172.16.2.193 # 从库IP
sub_nets: 172.16.2.% # 主从复制账号可信网段,% 表示所有子网
3.3 执行mysql安装
- 运行以下命令开始安装:
cd /usr/local/dbops/mysql_ansible/
cd playbooks
ansible-playbook master_slave.yml
3.4 确认信息
- 在执行 playbook 后,会输出以下信息,请确认无误后输入 confirm 继续。
TASK [Display the list of target hosts and additional information] ***************************************************************************************************************************************
ok: [172.16.2.192] => {
"msg": [
"Hosts to be affected by Deploy master-slave MySQL server using binary installation: 172.16.2.192, 172.16.2.193",
"DB type: mysql",
"MySQL port: 3306",
"MySQL version: 8.4.3",
"Server specs: auto",
"Roles to be executed: ../roles/pre_check_and_set, ../roles/mysql_server, ../roles/make_replication",
"Master IP: 172.16.2.192",
"Slave IPs: 172.16.2.193",
"mycnf_dir: /database/mysql/etc/3306",
"datadir: /database/mysql/data/3306",
"tmpdir: /database/mysql/tmp/3306",
"binlog_dir: /database/mysql/log/binlog/3306",
"relaylog_dir: /database/mysql/log/relaylog/3306",
"redolog_dir: /database/mysql/log/redolog/3306",
"socket_dir: /database/mysql/data/3306",
"mysqlx_socket_dir: /database/mysql/data/3306"
]
}
TASK [Prompt user for confirmation] **********************************************************************************************************************************************************************
[Prompt user for confirmation]
This will perform Deploy master-slave MySQL server using binary installation on the displayed hosts. Please type 'confirm' to continue or press Ctrl+C to cancel.:
- 输入 confirm 后,会开始安装。安装完成后,会输出以下信息:
TASK [../roles/make_replication : Clear temp file /tmp/master_slaves.sql] ********************************************************************************************************************************
changed: [172.16.2.192]
changed: [172.16.2.193]
TASK [../roles/make_replication : Touch master_slave_finish.flag] ****************************************************************************************************************************************
changed: [172.16.2.192]
changed: [172.16.2.193]
PLAY RECAP ***********************************************************************************************************************************************************************************************
172.16.2.192 : ok=73 changed=36 unreachable=0 failed=0 skipped=40 rescued=0 ignored=2
172.16.2.193 : ok=65 changed=33 unreachable=0 failed=0 skipped=40 rescued=0 ignored=1
Playbook run took 0 days, 0 hours, 2 minutes, 50 seconds
- 至此,MySQL 8.4.3 一主一从的安装就完成了。
4. 验证主从同步
4.1 主库
root@localhost [(none)]> SHOW REPLICAS;
+------------+--------------+------+------------+--------------------------------------+
| Server_Id | Host | Port | Source_Id | Replica_UUID |
+------------+--------------+------+------------+--------------------------------------+
| 3705309033 | 172.16.2.193 | 3306 | 3650095756 | 0be1ac6a-201f-11f0-b083-000c290b6743 |
+------------+--------------+------+------------+--------------------------------------+
1 row in set (0.00 sec)
4.2 从库
root@localhost [(none)]> SHOW REPLICA STATUS \G
*************************** 1. row ***************************
Replica_IO_State: Waiting for source to send event
Source_Host: 172.16.2.192
Source_User: repl
Source_Port: 3306
Connect_Retry: 60
Source_Log_File: mysql-bin.000002
Read_Source_Log_Pos: 931
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 1148
Relay_Source_Log_File: mysql-bin.000002
Replica_IO_Running: Yes
Replica_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_Source_Log_Pos: 931
Relay_Log_Space: 1353
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Source_SSL_Allowed: No
Source_SSL_CA_File:
Source_SSL_CA_Path:
Source_SSL_Cert:
Source_SSL_Cipher:
Source_SSL_Key:
Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Source_Server_Id: 3650095756
Source_UUID: 0a837fe0-201f-11f0-af7f-000c295ea64c
Source_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
Source_Retry_Count: 10
Source_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Source_SSL_Crl:
Source_SSL_Crlpath:
Retrieved_Gtid_Set: 0a837fe0-201f-11f0-af7f-000c295ea64c:1-3
Executed_Gtid_Set: 0a837fe0-201f-11f0-af7f-000c295ea64c:1-3
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Source_TLS_Version:
Source_public_key_path:
Get_Source_public_key: 1
Network_Namespace:
1 row in set (0.00 sec)
//到此,主从同步正常
5. 注意事项
5.1 报错1
- 报错信息
fatal: [172.16.2.192]: FAILED! => {
"assertion": false,
"changed": false,
"evaluated_to": false,
"msg": "Duplicate mysql_server_id detected!"
}
- 临时解决方案
- 看了yml文件,不懂mysql_server_id的处理思路,就先忽略吧
- 有思路的小伙伴评论区留言啊
./pre_tasks/confirmation.yml:16:- name: Check for duplicate mysql_server_id
这个文件的第16行,末行增加ignore_errors: yes,修改如下:
- name: Check for duplicate mysql_server_id
assert:
that: "{{ all_mysql_server_ids | unique | length == all_mysql_server_ids | length }}"
msg: "Duplicate mysql_server_id detected!"
run_once: true
delegate_to: localhost
ignore_errors: yes # 临时忽略错误(仅调试用)
5.2 报错2
- 报错信息
TASK [../roles/pre_check_and_set : If it is a physical machine, double network card binding is required] *************************************************************************************************
fatal: [172.16.2.192]: FAILED! => {"msg": "The conditional check 'ansible_default_ipv4.address in ansible_all_ipv4_addresses' failed. The error was: error while evaluating conditional (ansible_default_ipv4.address in ansible_all_ipv4_addresses): 'dict object' has no attribute 'address'\n\nThe error appears to be in '/usr/local/dbops/mysql_ansible/roles/pre_check_and_set/tasks/main.yml': line 135, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: If it is a physical machine, double network card binding is required\n ^ here\n"}
fatal: [172.16.2.193]: FAILED! => {"msg": "The conditional check 'ansible_default_ipv4.address in ansible_all_ipv4_addresses' failed. The error was: error while evaluating conditional (ansible_default_ipv4.address in ansible_all_ipv4_addresses): 'dict object' has no attribute 'address'\n\nThe error appears to be in '/usr/local/dbops/mysql_ansible/roles/pre_check_and_set/tasks/main.yml': line 135, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: If it is a physical machine, double network card binding is required\n ^ here\n"}
- 需要双网卡绑定
总结
以上就是使用dbops搭建MySQL8.4.3 一主一备的整个安装过程,有问题的小伙伴评论区留言哈!