MySQL5.7.35雙主搭建的實(shí)現(xiàn)
上傳并安裝MySQL 5.7.35
(172.25.0.19與172.25.0.20均部署安裝并啟動)
[root@QYWLAQ_VMC4234 ~]# hostnamectl Static hostname: QYWLAQ_VMC4234 Icon name: computer-vm Chassis: vm Machine ID: cd3605d12efd430fb723eb507cf5ce3b Boot ID: 00f929e5510944efb40d53ef55d92a49 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.el7.x86_64 Architecture: x86-64 [root@QYWLAQ_VMC4234 ~]# ll -rw-r--r-- 1 citc unicom 26658592 12月 25 17:02 mysql-community-client-5.7.35-1.el7.x86_64.rpm -rw-r--r-- 1 citc unicom 317800 12月 25 17:03 mysql-community-common-5.7.35-1.el7.x86_64.rpm -rw-r--r-- 1 citc unicom 2473348 12月 25 17:03 mysql-community-libs-5.7.35-1.el7.x86_64.rpm -rw-r--r-- 1 citc unicom 1263816 12月 26 14:35 mysql-community-libs-compat-5.7.35-1.el7.x86_64.rpm -rw-r--r-- 1 citc unicom 182213816 12月 25 17:05 mysql-community-server-5.7.35-1.el7.x86_64.rpm # 安裝rpm包 [root@QYWLAQ_VMC4234 ~]# yum localinstall -y *.rpm # 啟動并設(shè)置開啟自啟 [root@QYWLAQ_VMC4234 ~]# systemctl enable --now mysqld [root@QYWLAQ_VMC4234 citc]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since 三 2025-02-26 12:24:03 CST; 4min 26s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 8441 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 8374 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 8444 (mysqld) Tasks: 27 Memory: 289.2M CGroup: /system.slice/mysqld.service └─8444 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid 2月 26 12:23:57 QYWLAQ_VMC4234 systemd[1]: Starting MySQL Server... 2月 26 12:24:03 QYWLAQ_VMC4234 systemd[1]: Started MySQL Server. # 獲取初始密碼 [root@QYWLAQ_VMC4234 citc]# sudo grep 'temporary password' /var/log/mysqld.log 2025-02-26T04:23:59.794639Z 1 [Note] A temporary password is generated for root@localhost: IlM3/)Ya6VMH # 登錄 [root@QYWLAQ_VMC4234 citc]# mysql -u root -p'IlM3/)Ya6VMH' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.35 Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '8th3xY]:NA'; -- 修改root密碼 Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; -- 刷新權(quán)限以使修改立即生效 Query OK, 0 rows affected (0.01 sec)
雙主復(fù)制的配置
實(shí)例一:172.25.0.19:
# 此配置文件中追加下述配置 [root@QYWLAQ_VMC4234 ~]# vi /etc/my.cnf [mysqld] # 基本配置 server-id = 1 # 每個(gè)實(shí)例的 server-id 必須唯一 log_bin = /var/log/mysql/mysql-bin.log # 啟用二進(jìn)制日志 binlog_format = ROW # 推薦使用 ROW 格式 expire_logs_days = 7 # 自動清理 7 天前的日志 max_binlog_size = 100M # 每個(gè)二進(jìn)制日志文件的最大大小 # 雙主復(fù)制配置 log_slave_updates = 1 # 從庫記錄二進(jìn)制日志(必須啟用) auto_increment_increment = 2 # 自增步長 auto_increment_offset = 1 # 自增起始值(實(shí)例 1 為 1,實(shí)例 2 為 2) # 復(fù)制過濾(可選) replicate-do-db = my_database # 只復(fù)制指定的數(shù)據(jù)庫 replicate-ignore-db = mysql # 忽略系統(tǒng)庫 # 其他配置 bind-address = 0.0.0.0 # 允許遠(yuǎn)程連接
重啟MySQL
[root@QYWLAQ_VMC4234 ~]# systemctl restart mysqld
實(shí)例二:172.25.0.20:
# 此配置文件中追加下述配置 [root@QYWLAQ_VMC1091 ~]# vi /etc/my.cnf [mysqld] # 基本配置 server-id = 2 # 每個(gè)實(shí)例的 server-id 必須唯一 log_bin = /var/log/mysql/mysql-bin.log # 啟用二進(jìn)制日志 binlog_format = ROW # 推薦使用 ROW 格式 expire_logs_days = 7 # 自動清理 7 天前的日志 max_binlog_size = 100M # 每個(gè)二進(jìn)制日志文件的最大大小 # 雙主復(fù)制配置 log_slave_updates = 1 # 從庫記錄二進(jìn)制日志(必須啟用) auto_increment_increment = 2 # 自增步長 auto_increment_offset = 2 # 自增起始值(實(shí)例 1 為 1,實(shí)例 2 為 2) # 復(fù)制過濾(可選) replicate-do-db = my_database # 只復(fù)制指定的數(shù)據(jù)庫 replicate-ignore-db = mysql # 忽略系統(tǒng)庫 # 其他配置 bind-address = 0.0.0.0 # 允許遠(yuǎn)程連接
重啟MySQL
[root@QYWLAQ_VMC1091 ~]# systemctl restart mysqld
配置復(fù)制用戶
在每個(gè) MySQL 實(shí)例上創(chuàng)建一個(gè)用于復(fù)制的用戶。
在實(shí)例 1 (172.25.0.19)上執(zhí)行:
mysql> CREATE USER 'replication'@'172.25.0.20' IDENTIFIED BY '@2X0wZY/rq'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'172.25.0.20'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec)
查詢用戶
mysql> SELECT User, Host FROM mysql.user; +---------------+-------------+ | User | Host | +---------------+-------------+ | replication | 172.25.0.20 | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+-------------+ 4 rows in set (0.00 sec)
在實(shí)例 2 (172.25.0.20)上執(zhí)行:
mysql> CREATE USER 'replication'@'172.25.0.19' IDENTIFIED BY '@2X0wZY/rq'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'172.25.0.19'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
查詢用戶
mysql> SELECT User, Host FROM mysql.user; +---------------+-------------+ | User | Host | +---------------+-------------+ | replication | 172.25.0.19 | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+-------------+ 4 rows in set (0.00 sec)
配置主從復(fù)制
在每個(gè)實(shí)例上配置對方為主庫。
查看實(shí)例 2 節(jié)點(diǎn)狀態(tài):
mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 964 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
在實(shí)例 1 上執(zhí)行:
CHANGE MASTER TO MASTER_HOST='172.25.0.20', MASTER_USER='replication', MASTER_PASSWORD='@2X0wZY/rq', MASTER_LOG_FILE='mysql-bin.000002', -- 替換為實(shí)例 2 的當(dāng)前 binlog 文件 MASTER_LOG_POS=1784; -- 替換為實(shí)例 2 的當(dāng)前 binlog 位置 START SLAVE;
查看實(shí)例 1 節(jié)點(diǎn)狀態(tài):
mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 964 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
在實(shí)例 2 上執(zhí)行:
CHANGE MASTER TO MASTER_HOST='172.25.0.19', MASTER_USER='replication', MASTER_PASSWORD='@2X0wZY/rq', MASTER_LOG_FILE='mysql-bin.000002', -- 替換為實(shí)例 1 的當(dāng)前 binlog 文件 MASTER_LOG_POS=1784; -- 替換為實(shí)例 1 的當(dāng)前 binlog 位置 START SLAVE;
驗(yàn)證復(fù)制狀態(tài)
在每個(gè)實(shí)例上執(zhí)行以下命令,檢查復(fù)制狀態(tài):
SHOW SLAVE STATUS\G;
確保以下字段的值為 Yes
:
Slave_IO_Running
Slave_SQL_Running
測試雙主復(fù)制
在 Server A 上操作:
創(chuàng)建測試數(shù)據(jù)庫和表:
CREATE DATABASE test_db; USE test_db; CREATE TABLE test_table ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) ) ENGINE=InnoDB;
插入數(shù)據(jù):
INSERT INTO test_table (name) VALUES ('Server A Data');
在 Server B 上操作:
檢查數(shù)據(jù)是否同步:
USE test_db; SELECT * FROM test_table;
應(yīng)該能看到 Server A Data
。
插入數(shù)據(jù):
INSERT INTO test_table (name) VALUES ('Server B Data');
回到 Server A 上操作:
檢查數(shù)據(jù)是否同步:
USE test_db; SELECT * FROM test_table;
table (name) VALUES (‘Server A Data');
在 Server B 上操作:
檢查數(shù)據(jù)是否同步:
USE test_db; SELECT * FROM test_table;
應(yīng)該能看到 Server A Data。
插入數(shù)據(jù):
INSERT INTO test_table (name) VALUES (‘Server B Data');
回到 Server A 上操作:
檢查數(shù)據(jù)是否同步:
USE test_db; SELECT * FROM test_table;
應(yīng)該能看到 Server B Data。
到此這篇關(guān)于MySQL5.7.35雙主搭建的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MySQL雙主搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MYSQL數(shù)據(jù)庫查詢按日期分組統(tǒng)計(jì)詳細(xì)代碼
這篇文章主要給大家介紹了關(guān)于MYSQL數(shù)據(jù)庫查詢按日期分組統(tǒng)計(jì)的相關(guān)資料,按日期分組是指按照時(shí)間維度對數(shù)據(jù)進(jìn)行分類匯總統(tǒng)計(jì),常用于查詢分析具有時(shí)間屬性的數(shù)據(jù),例如訂單量、用戶活躍等,需要的朋友可以參考下2024-01-01MySQL中的GROUP_CONCAT()函數(shù)詳解與實(shí)戰(zhàn)應(yīng)用小結(jié)(示例詳解)
本文介紹了MySQL中的GROUP_CONCAT()函數(shù),詳細(xì)解釋了其基本語法、應(yīng)用示例以及ORDERBY和SEPARATOR參數(shù)的使用方法,此外,還提到了該函數(shù)的性能限制和注意事項(xiàng),感興趣的朋友一起看看吧2025-02-02Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)代碼
本文給大家介紹Unity連接MySQL并讀取表格數(shù)據(jù)的實(shí)現(xiàn)代碼,實(shí)例化的同時(shí)調(diào)用MySqlConnection,傳入?yún)?shù),這里的傳入?yún)?shù)個(gè)人認(rèn)為是CMD里面的直接輸入了,string格式直接類似手敲到cmd里面,完整代碼參考下本文2021-06-06mysql多表join時(shí)候update更新數(shù)據(jù)的方法
如果item表的name字段為''就用resource_library 表的resource_name字段前面加上字符串Review更新它,他們的關(guān)聯(lián)關(guān)系在表resource_review_link中。2011-03-03