With the rapid development of the Internet, database storage requirements have increased exponentially. How to optimize database reading and writing efficiency and improve reading and writing speed has become a problem for every developer. Database sharding technology is one of the most common and effective ways to solve this problem.
MyCat is a distributed database middleware based on MySQL that supports the parsing and execution of transactions and SQL statements, and provides functions such as master-slave replication, read-write separation, and distributed node orchestration. Many features of MyCat are similar to Sharding-JDBC, supporting automated sharding, read-write separation, automated fault discovery and recovery, and shard load balancing. At the same time, MyCat also supports distributed transparent transmission and multi-dimensional database virtualization. It can well meet large-scale data storage and reading and writing needs.
Java API is the API interface of the Java programming language. Developers can use the Java API for database operations and data processing. The following will demonstrate how to use MyCat for database sharding in Java API development.
1. Install MyCat
First you need to download and install MyCat. You can go to the official website http://www.mycat.io/ to download. After the installation is complete, start the MyCat service.
2. Configure MyCat
Modify MyCat’s configuration file mycatserver/conf/server.xml and set the corresponding database node and shard access policy, as follows:
<?xml version="1.0"?> <!DOCTYPE server SYSTEM "server.dtd"> <server> <system> <!-- 全局参数设置 --> <property name="useSqlStat" value="false" /> </system> <!-- 读写分离配置 --> <user name="root"> <property name="password">root</property> <!-- 读写分离 --> <property name="readNode" value="dn1,dn2" /> <property name="writeNode" value="dn1,dn2" /> </user> <!-- Mycat分片配置 --> <dataHost name="dh1" maxCon="1000" minCon="2" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="mysql1" url="192.168.1.101:3306" user="root2" password="root2"> <!--读写分离--> <readHost host="mysql1" url="192.168.1.101:3306" user="root2" password="root2" /> <readHost host="mysql2" url="192.168.1.102:3306" user="root2" password="root2" /> </writeHost> <writeHost host="mysql2" url="192.168.1.102:3306" user="root2" password="root2"> <!--读写分离--> <readHost host="mysql1" url="192.168.1.101:3306" user="root2" password="root2" /> <readHost host="mysql2" url="192.168.1.102:3306" user="root2" password="root2" /> </writeHost> </dataHost> <dataNode name="dn1" dataHost="dh1" database="test" /> <dataNode name="dn2" dataHost="dh1" database="test" /> <!--分片规则配置,定义了t_user 表的分片策略--> <tableRule name="t_user" dataNode="dn1,dn2" ruleType="mod" startShardKey="id" endShardKey="id" /> <!--schema配置,schema可以看成是单独的mysql实例--> <schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1,dn2" /> </server>
Above In the configuration file, we define a data node dh1, which has two write nodes mysql1 and mysql2, and their respective read nodes. In addition, we also configured a table sharding rule: the t_user table consists of two data nodes dn1 and dn2, and is sharded according to id. Finally, use the schema tag to configure the required database.
3. Use MyCat JDBC to connect to MySQL
In Java code, we can use MyCat's JDBC driver to connect to the MySQL database and use MyCat to implement the sharding function. The connection URL format is as follows:
jdbc:mysql://localhost:8066/dbName?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
where localhost is the host name where MyCat is located , 8066 is the port number of MyCat, and dbName is the name of the database that needs to be connected. You need to use MyCat's JDBC driver when connecting. The code is as follows:
String url = "jdbc:mysql://localhost:8066/mycatdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT"; Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, "root", "root");
In the code, we load MyCat's JDBC driver through Class.forName(), and then use DriverManager.getConnection() to establish the connection with MySQL Database connection.
4. Create a sharded table
Using MyCat, you can logically create a sharded table. When designing the table, it needs to be divided into multiple physical tables, and each physical table is stored on a different database. In Java, we need to use the CREATE TABLE statement to create a sharded table, as follows:
CREATE TABLE t_user ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(32) NOT NULL, PRIMARY KEY (id) ) PARTITION BY KEY(id) PARTITIONS 4;
In the above code, we use the PARTITION BY KEY(id) statement to define the sharding rules of the table, and use PARTITIONS 4 to define 4 shards.
5. Insert data into the sharded table
In Java code, using the MyCat sharded table is the same as an ordinary MySQL table. You only need to use the INSERT statement to insert into the table. The data is as follows:
Statement stmt = conn.createStatement(); stmt.executeUpdate("INSERT INTO t_user (name) VALUES ('tom')"); stmt.executeUpdate("INSERT INTO t_user (name) VALUES ('jerry')");
Through the executeUpdate() method of the Statement object, we can insert the data named tom and jerry into the sharded table t_user. MyCat will automatically store the data in the sharded table according to the sharding rules. in the corresponding sharded physical table.
6. Query the data in the sharded table
Use MyCat to query the sharded table data in Java code. It is the same as the ordinary MySQL query. You only need to use the SELECT statement, as follows:
Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM t_user"); while(rs.next()) { long id = rs.getLong("id"); String name = rs.getString("name"); System.out.printf("id: %d, name: %s ", id, name); }
Using the next() method of the ResultSet object, we can traverse the query result set and obtain the query results through its getXXX() method.
The above are the detailed steps and sample code for using MyCat for database sharding in Java API development. MyCat provides a very complete sharding strategy and numerous features. In scenarios with large-scale database storage and reading and writing requirements, using MyCat for database sharding can effectively improve the efficiency of database operations and the speed of reading and writing.
The above is the detailed content of Using MyCat for database sharding in Java API development. For more information, please follow other related articles on the PHP Chinese website!