1 案例1:部署mycat服務(wù)
1.1 問題
- 把主機(jī)mysql60 配置為 MySQL59 的從服務(wù)器
- 把主機(jī)mysql62 配置為 MySQL61 的從服務(wù)器
- 把主機(jī)mycat63 配置為mycat服務(wù)器
- 客戶端192.168.88.50訪問mycat服務(wù)
1.2 方案
準(zhǔn)備6臺虛擬機(jī),具體配置如表-1
1.3 步驟
實現(xiàn)此案例需要按照如下步驟進(jìn)行。
步驟一:把MySQL60配置為MySQL59的從服務(wù)器
1)配置主服務(wù)器MySQL59
//啟用binlog日志
[root@mysql59 ~]# yum –y install mysql-server mysql
[root@mysql59 ~]# systemctl start mysqld
[root@mysql59 ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=59
log-bin=mysql59
:wq
[root@mysql59 ~]# systemctl restart mysqld
//用戶授權(quán)
[root@mysql59 ~]# mysql
mysql> create user repluser@"%" identified by "123qqq...A"; 創(chuàng)建用戶
Query OK, 0 rows affected (0.11 sec)
mysql> grant replication slave on *.* to repluser@"%"; 授予權(quán)限
Query OK, 0 rows affected (0.09 sec)
//查看日志信息
mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| mysql59.000001 | 667 | | | |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2)配置slave服務(wù)器MySQL60
//指定server-id 并重啟數(shù)據(jù)庫服務(wù)
[root@mysql60 ~]# yum –y install mysql-serv er mysql
[root@mysql60 ~]# systemctl start mysqld
[root@mysql60 ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=60
:wq
[root@mysql60 ~]# systemctl restart mysqld
//登陸服務(wù)指定主服務(wù)器信息
[root@mysql60 ~]# mysql
mysql> change master to master_host="192.168.88.59" , master_user="repluser" , master_password="123qqq...A" ,master_log_file="mysql59.000001" , master_log_pos=667;
Query OK, 0 rows affected, 8 warnings (0.34 sec)
//啟動slave進(jìn)程
mysql> start slave ;
Query OK, 0 rows affected, 1 warning (0.04 sec)
//查看狀態(tài)信息
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.88.59
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql59.000001
Read_Master_Log_Pos: 667
Relay_Log_File: mysql60-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql59.000001
Slave_IO_Running: Yes //IO線程
Slave_SQL_Running: Yes //SQL線程
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_Master_Log_Pos: 667
Relay_Log_Space: 533
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 59
Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
mysql>
步驟二:把MySQL62配置為MySQL61的從服務(wù)器
1)配置主服務(wù)器MySQL61
//啟用binlog日志
[root@mysql61 ~]# yum –y install mysql-server mysql
[root@mysql61 ~]# systemctl start mysqld
[root@mysql61 ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=61
log-bin=mysql61
:wq
[root@mysql61 ~]# systemctl restart mysqld
//用戶授權(quán)
[root@mysql61 ~]# mysql
mysql> create user repluser@"%" identified by "123qqq...A"; 創(chuàng)建用戶
Query OK, 0 rows affected (0.11 sec)
mysql> grant replication slave on *.* to repluser@"%"; 授予權(quán)限
Query OK, 0 rows affected (0.09 sec)
//查看日志信息
mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| mysql61.000001 | 667 | | | |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2)配置slave服務(wù)器MySQL62
//指定server-id 并重啟數(shù)據(jù)庫服務(wù)
[root@mysql62 ~]# yum –y install mysql-server mysql
[root@mysql62 ~]# systemctl start mysqld
[root@mysql62 ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=62
:wq
[root@mysql62 ~]# systemctl restart mysqld
//登陸服務(wù)指定主服務(wù)器信息
[root@mysql62 ~]# mysql
mysql> change master to master_host="192.168.88.61" , master_user="repluser" , master_password="123qqq...A" ,master_log_file="mysql61.000001" , master_log_pos=667;
Query OK, 0 rows affected, 8 warnings (0.34 sec)
//啟動slave進(jìn)程
mysql> start slave ;
Query OK, 0 rows affected, 1 warning (0.04 sec)
//查看狀態(tài)信息
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.88.61
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql61.000001
Read_Master_Log_Pos: 667
Relay_Log_File: mysql62-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql61.000001
Slave_IO_Running: Yes //IO線程
Slave_SQL_Running: Yes //SQL線程
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_Master_Log_Pos: 667
Relay_Log_Space: 533
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 61
Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
mysql>
步驟三:把主機(jī)mycat63配置為mycat服務(wù)器。
1)拷貝軟件到mycat63主機(jī)
[root@server1 ~]# scp /linux-soft/s3/mycat2-1.21-release-jar-with-dependencies.jar root@192.168.88.63:/root/
[root@server1 ~]# scp /linux-soft/s3/mycat2-install-template-1.21.zip root@192.168.88.63:/root/
2)安裝mycat軟件
//安裝jdk
[root@mycat63 ~]# yum -y install java-1.8.0-openjdk.x86_64
//安裝mycat
[root@mycat63 ~]# which unzip || yum -y install unzip
[root@mycat63 ~]# unzip mycat2-install-template-1.21.zip
[root@mycat63 ~]# mv mycat /usr/local/
//安裝依賴
[root@mycat63 ~]# cp mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/
//修改權(quán)限
[root@mycat63 ~]# chmod -R 777 /usr/local/mycat/
3)定義客戶端連接時使用的用戶:
[root@mycat63 ~]# vim /usr/local/mycat/conf/users/root.user.json
{
"dialect":"mysql",
"ip":null,
"password":"654321",
"transactionType":"proxy",
"username":"mycat"
}
:wq
定義連接的數(shù)據(jù)庫服務(wù)?
[root@mycat63 ~]# vim /usr/local/mycat/conf/datasources/prototypeDs.data
{
"dbType":"mysql",
"idleTimeout":60000,
"initSqls":[],
"initSqlsGetConnection":true,
"instanceType":"READ_WRITE",
"maxCon":1000,
"maxConnectTimeout":3000,
"maxRetryCount":5,
"minCon":1,
"name":"prototypeDs",
"password":"123456", 密碼
"type":"JDBC",
"url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", 連接本機(jī)的數(shù)據(jù)庫服務(wù)
"user":"plj", 用戶名
"weight":0
}
:wq
5)在mycat63主機(jī)運(yùn)行數(shù)據(jù)庫服務(wù)
[root@mycat63 ~]# yum -y install mysql-server mysql
[root@mycat63 ~]# systemctl start mysqld
//創(chuàng)建plj用戶
[root@mycat63 ~]# mysql
mysql> create user plj@"%" identified by "123456"; 創(chuàng)建用戶
Query OK, 0 rows affected (0.05 sec)
mysql> grant all on *.* to plj@"%" ; 授予權(quán)限
Query OK, 0 rows affected (0.39 sec)
mysql> exit
Bye
[root@mycat63 ~]#
6)啟動mycat服務(wù)
[root@mycat63 ~]# /usr/local/mycat/bin/mycat help
Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
[root@mycat63 ~]# /usr/local/mycat/bin/mycat start
Starting mycat2...
//半分鐘左右 能看到端口
[root@mycat63 ~]# netstat -utnlp | grep 8066
tcp6 0 0 :::8066 :::* LISTEN 57015/java
[root@mycat63 ~]#
步驟四:連接mycat服務(wù)器
1)連接本機(jī)的mycat服務(wù)
[root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
mysql> show databases;
+--------------------+
| `Database` |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.11 sec)
Mysql>
步驟五:添加數(shù)據(jù)源
1)連接本機(jī)的mycat服務(wù),添加數(shù)據(jù)源
[root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
//添加MySQL59
MySQL>/*+ mycat:createdatasource{
"name":"dw0", "url":"jdbc:mysql://192.168.88.59:3306","user":"plj","password":"123456"}*/;
//添加MySQL60
Mysql>/*+ mycat:createdatasource{
"name":"dr0", "url":"jdbc:mysql://192.168.88.60:3306","user":"plj","password":"123456"}*/;
//添加MySQL61
Mysql>/*+ mycat:createdatasource{
"name":"dw1", "url":"jdbc:mysql://192.168.88.61:3306","user":"plj","password":"123456"}*/;
//添加MySQL62
Mysql>/*+ mycat:createdatasource{
"name":"dr1", "url":"jdbc:mysql://192.168.88.62:3306","user":"plj","password":"123456"}*/;
Mysql>
?2)查看存放目錄
[root@mycat63 ~]# ls /usr/local/mycat/conf/datasources/
dr0.datasource.json dr1.datasource.json dw0.datasource.json dw1.datasource.json prototypeDs.datasource.json
?3)查看數(shù)據(jù)信息
mysql> /*+mycat:showDataSources{}*/ \G
*************************** 1. row ***************************
NAME: dw0
USERNAME: plj
PASSWORD: 123456
MAX_CON: 1000
MIN_CON: 1
EXIST_CON: 0
USE_CON: 0
MAX_RETRY_COUNT: 5
MAX_CONNECT_TIMEOUT: 30000
DB_TYPE: mysql
URL: jdbc:mysql://192.168.88.59:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
WEIGHT: 0
INIT_SQL:
INIT_SQL_GET_CONNECTION: true
INSTANCE_TYPE: READ_WRITE
IDLE_TIMEOUT: 60000
DRIVER: {
CreateTime:"2023-05-08 16:10:26",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
TYPE: JDBC
IS_MYSQL: true
*************************** 2. row ***************************
NAME: dw1
USERNAME: plj
PASSWORD: 123456
MAX_CON: 1000
MIN_CON: 1
EXIST_CON: 0
USE_CON: 0
MAX_RETRY_COUNT: 5
MAX_CONNECT_TIMEOUT: 30000
DB_TYPE: mysql
URL: jdbc:mysql://192.168.88.61:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
WEIGHT: 0
INIT_SQL:
INIT_SQL_GET_CONNECTION: true
INSTANCE_TYPE: READ_WRITE
IDLE_TIMEOUT: 60000
DRIVER: {
CreateTime:"2023-05-08 16:10:26",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
TYPE: JDBC
IS_MYSQL: true
*************************** 3. row ***************************
NAME: dr0
USERNAME: plj
PASSWORD: 123456
MAX_CON: 1000
MIN_CON: 1
EXIST_CON: 0
USE_CON: 0
MAX_RETRY_COUNT: 5
MAX_CONNECT_TIMEOUT: 30000
DB_TYPE: mysql
URL: jdbc:mysql://192.168.88.61:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
WEIGHT: 0
INIT_SQL:
INIT_SQL_GET_CONNECTION: true
INSTANCE_TYPE: READ_WRITE
IDLE_TIMEOUT: 60000
DRIVER: {
CreateTime:"2023-05-08 16:10:26",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
TYPE: JDBC
IS_MYSQL: true
*************************** 4. row ***************************
NAME: dr1
USERNAME: plj
PASSWORD: 123456
MAX_CON: 1000
MIN_CON: 1
EXIST_CON: 0
USE_CON: 0
MAX_RETRY_COUNT: 5
MAX_CONNECT_TIMEOUT: 30000
DB_TYPE: mysql
URL: jdbc:mysql://192.168.88.62:3306?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&autoReconnect=true
WEIGHT: 0
INIT_SQL:
INIT_SQL_GET_CONNECTION: true
INSTANCE_TYPE: READ_WRITE
IDLE_TIMEOUT: 60000
DRIVER: {
CreateTime:"2023-05-08 16:10:26",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
TYPE: JDBC
IS_MYSQL: true
*************************** 5. row ***************************
NAME: prototypeDs
USERNAME: plj
PASSWORD: 123456
MAX_CON: 1000
MIN_CON: 1
EXIST_CON: 0
USE_CON: 0
MAX_RETRY_COUNT: 5
MAX_CONNECT_TIMEOUT: 3000
DB_TYPE: mysql
URL: jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
WEIGHT: 0
INIT_SQL:
INIT_SQL_GET_CONNECTION: true
INSTANCE_TYPE: READ_WRITE
IDLE_TIMEOUT: 60000
DRIVER: {
CreateTime:"2023-05-08 16:10:26",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
TYPE: JDBC
IS_MYSQL: true
5 rows in set (0.07 sec)
mysql>
步驟六:配置數(shù)據(jù)庫服務(wù)器
1)在主服務(wù)器添加plj用戶
[root@mysql59 ~]# mysql
mysql> create user plj@"%" identified by "123456";
Mysql> grant all on *.* to plj@"%";
[root@mysql61 ~]# mysql
mysql> create user plj@"%" identified by "123456";
Mysql> grant all on *.* to plj@"%";
?2)在從服務(wù)器查看用戶是否同步
[root@mysql60 ~]# mysql -e 'select user from mysql.user where user="plj"'
+------+
| user |
+------+
| plj |
+------+
[root@mysql60 ~]#
[root@mysql62 ~]# mysql -e 'select user from mysql.user where user="plj"'
+------+
| user |
+------+
| plj |
+------+
[root@host62 ~]#
步驟七:創(chuàng)建集群
1)連接本機(jī)的mycat服務(wù),創(chuàng)建集群
[root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
mysql> /*!mycat:createcluster{"name":"c0","masters":["dw0"],"replicas":["dr0"]}*/;
mysql> /*!mycat:createcluster{"name":"c1","masters":["dw1"],"replicas":["dr1"]}*/;
Mysql>
2)創(chuàng)建的集群保存在mycat安裝目錄下
[root@MySQL63 ~]# ls /usr/local/mycat/conf/clusters/
c0.cluster.json c1.cluster.json prototype.cluster.json
[root@mycat63 ~]#
3)查看集群信息
mysql> /*+ mycat:showClusters{}*/ \G
*************************** 1. row ***************************
NAME: prototype
SWITCH_TYPE: SWITCH
MAX_REQUEST_COUNT: 200
TYPE: BALANCE_ALL
WRITE_DS: prototypeDs
READ_DS: prototypeDs
WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
AVAILABLE: true
*************************** 2. row ***************************
NAME: c0
SWITCH_TYPE: SWITCH
MAX_REQUEST_COUNT: 2000
TYPE: BALANCE_ALL
WRITE_DS: dw0
READ_DS: dw0,dr0
WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
AVAILABLE: true
*************************** 3. row ***************************
NAME: c1
SWITCH_TYPE: SWITCH
MAX_REQUEST_COUNT: 2000
TYPE: BALANCE_ALL
WRITE_DS: dw1
READ_DS: dw1,dr1
WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
AVAILABLE: true
3 rows in set (0.03 sec)
mysql>
2 案例2:測試配置
2.1 問題
- 練習(xí)全局表
- 練習(xí)分片表
- 練習(xí)ER表
2.2 方案
在客戶端client50 連接mycat63 存儲數(shù)據(jù) ,驗證mycat63的配置
2.3 步驟
實現(xiàn)此案例需要按照如下步驟進(jìn)行。
步驟一:練習(xí)全局表
全局表 數(shù)據(jù)會插入到兩個庫中,并且兩個庫中都有全部的數(shù)據(jù)。
//在mycat63 連接本機(jī)的mycat服務(wù)建庫
[root@mycat63 ~]# mysql -h127.0.0.1 -umycat -p654321 -P8066
mysql> create database tarena;
Query OK, 0 rows affected (0.31 sec)
mysql> exit
Bye
[root@mycat63 ~]#
?配置文件存放位置
[root@mycat63 ~]# ls /usr/local/mycat/conf/schemas/tarena.schema.json
/usr/local/mycat/conf/schemas/tarena.schema.json
[root@mycat63 ~]#
創(chuàng)建全局表
//客戶端client50 連接mycat63主機(jī)的 建表存儲數(shù)據(jù)
[root@client50 ~]# mysql -h192.168.88.63 -umycat -p654321 -P8066
mysql> create table tarena.dept(dept_id int , dept_name char(10),primary key(dept_id)) default charset utf8 broadcast;
Query OK, 0 rows affected (4.46 sec)
//插入記錄
mysql> insert into tarena.dept values(1,"開發(fā)部"),(2,"運(yùn)維部"),(3,"測試部");
Query OK, 1 row affected (0.23 sec)
//查看記錄
mysql> select * from tarena.dept;
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| 1 | 開發(fā)部 |
| 2 | 運(yùn)維部 |
| 3 | 測試部 |
+---------+-----------+
3 rows in set (0.33 sec)
mysql>
?在4臺數(shù)據(jù)庫服務(wù)器查看
[root@mysql59 ~]# mysql -e 'select * from tarena.dept'
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| 1 | 開發(fā)部 |
| 2 | 運(yùn)維部 |
| 3 | 測試部 |
+---------+-----------+
[root@host61 ~]#
[root@mysql60 ~]# mysql -e 'select * from tarena.dept'
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| 1 | 開發(fā)部 |
| 2 | 運(yùn)維部 |
| 3 | 測試部 |
+---------+-----------+
[root@host62 ~]#
[root@mysql61 ~]# mysql -e 'select * from tarena.dept'
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| 1 | 開發(fā)部 |
| 2 | 運(yùn)維部 |
| 3 | 測試部 |
+---------+-----------+
[root@host63 ~]#
[root@mysql62 ~]# mysql -e 'select * from tarena.dept'
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| 1 | 開發(fā)部 |
| 2 | 運(yùn)維部 |
| 3 | 測試部 |
+---------+-----------+
步驟二:練習(xí)分片表
dbpartition 定義分庫使用的分片規(guī)則,
tbpartition 定義分表使用的分片規(guī)則。
mod_hash 分片規(guī)則,用employee_id表頭的值做取模計算
tbpartitions 1 表的分片數(shù)量
dbpartitions 2 庫的分片數(shù)量
//連接mycat服務(wù)建表
[root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321
create table tarena.employees(
employee_id int primary key,
name char(10),dept_id int ,
mail varchar(30)
) default charset utf8
dbpartition BY mod_hash(employee_id) tbpartition BY mod_hash(employee_id)
tbpartitions 1 dbpartitions 2;
?在4臺數(shù)據(jù)庫服務(wù)器查看表
[root@mysql59 ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| tarena |
| tarena_0 |
+--------------------+
[root@mysql59 ~]# mysql -e 'use tarena_0 ; show tables'
+--------------------+
| Tables_in_tarena_0 |
+--------------------+
| employees_0 |
+--------------------+
[root@host61 ~]#
[root@mysql60 ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| tarena |
| tarena_0 |
+--------------------+
[root@mysql60 ~]# mysql -e 'use tarena_0 ; show tables'
+--------------------+
| Tables_in_tarena_0 |
+--------------------+
| employees_0 |
+--------------------+
[root@host62 ~]#
[root@mysql61 ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| tarena |
| tarena_1 |
+--------------------+
[root@mysql61 ~]# mysql -e 'use tarena_1;show tables'
+--------------------+
| Tables_in_tarena_1 |
+--------------------+
| employees_1 |
+--------------------+
[root@host63 ~]#
[root@mysql62 ~]# mysql -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| tarena |
| tarena_1 |
+--------------------+
[root@mysql62 ~]# mysql -e 'use tarena_1;show tables'
+--------------------+
| Tables_in_tarena_1 |
+--------------------+
| employees_1 |
+--------------------+
[root@host64 ~]#
存儲數(shù)據(jù)
[root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321
mysql> insert into tarena.employees values (9,"a","1","a@163.com");
Query OK, 1 row affected (0.08 sec)
mysql> insert into tarena.employees values (8,"B","3","B@QQ.com");
Query OK, 1 row affected (0.13 sec)
mysql> insert into tarena.employees values (7,"C","2","c@QQ.com");
Query OK, 1 row affected (0.02 sec)
mysql> insert into tarena.employees values (6,"C","2","c@QQ.com");
Query OK, 1 row affected (0.06 sec)
mysql> select * from tarena.employees;
+-------------+------+---------+-----------+
| employee_id | name | dept_id | mail |
+-------------+------+---------+-----------+
| 6 | C | 2 | c@QQ.com |
| 8 | B | 3 | B@QQ.com |
| 7 | C | 2 | c@QQ.com |
| 9 | a | 1 | a@163.com |
+-------------+------+---------+-----------+
4 rows in set (2.07 sec)
?在數(shù)據(jù)庫服務(wù)器本機(jī)查看數(shù)據(jù)
[root@mysql59 ~]# mysql -e 'select * from tarena_0.employees_0'
+-------------+------+---------+----------+
| employee_id | name | dept_id | mail |
+-------------+------+---------+----------+
| 6 | C | 2 | c@QQ.com |
| 8 | B | 3 | B@QQ.com |
+-------------+------+---------+----------+
[root@mysql59 ~]#
[root@mysql60 ~]# mysql -e 'select * from tarena_0.employees_0'
+-------------+------+---------+----------+
| employee_id | name | dept_id | mail |
+-------------+------+---------+----------+
| 6 | C | 2 | c@QQ.com |
| 8 | B | 3 | B@QQ.com |
+-------------+------+---------+----------+
[root@mysql60 ~]#
[root@mysql61 ~]# mysql -e 'select * from tarena_1.employees_1'
+-------------+------+---------+-----------+
| employee_id | name | dept_id | mail |
+-------------+------+---------+-----------+
| 7 | C | 2 | c@QQ.com |
| 9 | a | 1 | a@163.com |
+-------------+------+---------+-----------+
[root@mysql61 ~]#
[root@mysql62 ~]# mysql -e 'select * from tarena_1.employees_1'
+-------------+------+---------+-----------+
| employee_id | name | dept_id | mail |
+-------------+------+---------+-----------+
| 7 | C | 2 | c@QQ.com |
| 9 | a | 1 | a@163.com |
+-------------+------+---------+-----------+
[root@mysql62 ~]#
步驟三:練習(xí)ER表
ER表,稱為關(guān)聯(lián)表,表示數(shù)據(jù)邏輯上有關(guān)聯(lián)性的兩個或多個表,例如工資表和員工表。對于關(guān)聯(lián)表,通常希望他們能夠有相同的分片規(guī)則,這樣在進(jìn)行關(guān)聯(lián)查詢時,能夠快速定位到同一個數(shù)據(jù)分片中。MyCat2中對于關(guān)聯(lián)表,不需要有過多的聲明,他可以根據(jù)分片規(guī)則自行判斷。
1)連接mycat服務(wù)建表
[root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321
mysql> create table tarena.salary(
employee_id int primary key,
p_date date , basic int , bonus int
) DEFAULT CHARSET=utf8
dbpartition BY mod_hash(employee_id)
tbpartition BY mod_hash(employee_id) tbpartitions 1;
Query OK, 1 row affected (1.93 sec)
2)在MyCat2終端查看關(guān)聯(lián)表關(guān)系。
[root@mycat63 ~]# mysql -h127.0.0.1 -P8066 -umycat –p654321
mysql> /*+ mycat:showErGroup{}*/ ;
+---------+------------+-----------+
| groupId | schemaName | tableName |
+---------+------------+-----------+
| 0 | tarena | employees |
| 0 | tarena | salary |
+---------+------------+-----------+
2 rows in set (0.00 sec)
mysql>
3)在2臺主服務(wù)器查看表
[root@mysql59 ~]# mysql -e 'use tarena_0 ; show tables'
+--------------------+
| Tables_in_tarena_0 |
+--------------------+
| employees_0 |
| salary_0 |
+--------------------+
[root@mysql59 ~]#
[root@mysql61 ~]# mysql -e 'use tarena_1;show tables'
+--------------------+
| Tables_in_tarena_1 |
+--------------------+
| employees_1 |
| salary_1 |
+--------------------+
[root@mysql61~]#
?4)插入數(shù)據(jù)文章來源:http://www.zghlxwxcb.cn/news/detail-804477.html
[root@client50 ~]# mysql -h192.168.88.63 -P8066 -umycat –p654321
mysql> desc tarena.salary;
+-------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------+------+-----+---------+-------+
| employee_id | int | NO | PRI | NULL | |
| p_date | date | YES | | NULL | |
| basic | int | YES | | NULL | |
| bonus | int | YES | | NULL | |
+-------------+------+------+-----+---------+-------+
4 rows in set (0.07 sec)
mysql> insert into tarena.salary values(6,20230110,20000,2000);
Query OK, 1 row affected (0.28 sec)
mysql> insert into tarena.salary values(7,20230210,25000,2500);
Query OK, 1 row affected (0.21 sec)
mysql> insert into tarena.salary values(8,20230310,30000,3000);
Query OK, 1 row affected (0.26 sec)
mysql> insert into tarena.salary values(9,20230410,35000,3500);
Query OK, 1 row affected (0.05 sec)
mysql> select * from tarena.salary;
+-------------+------------+-------+-------+
| employee_id | p_date | basic | bonus |
+-------------+------------+-------+-------+
| 6 | 2023-01-10 | 20000 | 2000 |
| 8 | 2023-03-10 | 30000 | 3000 |
| 7 | 2023-02-10 | 25000 | 2500 |
| 9 | 2023-04-10 | 35000 | 3500 |
+-------------+------------+-------+-------+
4 rows in set (0.16 sec)
mysql>
?5)在4臺數(shù)據(jù)庫服務(wù)器本機(jī)查看文章來源地址http://www.zghlxwxcb.cn/news/detail-804477.html
[root@mysql59 ~]# mysql -e 'select * from tarena_0.employees_0'
+-------------+------+---------+----------+
| employee_id | name | dept_id | mail |
+-------------+------+---------+----------+
| 6 | C | 2 | c@QQ.com |
| 8 | B | 3 | B@QQ.com |
+-------------+------+---------+----------+
[root@mysql59 ~]#
[root@mysql60 ~]# mysql -e 'select * from tarena_0.salary_0'
+-------------+------------+-------+-------+
| employee_id | p_date | basic | bonus |
+-------------+------------+-------+-------+
| 6 | 2023-01-10 | 20000 | 2000 |
| 8 | 2023-03-10 | 30000 | 3000 |
+-------------+------------+-------+-------+
[root@mysql60 ~]#
[root@mysql61 ~]# mysql -e 'select * from tarena_1.employees_1'
+-------------+------+---------+-----------+
| employee_id | name | dept_id | mail |
+-------------+------+---------+-----------+
| 7 | C | 2 | c@QQ.com |
| 9 | a | 1 | a@163.com |
+-------------+------+---------+-----------+
[root@mysql62 ~]# mysql -e 'select * from tarena_1.salary_1'
+-------------+------------+-------+-------+
| employee_id | p_date | basic | bonus |
+-------------+------------+-------+-------+
| 7 | 2023-02-10 | 25000 | 2500 |
| 9 | 2023-04-10 | 35000 | 3500 |
+-------------+------------+-------+-------+
[root@mysql62~]#
到了這里,關(guān)于數(shù)據(jù)分片概述、環(huán)境準(zhǔn)備、部署MyCAT服務(wù)、全局表、分片表、ER表的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!