User information table horizontal table sharding Solution

Source: Internet
Author: User

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/225U043W-0.jpg "/>

Below 650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/225U05603-1.jpg "/>

The method is to use the user id for MOD. MOD (new. id, 3) and 3 represent splitting to three tables.

Follow these steps:

Step 1: create three split tables on the master database (M), t0, t1, t2

 
 
  1. create table t0 like t; 
  2. create table t1 like t; 
  3. create table t2 like t; 

Step 2: Create Three triggers on the master database (M) for insertion, update, and deletion)

 
 
  1. DELIMITER $$ 
  2.  
  3. USE `test`$$ 
  4.  
  5. DROP TRIGGER /*!50032 IF EXISTS */ `t_insert`$$ 
  6.  
  7. CREATE 
  8.     /*!50017 DEFINER = 'admin'@'%' */ 
  9.     TRIGGER `t_insert` AFTER INSERT ON `t`  
  10.     FOR EACH ROW BEGIN 
  11.     DECLARE v_result INT; 
  12.     SET v_result=MOD(new.id,3); 
  13.     IF v_result = 0 THEN 
  14.       INSERT INTO t0(id,NAME,age,address) VALUES(new.id,new.name,new.age,new.address); 
  15.     ELSEIF v_result = 1 THEN 
  16.       INSERT INTO t1(id,NAME,age,address) VALUES(new.id,new.name,new.age,new.address); 
  17.     ELSE 
  18.       INSERT INTO t2(id,NAME,age,address) VALUES(new.id,new.name,new.age,new.address); 
  19.     END IF; 
  20.     END; 
  21. $$ 
  22.  
  23. DELIMITER ; 
  24.  
  25.  
  26. /*============================================*/ 
  27.  
  28. DELIMITER $$ 
  29.  
  30. USE `test`$$ 
  31.  
  32. DROP TRIGGER /*!50032 IF EXISTS */ `t_update`$$ 
  33.  
  34. CREATE 
  35.     /*!50017 DEFINER = 'admin'@'%' */ 
  36.     TRIGGER `t_update` AFTER UPDATE ON `t`  
  37.     FOR EACH ROW BEGIN 
  38.     DECLARE v_result INT; 
  39.     SET v_result=MOD(new.id,3); 
  40.     IF v_result = 0 THEN 
  41.       REPLACE INTO t0(id,NAME,age,address) VALUES(new.id,new.name,new.age,new.address); 
  42.     ELSEIF v_result = 1 THEN 
  43.       REPLACE INTO t1(id,NAME,age,address) VALUES(new.id,new.name,new.age,new.address); 
  44.     ELSE 
  45.       REPLACE INTO t2(id,NAME,age,address) VALUES(new.id,new.name,new.age,new.address); 
  46.     END IF; 
  47.     END; 
  48. $$ 
  49.  
  50. DELIMITER ; 
  51.  
  52.  
  53. /*============================================*/ 
  54.  
  55. DELIMITER $$ 
  56.  
  57. USE `test`$$ 
  58.  
  59. DROP TRIGGER /*!50032 IF EXISTS */ `t_delete`$$ 
  60.  
  61. CREATE 
  62.     /*!50017 DEFINER = 'admin'@'%' */ 
  63.     TRIGGER `t_delete` AFTER DELETE ON `t`  
  64.     FOR EACH ROW BEGIN 
  65.     DECLARE v_result INT; 
  66.     SET v_result=MOD(old.id,3); 
  67.     IF v_result = 0 THEN 
  68.       DELETE FROM t0 WHERE id = OLD.id; 
  69.     ELSEIF v_result = 1 THEN 
  70.       DELETE FROM t1 WHERE id = OLD.id; 
  71.     ELSE 
  72.       DELETE FROM t2 WHERE id = OLD.id; 
  73.     END IF; 
  74.     END; 
  75. $$ 
  76.  
  77. DELIMITER ; 

In this way, the t table data will be updated to the tables t0, t1, and t2 Based on the modulo result.

Step 3: When the synchronization replication latency is 0, stop the synchronization replication on the slave database (S) stop slave and show the slave status \ G records to the text for later use.

Step 4: Create a new instance (S2) from the database and export the tables t, t0, t1, and t2 as long as the data is exported, instead of the table structure)

 
 
  1. /usr/local/mysql/bin/mysqldump -uroot -p123456 --dump-slave=2 -nt --skip-triggers -q --single-transaction test t t0 t1 t2 > t_all.sql

-- Dump-slave is a feature of MySQL5.5. For details, refer to this article.

Http://hcymysql.blog.51cto.com/blog/5223301/889971

If you are 5.1, we need to use the show slave status \ G recorded in step 3 to find the point of change master to, so as to ensure the incremental data after the split.

Step 5: create tables t, t0, t1, and t2 on the S2 instance of the slave database. The Three triggers are also created, during the import, data is distributed to tables t0, t1, and t2 through the trigger modulo operation, and the master database M is synchronized and copied.

Step 6: After completing the preceding five steps, check the data growth and check whether the synchronization is normal. If there is no problem, you can distribute the data to the new three servers.

On the S2 instance, dump the t0 table and import it to the M_new1 machine. Then, on the M_new1 machine, add

 
 
  1. replicate-ignore-table=test.t1 
  2. replicate-ignore-table=test.t2 

Ignore synchronous replication of tables t1 and t2.

Dump table t1 and import it to machine M_new2. Then add it to machine my. cnf on machine M_new2.

 
 
  1. replicate-ignore-table=test.t0  
  2. replicate-ignore-table=test.t2  

Ignore the synchronization replication of tables t0 and t2.

Dump table t2 and import it to machine M_new3. Then add it to machine my. cnf on machine M_new3.

 
 
  1. replicate-ignore-table=test.t0   
  2. replicate-ignore-table=test.t1  

Ignore the synchronization replication of tables t0 and t1.

Step 7: so far, we have basically finished it. The rest of the work is to develop and modify their code, tell them the sharding rules, modulo by user ID, and split several tables, what is the IP address of the machine to which the server is split.

At a.m., the development required O & M to restart the front-end application, so that the user information table could be split smoothly.

The process may be complicated. If you are interested, you can perform the test according to the above operations.

 

This article is from the "hechun's technical column" blog, please be sure to keep this source http://hcymysql.blog.51cto.com/5223301/1179880

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.