骨科醫(yī)院網(wǎng)站優(yōu)化服務商seo網(wǎng)絡推廣公司報價
文章目錄
- Ansible 自動化運維工具部署主從數(shù)據(jù)庫+讀寫分離
- 一、主從復制和讀寫分離介紹
- 二、準備工作
- (1)節(jié)點規(guī)劃
- (2)修改主機名
- (3)免密
- (4)配置IP映射
- (5)安裝ansible
- (6)配置主機清單文件
- 三、目錄結構
- (1)創(chuàng)建項目目錄
- (2)創(chuàng)建角色目錄
- (3)創(chuàng)建變量目錄
- (4)init角色
- (5)編寫劇本入口文件
- 四、編寫角色任務文件
- (1)init初始化任務文件
- (2)mysql-master任務文件
- (3)mysql-slave任務文件
- (4)mysql-create-testdb任務文件
- (5)mycat任務文件
- 五、執(zhí)行playbook
- 六、驗證主從復制讀寫分離
- (1)登錄mycat管理窗口
- (2)讀寫分離驗證
Ansible 自動化運維工具部署主從數(shù)據(jù)庫+讀寫分離
一、主從復制和讀寫分離介紹
數(shù)據(jù)庫主從復制是一種常見的數(shù)據(jù)庫架構,用于提高數(shù)據(jù)庫的可用性、可擴展性和性能。它通過將寫操作(主節(jié)點)復制到一個或多個從節(jié)點來實現(xiàn)數(shù)據(jù)的同步。
主從復制的工作原理如下:
- 首先,將一個數(shù)據(jù)庫節(jié)點指定為主節(jié)點,所有的寫操作都在主節(jié)點上執(zhí)行。
- 主節(jié)點將寫操作記錄到二進制日志(binary log)中,并將這些日志發(fā)送到從節(jié)點。
- 從節(jié)點接收到二進制日志后,將其應用到自己的數(shù)據(jù)庫上,實現(xiàn)數(shù)據(jù)的同步。
讀寫分離是基于主從復制的,核心思想是將讀操作分發(fā)到多個從節(jié)點上,而將寫操作集中在主節(jié)點上。Mycat是一個開源的數(shù)據(jù)庫中間件,支持讀寫分離,將讀操作和寫操作分別分發(fā)到不同的數(shù)據(jù)庫節(jié)點上
二、準備工作
(1)節(jié)點規(guī)劃
準備三臺服務器,centos7.9.2009
IP | 節(jié)點 |
---|---|
192.168.100.10 | ansible |
192.168.100.20 | master |
192.168.100.30 | slave |
192.168.100.40 | mycat |
(2)修改主機名
1. 修改主機名
# ansible節(jié)點
[root@localhost ~]# hostnamectl set-hostname ansible
[root@localhost ~]# bash
[root@ansible ~]#
# master節(jié)點
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@master ~]#
# slave節(jié)點
[root@localhost ~]# hostnamectl set-hostname slave
[root@localhost ~]# bash
[root@slave ~]#
# mycat節(jié)點
[root@localhost ~]# hostnamectl set-hostname mycat
[root@localhost ~]# bash
[root@mycat ~]#
(3)免密
[root@ansible ~]# ssh-keygen
[root@master ~]# ssh-keygen
[root@slave ~]# ssh-keygen
[root@mycat ~]# ssh-keygen[root@ansible ~]# ssh-copy-id 192.168.100.20
[root@ansible ~]# ssh-copy-id 192.168.100.30
[root@ansible ~]# ssh-copy-id 192.168.100.40
(4)配置IP映射
[root@ansible ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.10 ansible
192.168.100.20 master
192.168.100.30 slave
192.168.100.40 mycat
(5)安裝ansible
[root@ansible ~]# yum install -y epel-release vim tree
[root@ansible ~]# yum install -y ansible
[root@ansible ~]# ansible --version
ansible 2.9.27config file = /etc/ansible/ansible.cfgconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python2.7/site-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 2.7.5 (default, Oct 14 2020, 14:45:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
(6)配置主機清單文件
[root@ansible ~]# vim /etc/ansible/hosts
[master]
192.168.100.20
[slave]
192.168.100.30
[mycat]
192.168.100.40[root@ansible ~]# ansible all -m ping
192.168.100.30 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
}
192.168.100.20 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"
三、目錄結構
(1)創(chuàng)建項目目錄
[root@ansible ~]# mkdir mycat_mariadb
[root@ansible ~]# cd mycat_mariadb/
(2)創(chuàng)建角色目錄
在創(chuàng)建roles之前,我們將數(shù)據(jù)庫讀寫分離部署的步驟分為 3個role執(zhí)行,這樣更加易懂
init(初始化)
mysql(主從復制)
mycat(讀寫分離)
[root@ansible mycat_mariadb]# mkdir -p roles/{mysql-master,mysql-slave,init,mycat,mysql-create-testdb}/{templates,tasks,files}
這次項目中會用到template、tasks、files目錄,可以選擇性創(chuàng)建,自己用到哪個創(chuàng)建哪個
(3)創(chuàng)建變量目錄
創(chuàng)建該目錄用來存放變量
[root@ansible mycat_mariadb]# mkdir group_vars
[root@ansible mycat_mariadb]# vim group_vars/all
PASSWD: '000000'
master_host: 192.168.100.20
slave_host: 192.168.100.30
mycat_host: 192.168.100.40
shujuku: testdb
(4)init角色
這一步操作是用來創(chuàng)建數(shù)據(jù)庫主從復制,以及讀寫分離的配置文件
master-my.cnf.j2文件內容
[root@ansible mycat_mariadb]# vim roles/mysql-master/files/master-my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server-id=20
log_bin = mysql-bin
binlog-ignore-db=mysql
binlog-do-db=testdb
binlog_format=STATEMENT
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
slave-my.cnf.j2 文件內容
[root@ansible mycat_mariadb]# vim roles/mysql-slave/files/slave-my.cnf[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemdserver_id=30
relay-log=mysql-relay
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
server.xml.j2 文件內容
[root@ansible mycat_mariadb]# vim roles/mycat/templates/server.xml.j2
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/"><system><property name="useSqlStat">0</property> <!-- 1為開啟實時統(tǒng)計、0為關閉 --><property name="useGlobleTableCheck">0</property> <!-- 1為開啟全加班一致性檢測、0為關閉 --><property name="sequnceHandlerType">2</property><!-- <property name="useCompression">1</property>--> <!--1為開啟mysql壓縮協(xié)議--><!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--設置模擬的MySQL版本號--><!-- <property name="processorBufferChunk">40960</property> --><!-- <property name="processors">1</property> <property name="processorExecutor">32</property> --><!--默認為type 0: DirectByteBufferPool | type 1 ByteBufferArena--><property name="processorBufferPoolType">0</property><!--默認是65535 64K 用于sql解析時最大文本長度 --><!--<property name="maxStringLiteralLength">65535</property>--><!--<property name="sequnceHandlerType">0</property>--><!--<property name="backSocketNoDelay">1</property>--><!--<property name="frontSocketNoDelay">1</property>--><!--<property name="processorExecutor">16</property>--><!--<property name="serverPort">8066</property> <property name="managerPort">9066</property> <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> --><!--分布式事務開關,0為不過濾分布式事務,1為過濾分布式事務(如果分布式事務內只涉及全局表,則不過濾),2為不過濾分布式事務,但是記錄分布式事務日志--><property name="handleDistributedTransactions">0</property><!--off heap for merge/order/group/limit 1開啟 0關閉--><property name="useOffHeapForMerge">1</property><!--單位為m--><property name="memoryPageSize">1m</property><!--單位為k--><property name="spillsFileBufferSize">1k</property><property name="useStreamOutput">0</property><!--單位為m--><property name="systemReserveMemorySize">384m</property><!--是否采用zookeeper協(xié)調切換 --><property name="useZKSwitch">true</property></system><!-- 全局SQL防火墻設置 --><!-- <firewall> <whitehost><host host="127.0.0.1" user="mycat"/><host host="127.0.0.2" user="mycat"/></whitehost><blacklist check="false"></blacklist></firewall>--><user name="root"><property name="password">{{PASSWD}}</property><property name="schemas">TESTDB</property><!-- 表級 DML 權限設置 --><!-- <privileges check="false"><schema name="TESTDB" dml="0110" ><table name="tb01" dml="0000"></table><table name="tb02" dml="1111"></table></schema></privileges>--></user><user name="user"><property name="password">{{PASSWD}}</property><property name="schemas">TESTDB</property><property name="readOnly">true</property></user></mycat:server>
schema.xml.j2 文件內容
[root@ansible mycat_mariadb]# vim roles/mycat/templates/schema.xml.j2 <?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/"><schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema><dataNode name="dn1" dataHost="host1" database="{{shujuku}}" /><dataHost name="host1" maxCon="1000" minCon="10" balance="3"writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"><heartbeat>select user()</heartbeat><writeHost host="hostM1" url="{{master_host}}:3306" user="root"password="{{PASSWD}}"><readHost host="hostS1" url="{{slave_host}}:3306" user="root" password="{{PASSWD}}" /></writeHost></dataHost>
</mycat:schema>
mycat.sh
[root@ansible mycat_mariadb]# vim roles/mycat/files/mycat.sh
#!/bin/bash
cd /usr/local/mycat/bin/ && ./mycat start
(5)編寫劇本入口文件
將調用roles的順序及哪些主機調用哪些roles在這個文件夾中體現(xiàn)出來
[root@ansible mycat_mariadb]# vim mycat_mariadb.yaml
- hosts: allremote_user: rootroles:- init- hosts: masterremote_user: rootroles:- mysql-master- hosts: slaveremote_user: rootroles:- mysql-slave- hosts: mycatremote_user: rootroles:- mycat
四、編寫角色任務文件
(1)init初始化任務文件
[root@ansible tasks]# vim /root/mycat_mariadb/roles/init/tasks/main.yaml - name: 配置所有主機的host映射copy: src=/etc/hosts dest=/etc/hosts- name: 關閉防火墻shell: systemctl stop firewalld && systemctl disable firewalld && setenforce 0- name: 安裝mariadb expectshell: yum install -y mariadb-server expect- name: 設置開機自啟并開啟mariadbshell: systemctl enable mariadb --now- name: 設置密碼shell: mysqladmin -uroot password '{{PASSWD}}'- name: 安裝MySQL-pythonshell: yum install -y MySQL-python
(2)mysql-master任務文件
[root@ansible tasks]# vim /root/mycat_mariadb/roles/mysql-master/tasks/main.yaml - name: 移動文件copy: src=master-my.cnf dest=/etc/my.cnf- name: 重新啟動mariadbshell: systemctl restart mariadb- name: 設置root用戶訪問權限shell: mysql -uroot -p{{PASSWD}} -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '{{PASSWD}}';flush privileges;"- name: 創(chuàng)建數(shù)據(jù)庫用戶用于復制shell: mysql -uroot -p{{PASSWD}} -e "grant replication slave on *.* to 'csq'@'192.168.100.%' identified by '{{PASSWD}}';"- name: 存放變量Log_nameshell: mysql -uroot -p{{PASSWD}} -e "show master status;"|awk 'NR==2{print $1}'register: file
- name: 存放變量File_sizeshell: mysql -uroot -p{{PASSWD}} -e "show master status;"|awk 'NR==2{print $2}'register: pot- name: 設置全局變量set_fact: masterbin={{ file.stdout_lines[0] }}
- name: 設置全局變量set_fact: position={{ pot.stdout_lines[0] }}
(3)mysql-slave任務文件
[root@ansible mycat_mariadb]# vim roles/mysql-slave/tasks/main.yaml- name: 移動文件copy: src=slave-my.cnf dest=/etc/my.cnf
- name: 重啟mariadbshell: systemctl restart mariadb- name: 設置root用戶訪問權限shell: mysql -uroot -p{{PASSWD}} -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '{{PASSWD}}';flush privileges;"- name: 設置主數(shù)據(jù)信息mysql_replication:login_user: rootlogin_password: '000000'mode: changemastermaster_user: csqmaster_password: '000000'master_host: 192.168.100.20master_log_file: "{{ hostvars['192.168.100.20']['masterbin'] }}"master_log_pos: "{{ hostvars['192.168.100.20']['position'] }}"- name: 開啟slaveshell: mysql -uroot -p{{PASSWD}} -e "start slave;" - name: 定義輸出的信息shell: mysql -uroot -p{{PASSWD}} -e "show slave status\G;" | grep -E "Slave_IO_Running|Slave_SQL_Running"register: slave_status_output
- name: 打印輸出的信息debug:var: slave_status_output.stdout
(4)mysql-create-testdb任務文件
[root@ansible mycat_mariadb]# vim roles/mysql-create-testdb/tasks/main.yaml
- name: 創(chuàng)建用于復制的testdb庫shell: mysql -uroot -p000000 -e "create database testdb;"
- name: 創(chuàng)建表插入數(shù)據(jù)shell: mysql -uroot -p000000 -e "use testdb; create table mytbl(id int,name varchar(20));insert into mytbl values(1,'csq');"
(5)mycat任務文件
[root@ansible mycat_mariadb]# vim roles/mycat/tasks/main.yaml- name: 設置root用戶訪問權限shell: mysql -uroot -p{{PASSWD}} -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '{{PASSWD}}';flush privileges;"- name: 安裝openjdkshell: yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel wget net-tools- name: 下載Mycat軟件包shell: wget http://dl.mycat.org.cn/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz- name: 解壓Mycat軟件包到/usr/localshell: tar -zxvf /root/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/- name: 復制模板文件template: src=schema.xml.j2 dest=/usr/local/mycat/conf/schema.xml- name: 復制模板文件template: src=server.xml.j2 dest=/usr/local/mycat/conf/server.xml- name: 開啟mycatshell: /bin/bash /usr/local/mycat/bin/mycat start
五、執(zhí)行playbook
[root@ansible mycat_mariadb]# ansible-playbook mycat_mariadb.yaml PLAY [all] ************************************************************************************************************************************************************TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [192.168.100.40]
ok: [192.168.100.30]
ok: [192.168.100.20]TASK [init : 配置所有主機的host映射] *******************************************************************************************************************************************
changed: [192.168.100.40]
changed: [192.168.100.20]
changed: [192.168.100.30]TASK [init : 關閉防火墻] ***************************************************************************************************************************************************
changed: [192.168.100.40]
changed: [192.168.100.30]
changed: [192.168.100.20]TASK [init : 安裝mariadb expect] ****************************************************************************************************************************************
[WARNING]: Consider using the yum module rather than running 'yum'. If you need to use command because yum is insufficient you can add 'warn: false' to this command
task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.100.40]
changed: [192.168.100.30]
changed: [192.168.100.20]TASK [init : 設置開機自啟并開啟mariadb] ****************************************************************************************************************************************
changed: [192.168.100.30]
changed: [192.168.100.40]
changed: [192.168.100.20]TASK [init : 設置密碼] ****************************************************************************************************************************************************
changed: [192.168.100.30]
changed: [192.168.100.20]
changed: [192.168.100.40]TASK [init : 安裝MySQL-python] ******************************************************************************************************************************************
changed: [192.168.100.40]
changed: [192.168.100.30]
changed: [192.168.100.20]PLAY [master] *********************************************************************************************************************************************************TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [192.168.100.20]TASK [mysql-master : 移動文件] ********************************************************************************************************************************************
changed: [192.168.100.20]TASK [mysql-master : 重新啟動mariadb] *************************************************************************************************************************************
changed: [192.168.100.20]TASK [mysql-master : 設置root用戶訪問權限] ************************************************************************************************************************************
changed: [192.168.100.20]TASK [mysql-master : 創(chuàng)建數(shù)據(jù)庫用戶用于復制] *************************************************************************************************************************************
changed: [192.168.100.20]TASK [mysql-master : 存放變量Log_name] ************************************************************************************************************************************
changed: [192.168.100.20]TASK [mysql-master : 存放變量File_size] ***********************************************************************************************************************************
changed: [192.168.100.20]TASK [mysql-master : 設置全局變量] ******************************************************************************************************************************************
ok: [192.168.100.20]TASK [mysql-master : 設置全局變量] ******************************************************************************************************************************************
ok: [192.168.100.20]PLAY [slave] **********************************************************************************************************************************************************TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [192.168.100.30]TASK [mysql-slave : 移動文件] *********************************************************************************************************************************************
changed: [192.168.100.30]TASK [mysql-slave : 重啟mariadb] ****************************************************************************************************************************************
changed: [192.168.100.30]TASK [mysql-slave : 設置root用戶訪問權限] *************************************************************************************************************************************
changed: [192.168.100.30]TASK [mysql-slave : 設置主數(shù)據(jù)信息] ******************************************************************************************************************************************
changed: [192.168.100.30]TASK [mysql-slave : 開啟slave] ******************************************************************************************************************************************
changed: [192.168.100.30]TASK [mysql-slave : 定義輸出的信息] ******************************************************************************************************************************************
changed: [192.168.100.30]TASK [mysql-slave : 打印輸出的信息] ******************************************************************************************************************************************
ok: [192.168.100.30] => {"slave_status_output.stdout": " Slave_IO_Running: Yes\n Slave_SQL_Running: Yes"
}PLAY [master] *********************************************************************************************************************************************************TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [192.168.100.20]TASK [mysql-create-testdb : 創(chuàng)建用于復制的testdb庫] ***************************************************************************************************************************
changed: [192.168.100.20]TASK [mysql-create-testdb : 創(chuàng)建表插入數(shù)據(jù)] **********************************************************************************************************************************
changed: [192.168.100.20]PLAY [mycat] **********************************************************************************************************************************************************TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [192.168.100.40]TASK [mycat : 設置root用戶訪問權限] *******************************************************************************************************************************************
changed: [192.168.100.40]TASK [mycat : 安裝openjdk] **********************************************************************************************************************************************
changed: [192.168.100.40]TASK [mycat : 下載Mycat軟件包] *********************************************************************************************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'wget'. If you need to use command because get_url or uri is insufficient you can add 'warn:
false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.100.40]TASK [mycat : 解壓Mycat軟件包到/usr/local] **********************************************************************************************************************************
[WARNING]: Consider using the unarchive module rather than running 'tar'. If you need to use command because unarchive is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.100.40]TASK [mycat : 復制模板文件] *************************************************************************************************************************************************
changed: [192.168.100.40]TASK [mycat : 復制模板文件] *************************************************************************************************************************************************
changed: [192.168.100.40]TASK [開啟mycat] ********************************************************************************************************************************************************
changed: [192.168.100.40]PLAY RECAP ************************************************************************************************************************************************************
192.168.100.20 : ok=19 changed=14 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.100.30 : ok=15 changed=12 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.100.40 : ok=15 changed=13 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在執(zhí)行過程中會打印出這兩條信息,如果都是YES 說明你主從配置成功了
六、驗證主從復制讀寫分離
(1)登錄mycat管理窗口
[root@mycat ~]# mysql -uroot -p000000 -h127.0.0.1 -P 9066
查看讀寫配置情況
MySQL [(none)]> show @@datasource;
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
| dn1 | hostM1 | mysql | 192.168.100.20 | 3306 | W | 0 | 10 | 1000 | 57 | 0 | 1 |
| dn1 | hostS1 | mysql | 192.168.100.30 | 3306 | R | 0 | 5 | 1000 | 53 | 2 | 0 |
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
參數(shù)詳解:
DATANODE:數(shù)據(jù)節(jié)點的名稱
NAME:數(shù)據(jù)節(jié)點的標識名稱
TYPE:數(shù)據(jù)節(jié)點的類型,這里是mysql
HOST:數(shù)據(jù)節(jié)點的主機地址
PORT:數(shù)據(jù)節(jié)點的端口號
W/R:數(shù)據(jù)節(jié)點的讀寫類型,W表示寫入,R表示讀取
ACTIVE:當前活躍連接數(shù)
IDLE:當前空閑連接數(shù)
ZE:連接池大小,即連接池中的最大連接數(shù)
EXECUTE:執(zhí)行次數(shù),表示該數(shù)據(jù)節(jié)點的執(zhí)行操作次數(shù)
READ_LOAD:讀取負載,表示該數(shù)據(jù)節(jié)點的讀取負載情況
查看心跳信息
MySQL [(none)]> show @@heartbeat;
+--------+-------+----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| NAME | TYPE | HOST | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME | STOP |
+--------+-------+----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| hostM1 | mysql | 192.168.100.20 | 3306 | 1 | 0 | idle | 0 | 1,1,1 | 2023-09-13 15:27:30 | false |
| hostS1 | mysql | 192.168.100.30 | 3306 | 1 | 0 | idle | 0 | 1,0,1 | 2023-09-13 15:27:30 | false |
+--------+-------+----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
參數(shù)詳解:
NAME:心跳節(jié)點的名稱
TYPE:心跳節(jié)點的類型,這里是mysql
HOST:心跳節(jié)點的主機地址
PORT:心跳節(jié)點的端口號
RS_CODE:復制狀態(tài)碼,用于判斷復制是否正常進行 (1表示主從復制正常進行)
RETRY:重試次數(shù),表示心跳節(jié)點嘗試重連的次數(shù)
STATUS:心跳節(jié)點的狀態(tài),包括idle(空閑)和active(活躍)
TIMEOUT:超時時間,表示心跳節(jié)點的超時時間
EXECUTE_TIME:執(zhí)行時間,表示心跳節(jié)點的執(zhí)行時間
LAST_ACTIVE_TIME:最后活躍時間,表示心跳節(jié)點最后一次發(fā)送心跳的時間
(2)讀寫分離驗證
[root@mycat ~]# mysql -uroot -p000000 -h127.0.0.1 -P 8066
MySQL [(none)]> use TESTDB;
MySQL [TESTDB]> insert into mytbl values(1,@@hostname); # 插入數(shù)據(jù)
MySQL [TESTDB]> select * from mytbl; # 查詢數(shù)據(jù),自動跳轉從主機進行查詢
+------+-------+
| id | name |
+------+-------+
| 1 | csq |
| 1 | slave |
+------+-------+
# @@hostname是MySQL系統(tǒng)變量,表示當前數(shù)據(jù)庫服務器的主機名。
# 由此可以推斷,select * from mytbl;查詢的是從庫。
使用master節(jié)點訪問一下
[root@master ~]# mysql -uroot -p000000 -e "use testdb;select * from mytbl;"
+------+--------+
| id | name |
+------+--------+
| 1 | csq |
| 1 | master |
使用slave節(jié)點訪問一下
[root@slave ~]# mysql -uroot -p000000 -e "use testdb;select * from mytbl;"
+------+-------+
| id | name |
+------+-------+
| 1 | csq |
| 1 | slave |
+------+-------+
至此 Ansible部署主從數(shù)據(jù)庫+讀寫分離結束