Mycat - The set global increment id

In " Mycat - The realization of MySQL levels of sub-table " in the article, we will order database store database in order_master data table, was the level of segmentation based on customer_id field, when we insert data into the order_master data table by Mycat when the query data order_master Mycat through the data in the table, it was found out by Mycat query data, there are many fields order_id duplicate values. This is due to orderdb01 orderdb04 database primary key order_id order_master data tables are set to auto-increment integer types, which means that orderdb01 primary key table orderdb04 order_master data in the database is increased from the beginning from 1, which led through Mycat query data order_master data tables, a large number of duplicate primary key values.

So, how to solve the problem?

Mycat provides global auto-increment id function, can solve this problem!

How does it work

At this point, we Mycat on binghe151 server resides, install the MySQL database.

If you need to install MySQL 8.x version, you can refer to " MySQL - The source compiler MySQL8.x + upgrade gcc + upgrade cmake (full pro-test version) "

If you need to install MySQL 5.x version, you can refer to the " --CentOS6.5 compile MySQL 5.7 or the installation MySQL5.6 "

After installation, log on MySQL command line, as shown below.

[root@binghe151 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 33926
Server version: 8.0.18 binghe edition

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> 

Note: This is the MySQL command line login, rather than Mycat command line. Next, a new command line mycat MySQL database, as shown below.

mysql> create database mycat;
Query OK, 1 row affected (2.52 sec)

Note: In fact, any of the mycat database built on binghe152 ~ binghe158 server on one MySQL instance can, here, I order to separate mycat database and store business database, install a separate MySQL again on binghe151 server to create mycat database .

Next, we need to mycat database initialization data tables and some data values, the required data tables and data values ​​can be initialized install dbseq.sql script conf directory under the directory by running Mycat. Next, we will dbseq.sql script into mycat database, as shown below.

[root@binghe151 ~]# mysql -uroot -p mycat < /usr/local/mycat/conf/dbseq.sql 
Enter password: 

After completion of introduction, we see the data table row under the myCat, MySQL command as shown below.

mysql> use mycat;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_mycat |
+-----------------+
| mycat_sequence  |
+-----------------+
1 row in set (0.98 sec)

mysql> select * from mycat_sequence;
+--------+---------------+-----------+
| name   | current_value | increment |
+--------+---------------+-----------+
| GLOBAL |             1 |         1 |
+--------+---------------+-----------+
1 row in set (0.03 sec)

See mycat function exist in the database, as shown below.

mysql> show function status like 'mycat%' \G
*************************** 1. row ***************************
                  Db: mycat
                Name: mycat_seq_currval
                Type: FUNCTION
             Definer: root@localhost
            Modified: 2020-03-01 22:06:11
             Created: 2020-03-01 22:06:11
       Security_type: DEFINER
             Comment: 
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
  Database Collation: utf8mb4_0900_ai_ci
*************************** 2. row ***************************
                  Db: mycat
                Name: mycat_seq_nextval
                Type: FUNCTION
             Definer: root@localhost
            Modified: 2020-03-01 22:06:11
             Created: 2020-03-01 22:06:11
       Security_type: DEFINER
             Comment: 
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
  Database Collation: utf8mb4_0900_ai_ci
*************************** 3. row ***************************
                  Db: mycat
                Name: mycat_seq_nextvals
                Type: FUNCTION
             Definer: root@localhost
            Modified: 2020-03-01 22:06:12
             Created: 2020-03-01 22:06:12
       Security_type: DEFINER
             Comment: 
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
  Database Collation: utf8mb4_0900_ai_ci
*************************** 4. row ***************************
                  Db: mycat
                Name: mycat_seq_setval
                Type: FUNCTION
             Definer: root@localhost
            Modified: 2020-03-01 22:06:12
             Created: 2020-03-01 22:06:12
       Security_type: DEFINER
             Comment: 
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
  Database Collation: utf8mb4_0900_ai_ci
4 rows in set (0.00 sec)

Next, we need to make the appropriate changes to server.xml file. Here, the value sequenceHandlerType under server.xml file system to modify the tag 1, if this option is not configured, the configuration item is increased, as shown below.

<property name="sequenceHandlerType">1</property>

Next, add in schema.xml file a dataHost node that is the server node mycat database resides, as shown below.

<dataHost name="binghe151" maxCon="1000" minCon="10" balance="1"
          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
    <heartbeat>select user()</heartbeat>
    <writeHost host="binghe51" url="192.168.175.151:3306" user="mycat" password="mycat"/>
</dataHost>

At this point, we need to create a user in MySQL binghe151 mycat server.

If you are installing a MySQL 8.x version, the MySQL command line execute the following command.

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, EXECUTE  ON *.* TO 'mycat'@'192.168.175.%';
FLUSH PRIVILEGES;

If you are installing MySQL 5.x version, in the MySQL command line execute the following command.

GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE  ON *.* TO 'mycat'@'192.168.175.%' IDENTIFIED BY 'mycat';
FLUSH PRIVILEGES;

Note: MySQL database on a server binghe151 increased mycat addition to having an outer user CRUD permissions database, need to have permission to perform the function.

Next, we need to add a node schema.xml data file, as shown below.

<dataNode name="mycat" dataHost="binghe151" database="mycat" />

In addition, we also need conf directory under the directory mounted Mycat modify sequence_db_conf.properties file as follows.

vim /usr/local/mycat/conf/sequence_db_conf.properties

FIG equence_db_conf.properties modified file contents are as follows.

GLOBAL=mycat
order_master=mycat

Next, we log on binghe151 MySQL server command line, insert data into the database mycat_sequence mycat data table, as shown below.

mysql> use mycat;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_mycat |
+-----------------+
| mycat_sequence  |
+-----------------+
1 row in set (0.21 sec)

mysql> insert into mycat_sequence values ('order_master',1,1);
Query OK, 1 row affected (0.65 sec)

mysql> select * from mycat_sequence;
+--------------+---------------+-----------+
| name         | current_value | increment |
+--------------+---------------+-----------+
| GLOBAL       |             1 |         1 |
| order_master |             1 |         1 |
+--------------+---------------+-----------+
2 rows in set (0.00 sec)

At this point, you can see mycat_sequence data in the table more than a name order_master data.

Then you need to tell Mycat how to use the global increment id.

How to use the global increment id?

Schema.xml file needs to be configured, the opening of the global data table order_master increment id, autoIncrement need to add a configuration table order_master = "true" attribute configured as follows.

<table name="order_master" primaryKey="order_id" dataNode = "orderdb01,orderdb02,orderdb03,orderdb04" rule="order_master" autoIncrement="true"/>

Next, restart Mycat, validate the configuration. In this case, after use Mycat order_master insert data into the data table, and then by Mycat order_master query data in the data table, Found order_id primary key is no longer repeated. Description Global id take effect.

Released 1322 original articles · won praise 2046 · Views 5.18 million +

Guess you like

Origin blog.csdn.net/l1028386804/article/details/104602420