MySQL5.7.35雙主搭建的實現(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 # 啟動并設置開啟自啟 [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; -- 刷新權限以使修改立即生效 Query OK, 0 rows affected (0.01 sec)
雙主復制的配置
實例一:172.25.0.19:
# 此配置文件中追加下述配置 [root@QYWLAQ_VMC4234 ~]# vi /etc/my.cnf [mysqld] # 基本配置 server-id = 1 # 每個實例的 server-id 必須唯一 log_bin = /var/log/mysql/mysql-bin.log # 啟用二進制日志 binlog_format = ROW # 推薦使用 ROW 格式 expire_logs_days = 7 # 自動清理 7 天前的日志 max_binlog_size = 100M # 每個二進制日志文件的最大大小 # 雙主復制配置 log_slave_updates = 1 # 從庫記錄二進制日志(必須啟用) auto_increment_increment = 2 # 自增步長 auto_increment_offset = 1 # 自增起始值(實例 1 為 1,實例 2 為 2) # 復制過濾(可選) replicate-do-db = my_database # 只復制指定的數(shù)據(jù)庫 replicate-ignore-db = mysql # 忽略系統(tǒng)庫 # 其他配置 bind-address = 0.0.0.0 # 允許遠程連接
重啟MySQL
[root@QYWLAQ_VMC4234 ~]# systemctl restart mysqld
實例二:172.25.0.20:
# 此配置文件中追加下述配置 [root@QYWLAQ_VMC1091 ~]# vi /etc/my.cnf [mysqld] # 基本配置 server-id = 2 # 每個實例的 server-id 必須唯一 log_bin = /var/log/mysql/mysql-bin.log # 啟用二進制日志 binlog_format = ROW # 推薦使用 ROW 格式 expire_logs_days = 7 # 自動清理 7 天前的日志 max_binlog_size = 100M # 每個二進制日志文件的最大大小 # 雙主復制配置 log_slave_updates = 1 # 從庫記錄二進制日志(必須啟用) auto_increment_increment = 2 # 自增步長 auto_increment_offset = 2 # 自增起始值(實例 1 為 1,實例 2 為 2) # 復制過濾(可選) replicate-do-db = my_database # 只復制指定的數(shù)據(jù)庫 replicate-ignore-db = mysql # 忽略系統(tǒng)庫 # 其他配置 bind-address = 0.0.0.0 # 允許遠程連接
重啟MySQL
[root@QYWLAQ_VMC1091 ~]# systemctl restart mysqld
配置復制用戶
在每個 MySQL 實例上創(chuàng)建一個用于復制的用戶。
在實例 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)
在實例 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)
配置主從復制
在每個實例上配置對方為主庫。
查看實例 2 節(jié)點狀態(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)
在實例 1 上執(zhí)行:
CHANGE MASTER TO MASTER_HOST='172.25.0.20', MASTER_USER='replication', MASTER_PASSWORD='@2X0wZY/rq', MASTER_LOG_FILE='mysql-bin.000002', -- 替換為實例 2 的當前 binlog 文件 MASTER_LOG_POS=1784; -- 替換為實例 2 的當前 binlog 位置 START SLAVE;
查看實例 1 節(jié)點狀態(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)
在實例 2 上執(zhí)行:
CHANGE MASTER TO MASTER_HOST='172.25.0.19', MASTER_USER='replication', MASTER_PASSWORD='@2X0wZY/rq', MASTER_LOG_FILE='mysql-bin.000002', -- 替換為實例 1 的當前 binlog 文件 MASTER_LOG_POS=1784; -- 替換為實例 1 的當前 binlog 位置 START SLAVE;
驗證復制狀態(tài)
在每個實例上執(zhí)行以下命令,檢查復制狀態(tài):
SHOW SLAVE STATUS\G;
確保以下字段的值為 Yes
:
Slave_IO_Running
Slave_SQL_Running
測試雙主復制
在 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;
應該能看到 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;
應該能看到 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;
應該能看到 Server B Data。
到此這篇關于MySQL5.7.35雙主搭建的實現(xiàn)的文章就介紹到這了,更多相關MySQL雙主搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MYSQL數(shù)據(jù)庫查詢按日期分組統(tǒng)計詳細代碼
這篇文章主要給大家介紹了關于MYSQL數(shù)據(jù)庫查詢按日期分組統(tǒng)計的相關資料,按日期分組是指按照時間維度對數(shù)據(jù)進行分類匯總統(tǒng)計,常用于查詢分析具有時間屬性的數(shù)據(jù),例如訂單量、用戶活躍等,需要的朋友可以參考下2024-01-01MySQL中的GROUP_CONCAT()函數(shù)詳解與實戰(zhàn)應用小結(示例詳解)
本文介紹了MySQL中的GROUP_CONCAT()函數(shù),詳細解釋了其基本語法、應用示例以及ORDERBY和SEPARATOR參數(shù)的使用方法,此外,還提到了該函數(shù)的性能限制和注意事項,感興趣的朋友一起看看吧2025-02-02Unity連接MySQL并讀取表格數(shù)據(jù)的實現(xiàn)代碼
本文給大家介紹Unity連接MySQL并讀取表格數(shù)據(jù)的實現(xiàn)代碼,實例化的同時調(diào)用MySqlConnection,傳入?yún)?shù),這里的傳入?yún)?shù)個人認為是CMD里面的直接輸入了,string格式直接類似手敲到cmd里面,完整代碼參考下本文2021-06-06mysql多表join時候update更新數(shù)據(jù)的方法
如果item表的name字段為''就用resource_library 表的resource_name字段前面加上字符串Review更新它,他們的關聯(lián)關系在表resource_review_link中。2011-03-03