flickrf 分布式主鍵產生方案【mysql】

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   使用   sp   資料   

【相關連結:http://blog.csdn.net/bluishglc/article/details/7710738】

具體做法:

1:找兩台伺服器,分別配置:

1 TicketServer1:2 auto-increment-increment = 23 auto-increment-offset = 14 5 TicketServer2:6 auto-increment-increment = 27 auto-increment-offset = 2

 

例如:可以在全域global,本地session或者local動態設定這兩個參數以控制auto_increment的增量與位移量。本例在session中設定這二者的值:

具體做法是:
1.1:修改自增量

  

mysql> set @@session.auto_increment_increment=2;Query OK, 0 rows affected (0.00 sec)mysql> show variables like ‘auto_increment_increment‘;+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| auto_increment_increment | 2 |+--------------------------+-------+1 row in set (0.00 sec)mysql>

   

1.2:修改步長 

 

mysql> set @@session.auto_increment_offset=3;Query OK, 0 rows affected (0.00 sec)mysql> show variables like ‘auto_increment_offset‘;+-----------------------+-------+| Variable_name | Value |+-----------------------+-------+| auto_increment_offset | 3 |+-----------------------+-------+1 row in set (0.00 sec)mysql>

  

2:每張表建資料庫flickr,建表Tickets:

CREATE TABLE `Tickets` (`id` bigint(20) unsigned NOT NULL auto_increment,`stub` char(1) NOT NULL default ‘‘,PRIMARY KEY (`id`),UNIQUE KEY `stub` (`stub`)) ENGINE=MyISAM

  

注意:

2.1:表中必須包含了一個自增主鍵和一個unique欄位。
2.2:stub欄位為char(1)
2.3:引擎選用MyIsam。


3:使用LAST_INSERT_ID()函數取序列值,使用REPLACE INTO插入資料,這是很討巧的作法,主要是希望利用mysql自身的機制產生ID,不僅是因為這樣簡單,更是因為我們需要ID按照我們設定的方式(初值和步長)來產生。

4:SELECT LAST_INSERT_ID()必須要於REPLACE INTO語句在同一個資料庫連接下才能得到剛剛插入的新ID,否則返回的值總是0

5:使用純粹的JDBC來實現對錶的操作。

flickrf 分布式主鍵產生方案【mysql】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.