本文教你如何实现Mycat与MySQL 8.x互连,也就是说实现Mycat连接MySQL 8.x数据库,同时,实现使用MySQL 8.x的命令行连接Mycat。
1.创建MySQL用户
首先,在MySQL8.x中创建Mycat连接MySQL的用户,如下所示。
CREATE USER 'mycat'@'192.168.175.%' IDENTIFIED BY 'mycat';
ALTER USER 'mycat'@'192.168.175.%' IDENTIFIED WITH mysql_native_password BY 'mycat';
GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'mycat'@'192.168.175.%';
FLUSH PRIVILEGES;
2.配置schema.xml文件
select user()
select user()
select user()
3.配置server.xml文件
1
druidparser
3307
3308
0
0.0.0.0
utf8mb4
2048
2
2
1800000
300
0
0
2
1000
104857600
1
cTwf23RrpBCEmalp/nx0BAKenNhvNs2NSr9nYiMzHADeEDEfwVWlI6hBDccJjNBJqJxnunHFp5ae63PPnMfGYA==
shop
注意:在需要使用MySQL 8.x的mysql命令连接Mycat时,在server.xml文件的system标签下必须配置如下选项。
1
druidparser
否则,MySQL 8.x的mysql命令连接Mycat会失败。
user标签下的password标签的值为登录Mycat的密码,此值使用Mycat提供的加密类对密码明文进行了加密。此类存在于Mycat安装目录下的lib目录下的Mycat-server-xxx-release.jar中的io.mycat.util.DecryptUtil类,可以使用如下命令对密码进行加密。
java java -cp /usr/local/mycat/lib/Mycat-server-xxx-release.jar io.mycat.util.DecryptUtil 0:mycat:mycat
其中0:mycat:mycat为运行Jar包的参数,0表示应用程序连接Mycat时使用密文密码;第一个mycat代表连接Mycat的用户名,也就是说为哪个用户的密码加密;第二个mycat代表需要加密的密码。
加密后的结果数据如下所示
cTwf23RrpBCEmalp/nx0BAKenNhvNs2NSr9nYiMzHADeEDEfwVWlI6hBDccJjNBJqJxnunHFp5ae63PPnMfGYA==
即user标签下的password属性的值。
如果按照上述方式为连接Mycat的密码加密后,需要在user标签下配置如下选项,否则无法正确连接Mycat
1
4.连接Mycat
使用MySQL 8.x中的mysql命令连接Mycat,如下所示。
[root@binghe151 ~]# mysql -umycat -pmycat -h192.168.175.151 -P3307 --default-auth=mysql_native_password
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-xxx-release-20200228205020 MyCat Server (OpenCloudDB)
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>
使用MySQL8.x中的mysql命令连接Mycat时,需要注意的是要在mysql命令后面添加--default-auth=mysql_native_password选项。
接下来,查看下Mycat下的逻辑库和逻辑表,如下所示。
mysql> SHOW DATABASES;
+----------+
| DATABASE |
+----------+
| shop |
+----------+
1 row in set (0.00 sec)
mysql> USE shop;
Database changed
mysql> SHOW TABLES;
+-----------------------+
| Tables in shop |
+-----------------------+
| customer_balance_log |
| customer_inf |
| customer_level_inf |
| customer_login |
| customer_login_log |
| customer_point_log |
| order_cart |
| order_customer_addr |
| order_detail |
| order_master |
| product_brand_info |
| product_category |
| product_comment |
| product_info |
| product_pic_info |
| product_supplier_info |
| region_info |
| serial |
| shipping_info |
| warehouse_info |
| warehouse_proudct |
+-----------------------+
21 rows in set (0.00 sec)
mysql> SELECT product_id, product_code, product_name FROM product_info LIMIT 10;
+------------+------------------+---------------------------------------+
| product_id | product_code | product_name |
+------------+------------------+---------------------------------------+
| 1 | 3700000000000001 | [Columbia]打底裤示例商品-1 |
| 2 | 3600000000000001 | [TheNorthFace]小脚裤示例商品-1 |
| 3 | 3500000000000001 | [李宁]九分裤示例商品-1 |
| 4 | 3400000000000001 | [LOWA]哈伦裤示例商品-1 |
| 5 | 3300000000000001 | [JACK&JONES]连体裤示例商品-1 |
| 6 | 3200000000000001 | [诺诗兰]牛仔裤示例商品-1 |
| 7 | 3100000000000001 | [骆驼]休闲裤示例商品-1 |
| 8 | 3000000000000001 | [金狐狸]风衣示例商品-1 |
| 9 | 2900000000000001 | [Columbia]小西装示例商品-1 |
| 10 | 2800000000000001 | [李宁]外套示例商品-1 |
+------------+------------------+---------------------------------------+
10 rows in set (0.01 sec)
至此,就配置完了,是不是很简单呢?喜欢的话点个赞吧!!