Mycat中介軟體實現Mysql主從讀寫分離
環境規劃:
IP地址 | 主機名 | 角色 | 備註 |
10.4.132.50 | k8s01 | mycat,master |
|
10.4.132.42 | k8s02 | slave |
|
10.4.132.66 | k8s03 | slave |
|
Mycat下載地址:
Mysql下載地址:
Mycal管理叢集埠:[root@k8s01 conf]# mysql -h 127.0.0.1 -u root -p123456 -P 9066
Mycat資料埠:[root@k8s01 conf]# mysql -h 127.0.0.1 -u root -p123456 -P 8066
1.下載安裝mysql(1臺master節點和2臺slave節點)
[root@k8s01 soft]# wget
[root@k8s01 soft]# tar xvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@k8s01 soft]# cd /usr/local/
[root@k8s01 local]# mv mysql-5.7.27-linux-glibc2.12-x86_64/ mysql-5.7.27
[root@k8s01 local]# chown -R root:root mysql-5.7.27/
[root@k8s01 local]# cd mysql-5.7.27/
[root@k8s01 mysql-5.7.27]# mkdir data
[root@k8s01 mysql-5.7.27]# useradd -r -M -s /bin/nologin mysql
[root@k8s01 mysql-5.7.27]# chown -R mysql:mysql data/
[root@k8s01 mysql-5.7.27]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7.27 --datadir=/usr/local/mysql-5.7.27/data
2019-11-02T04:24:41.908404Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-11-02T04:24:46.687678Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-11-02T04:24:47.428823Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-11-02T04:24:47.487404Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b42cef88-fd28-11e9-a5cc-000c29ee86d5.
2019-11-02T04:24:47.488204Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-11-02T04:24:47.612739Z 1 [Note] A temporary password is generated for root@localhost: 3m;5yQ_7T#jc --登陸密碼
[root@k8s01 mysql-5.7.27]# cp -a support-files/mysql.server /etc/init.d/mysqld
[root@k8s01 mysql-5.7.27]# chkconfig --add mysqld
[root@k8s01 mysql-5.7.27]# chkconfig mysqld on
[root@k8s01 mysql-5.7.27]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql-5.7.27
datadir=/usr/local/mysql-5.7.27/data
[root@k8s01 mysql-5.7.27]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql-5.7.27
datadir=/usr/local/mysql-5.7.27/data
socket=/tmp/mysql.sock
symbolic-links=0
server_id=10
binlog_format=ROW
max_binlog_size=2G
sync_binlog=1
binlog_cache_size=64M
log_bin=bin-log
log_bin_index=bin-index
[mysqld_safe]
log-error=/usr/local/mysql-5.7.27/data/mariadb.log
pid-file=/usr/local/mysql-5.7.27/data/mariadb.pid
[root@k8s01 mysql-5.7.27]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL.Logging to '/usr/local/mysql-5.7.27/data/mariadb.log'.
... SUCCESS!
[root@k8s01 mysql-5.7.27]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql-5.7.27/bin
[root@k8s01 mysql-5.7.27]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password=password('System135');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@k8s01 mysql-5.7.27]# mysql -u root -pSystem135
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 6
Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
2.master節點和slave節點做主從
master節點:
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 39
Server version: 5.7.27-log MySQL Community Server (GPL)
affiliates. Other names may be trademarks of their respective owners.
+----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| bin-log.000002 | 3093 | | | |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3.兩個slave節點(兩個slave節點都要連線到master節點)
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.27 MySQL Community Server (GPL)
affiliates. Other names may be trademarks of their respective owners.
Query OK, 0 rows affected, 2 warnings (0.08 sec)
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.4.132.50
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: bin-log.000002
Read_Master_Log_Pos: 3093
Relay_Log_File: k8s02-relay-bin.000002
Relay_Log_Pos: 318
Relay_Master_Log_File: bin-log.000002
Slave_IO_Running:
Yes
Slave_SQL_Running:
Yes
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
4.驗證主從資料是否同步
master節點:
mysql> system hostname
k8s01
mysql> create database wuhan charset utf8;
Query OK, 1 row affected (0.00 sec)
Database changed
mysql> create table t1 (a int);
Query OK, 0 rows affected (0.01 sec)
Query OK, 1 row affected (0.00 sec)
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
slave1節點:
mysql> system hostname
k8s02
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> select * from t1;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql>
slave2節點:
k8s03
mysql> use wuhan
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> select * from t1;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
5.下載安裝配置Mycat(master節點)
[root@k8s01
soft]# rpm -ivh jdk-8u221-linux-x64.rpm
warning: jdk-8u221-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jdk1.8-2000:1.8.0_221-fcs ################################# [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...
[root@k8s01 soft]# tar xvf Mycat-server-1.6.7.3-release-20190828135747-linux.tar.gz -C /usr/local/
[root@k8s01 soft]# cd /usr/local/mycat/conf/
[root@k8s01 conf]# vim schema.xml
配置講解:
schema name="wuhan" --需要做讀取寫分離的庫
checkSQLschema="true" --執行sql時是否去掉schema名
sqlMaxLimit="100" --如果sql語句沒有加limit限制,此時預設值是100
<table name="t1" --指定讀寫分離的表
dataNode="dn1" --資料節點
<dataNode name="dn1" --對應上面的資料節點(任意起)
dataHost="10.4.132.50" --資料主機名(任意起)
database="wuhan" --庫名
<dataHost name="10.4.132.50" --對應以上值
balance="0" --不開啟讀寫分離機制,所有操作都在master上。1 所有讀操作都在slave節點上。 2 所有讀操作都隨機在master和slave節點上。 3 所有讀操作都傳送到slave節點,master節點只負責寫。
<heartbeat>select user()</heartbeat> --心跳探測
<writeHost host="W_k8s01" url="10.4.132.50:3306" user="repl" password="123456"> --後端主機(使用者名稱和密碼是mycat伺服器可以登陸後面mysql的許可權)
<schema name="hubei" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"> --不寫table name關鍵字,表示全庫所有表做讀寫分離。
[root@k8s01 conf]# vim server.xml
<property name="password">123456</property> --mycat登陸帳號(密碼可以隨意指定)
<property name="schemas">wuhan</property> --讀寫分離的庫名
[root@k8s01 logs]# ../bin/mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
[root@k8s01 logs]#
6.檢視各節點的狀態
7.測試mycat實現的讀寫分離(登陸一次查詢後要退出,再登陸查詢)
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.6.29-mycat-1.6.7.3-release-20190828215749 MyCat Server (OpenCloudDB)
affiliates. Other names may be trademarks of their respective
owners.
Database changed
mysql> select * from t1;
+------+
| a |
+------+
| 1 |
+------+
1 rows in set (0.08 sec)
Bye
[root@k8s01 conf]# mysql -h 127.0.0.1 -u root -p123456 -P 8066 -A
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 3
Server version: 5.6.29-mycat-1.6.7.3-release-20190828215749 MyCat Server (OpenCloudDB)
affiliates. Other names may be trademarks of their respective
owners.
Database changed
mysql> select * from t1;
+------+
| a |
+------+
| 1 |
+------+
1 rows in set (0.00 sec)
日誌檢視結果:
可重複登陸查詢,寫入資料結果(必須退出會話再登陸查詢):
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25854343/viewspace-2662382/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Mycat中介軟體實現Percona Cluster讀寫分離
- 高可用Mysql架構_Mysql主從複製、Mysql雙主熱備、Mysql雙主雙從、Mysql讀寫分離(Mycat中介軟體)、Mysql分庫分表架構(Mycat中介軟體)的演變MySql架構
- MySQL 中介軟體Atlas 實現讀寫分離MySql
- 使用proxysql 1.4.14中介軟體實現mysql 5.7.26主從的讀寫分離MySql
- mycat結合MySQL的雙主實現讀寫分離MySql
- MySQL cetus 中介軟體 讀寫分離MySql
- MysqL讀寫分離的實現-Mysql proxy中介軟體的使用MySql
- 搭建MySQL主從實現Django讀寫分離MySqlDjango
- Mycat實現mysql的負載均衡讀寫分離MySql負載
- MySQL怎麼實現主從同步和Django實現MySQL讀寫分離MySql主從同步Django
- MySQL中介軟體之ProxySQL(3):初試讀寫分離MySql
- MYSQL 主從 + ATLAS 讀寫分離 搭建MySql
- 配置\清除 MySQL 主從 讀寫分離MySql
- MySQL主從複製讀寫分離MySql
- Mycat讀寫分離配置實踐
- MySQL主從分離實現MySql
- Mycat讀寫分離、主從切換、分庫分表的操作記錄
- 資料庫中介軟體sharding-jdbc實現讀寫分離資料庫JDBC
- MyCat 讀寫分離 資料庫分庫分表 中介軟體 安裝部署,及簡單使用資料庫
- Mycat中介軟體實現Mysql資料分片( 下篇)MySql
- Mycat中介軟體實現Mysql資料分片(上篇)MySql
- MySQL主從複製與讀寫分離MySql
- [Mysql]主從複製和讀寫分離MySql
- haproxy+keepalived+mycat+mysql (讀寫分離)MySql
- CentOS7系統下使用Mycat實現mysql讀寫分離部署CentOSMySql
- MyCat分庫分表、讀寫分離
- Mycat 讀寫分離+分庫分表
- Mysql-主從複製與讀寫分離MySql
- ProxySQL實現MySQL讀寫分離MySql
- Amoeba 實現 MySQL 讀寫分離MySql
- 資料庫讀寫分離,主從同步實現方法資料庫主從同步
- springboot+mybatis+druid實現mysql主從讀寫分離(五)Spring BootMyBatisUIMySql
- 【Mycat】Mycat讀寫分離與負載均衡2負載
- MySQL主從同步讀寫分離的叢集配置MySql主從同步
- Amoeba+Mysql 實現讀寫分離MySql
- mysql-proxy實現讀寫分離MySql
- MySQL從庫卡主了--讀寫分離也不能亂讀MySql
- MySQL運維15-一主一從讀寫分離MySql運維