Encrypt

Usage

Pre-work

  1. Start the MySQL service
  2. Create MySQL database (refer to ShardingProxy data source configuration rules)
  3. Create a role or user with creation permission for ShardingProxy
  4. Start Zookeeper service (for persistent configuration)

Start ShardingProxy

  1. Add mode and authentication configurations to server.yaml (please refer to the example of ShardingProxy)
  2. Start ShardingProxy (Related introduction)

Create a distributed database and sharding tables

  1. Connect to ShardingProxy
  2. Create a distributed database
  1. CREATE DATABASE encrypt_db;
  1. Use newly created database
  1. USE encrypt_db;
  1. Configure data source information
  1. ADD RESOURCE ds_0 (
  2. HOST=127.0.0.1,
  3. PORT=3306,
  4. DB=ds_0,
  5. USER=root,
  6. PASSWORD=root
  7. );
  1. Create encrypt table
  1. CREATE TABLE `t_encrypt` (
  2. `order_id` int NOT NULL,
  3. `user_plain` varchar(45) DEFAULT NULL,
  4. `user_cipher` varchar(45) DEFAULT NULL,
  5. PRIMARY KEY (`order_id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  1. Create encrypt rule
  1. CREATE ENCRYPT RULE t_encrypt (
  2. COLUMNS(
  3. (NAME=user_id,PLAIN=user_plain,CIPHER=user_cipher,TYPE(NAME=AES,PROPERTIES('aes-key-value'='123456abc'))),
  4. (NAME=order_id, CIPHER =order_cipher,TYPE(NAME=MD5))
  5. ));
  1. Alter encrypt rule
  1. CREATE ENCRYPT RULE t_encrypt (
  2. COLUMNS(
  3. (NAME=user_id,PLAIN=user_plain,CIPHER=user_cipher,TYPE(NAME=AES,PROPERTIES('aes-key-value'='123456abc'))),
  4. ));
  1. Drop encrypt rule
  1. DROP ENCRYPT RULE t_encrypt;
  1. Drop resource
  1. DROP RESOURCE ds_0;
  1. Drop distributed database
  1. DROP DATABASE encrypt_db;

Notice

  1. Currently, DROP DATABASE will only remove the logical distributed database, not the user’s actual database.
  2. DROP TABLE will delete all logical fragmented tables and actual tables in the database.
  3. CREATE DATABASE will only create a logical distributed database, so users need to create actual databases in advance.