最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

mysql主從同步 數(shù)據(jù)分離 分庫分表

2023-02-18 13:12 作者:bili_39183997178  | 我要投稿

1.1 主從同步介紹

存儲(chǔ)數(shù)據(jù)的服務(wù)結(jié)構(gòu),分為2種角色:

主服務(wù)器(master):接受客戶端訪問連接

從服務(wù)器(slave):自動(dòng)同步主服務(wù)器數(shù)據(jù)

1.2主從同步工作原理

主服務(wù)器:?jiǎn)⒂胋inlog日志

?從服務(wù)器:

IO線程:復(fù)制master主機(jī) binlog日志文件里的SQL命令保存到中繼日志文件里。

SQL線程:執(zhí)行中繼日志文件里的SQL語句,實(shí)現(xiàn)與Master數(shù)據(jù)一致。

1.3常用的主從同步結(jié)構(gòu)

一主一從結(jié)構(gòu)

一主多從結(jié)構(gòu)

主從從結(jié)構(gòu)

主主結(jié)構(gòu)


?(一主一從同步結(jié)構(gòu))

第一步 配置master服務(wù)器?

????????????? ???? 具體步驟如下:

???????????????? ?? 1) 啟用binlog日志文件

???????????????? ? vim /etc/my.cnf

???????????????? ? [mysqld]

???????????????? ? server_id=51

???????????????? ? log_bin=master51?? #日志默認(rèn)存儲(chǔ)在數(shù)據(jù)庫目錄下

? ? ? ? ? ? ? ? ? ??]# systemctl? restart mysqld

? ? ? ? ? ? ? ? ? ??2)用戶授權(quán)

???????????????? ? ]# mysql -uroot? -p密碼

???????????????? ? replication slave 讓用戶有復(fù)制命令權(quán)限

???????????????? ? mysql> grant? replication slave on *.*? to? repluser@"%"

???????????????? ? identified by? "123qqq...A";

? ? ? ? ? ? ? ? ? ? ?3)查看日志信息

???????????????? ?? #查看到的日志名和偏移量是給 從服務(wù)器使用的

mysql> show master status;

?第二步 配置slave服務(wù)器

?1 指定server_id 并重啟mysqld服務(wù)

???????????????????????? vim /etc/my.cnf

???????????????????????? [mysqld]

???????????????????????? server_id=52? #自己添加的

? ? ? ? ? ? ? ? ? ? ? ??systemctl? restart mysqld

? ? ? ? ? ? ? ? ? ?? 2 確保數(shù)據(jù)一致(如果一致的此步驟可以省略)

???????????????? ?? 3 指定主服務(wù)器信息

???????????????? ?? ]#mysql -uroot? -p密碼

mysql> show slave status \G? #不是從數(shù)據(jù)庫服務(wù)器

Empty set (0.00 sec)

? ? ? ? ? ? ? ? ? ?? mysql> change master to? master_host="192.168.4.51",

???????????????? ?? master_user="repluser" , master_password="123qqq...A",

???????????????? ?? master_log_file="master51.000001" , master_log_pos=441 ;

? ? ? ? ? ? ? ? ? ?4)啟動(dòng)slave進(jìn)程

???????????????? ?? mysql> start slave;

? ? ? ? ? ? ? ? ? 5)查看狀態(tài) (IO線程和 SQL線程必須同時(shí)是YES 狀態(tài))

???????????????? ?? mysql> show slave status \G?

根據(jù) IO線程和 SQL線程的報(bào)錯(cuò)信息排錯(cuò)

mysql> show slave status \G????????????????? ??

Last_IO_Error:? IO線程的報(bào)錯(cuò)信息

Last_SQL_Error:? SQL線程的報(bào)錯(cuò)信息

在從服務(wù)器執(zhí)行如下操作:

mysql> stop slave;

在主服務(wù)器查看日志名和偏移量

host51? show? master status;


所有錯(cuò)誤的 統(tǒng)一解決辦法:

把slave角色的服務(wù)器恢復(fù)為獨(dú)立的數(shù)據(jù)庫服務(wù)器,重新配置為從服務(wù)器。

在從服務(wù)器執(zhí)行如下操作:

?systemctl? stop?? mysqld

?cd? /var/lib/mysql/

?rm? -rf master.info

?rm -rf?? *-relay-bin.*

?rm -rf relay-log.info

?systemctl?? start? mysqld


一主多從同步結(jié)構(gòu)

和上面的配置一樣的只是id不同相同

[root@host53 ~]# vim /etc/my.cnf

[mysqld]

server_id=53? #添加

:wq

[root@host53 ~]# systemctl? restart mysqld


主從從 同步結(jié)構(gòu)

? 第1步:配置主數(shù)據(jù)庫服務(wù)器 192.168.4.53

???????????????????????? 1) 啟用binlog日志

???????????????????????? vim /etc/my.cnf

???????????????????????? [mysqld]

???????????????????????? server_id=53

???????????????????????? log_bin=master53

???????????????????????? :wq

? ? ? ? ? ? ? ? ? ? ? systemctl restart? mysqld

? ? ? ? ? ? ? ? ? ? ? ? ? 2) 用戶授權(quán)

host53~]# mysql? -uroot? -p123qqq...A

mysql> grant replication slave on *.*? to repluser@"%" identified by "123qqq...A";

? ? ? ? ? ? ? ? 3) 查看日志信息

mysql> show master status;

配置從服務(wù)器

vim /etc/my.cnf

???????????? [mysqld]

???????????? server_id=54

???????????? log_bin=master54

???????????? log_slave_updates #允許級(jí)聯(lián)復(fù)制,host54主機(jī)把自己主服務(wù)器的數(shù)據(jù)

systemctl? restart mysqld

???????????? [root@host54 ~]# mysql -uroot -p123qqq...A

mysql> grant? replication slave on *.*? to repluser@"%" identified by "123qqq...A";

???????????? mysql> show master status;

change master to? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? master_host="192.168.4.53",master_user="repluser",master_password="123qqq...A",

??? -> master_log_file="master53.000001",master_log_pos=441;

mysql> start slave;

配置從服務(wù)器

]# vim /etc/my.cnf

[mysqld]

server_id=55

:wq

systemctl restart? mysqld

]# mysql? -uroot? -p123qqq...A??????????????????????????

mysql> change master to master_host="192.168.4.54",master_user="repluser",master_password="123qqq...A",

??? -> master_log_file="master54.000001",master_log_pos=441;

mysql> start slave;

mysql> show slave status \G


同步結(jié)構(gòu) 主主結(jié)構(gòu)

vim /etc/my.cnf

[mysqld]

server_id=68

log_bin=master68

:wq

systemctl? restart? mysqld??????????

?]# mysql? -uroot? -p123qqq...A? ? ? ? ??

mysql> grant? replication slave on *.*? to? repluser@"%" identified by "123qqq...A";

Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> show master status;

第二臺(tái)

[root@host69 ~]# vim /etc/my.cnf

[mysqld]

server_id=69

log_bin=master69

:wq

systemctl? restart? mysqld??

?mysql> grant replication slave on *.*? to repluser@"%"? identified by "123qqq...A";

Query OK, 0 rows affected, 1 warning (0.04 sec)

?mysql> show master status;

#把自己配置為host68的slave服務(wù)器,日志名和偏移量要在host68主機(jī)查看后填寫

mysql> change master to? master_host="192.168.4.68",master_user="repluser",master_password="123qqq...A",

??? -> master_log_file="master68.000001",master_log_pos=441;

mysql> start slave;

mysql> show slave status \G

???????? Master_Host: 192.168.4.68

???????? Slave_IO_Running: Yes

??????????? Slave_SQL_Running: Yes

?

第3步:在host68? 主機(jī)數(shù)據(jù)庫管理員登錄服務(wù)后

???????? #把自己指定為host69 主機(jī)的slave 服務(wù)器

[root@host68 ~]# mysql -uroot -p123qqq...A

mysql> change master to? master_host="192.168.4.69" , master_user="repluser" , master_password="123qqq...A",

??? -> master_log_file="master69.000001",master_log_pos=441;

mysql> start slave;

mysql> show slave status \G

???????? Master_Host: 192.168.4.69

???????? Slave_IO_Running: Yes

??????????? Slave_SQL_Running: Yes


mysql 主從同步復(fù)制數(shù)據(jù)工作模式

??? 1)主從同步復(fù)制模式 可以在任意數(shù)據(jù)庫服務(wù)器啟用半同步服務(wù)模式

? ?支持的工作方式:

???????????????? 第1種 異步復(fù)制模式(默認(rèn))

???????????????? 主服務(wù)器執(zhí)行完一次事務(wù)后,立即將結(jié)果返給客戶端,

???????????????? 不關(guān)心從服務(wù)器是否已經(jīng)同步數(shù)據(jù)。

第2種 半同步復(fù)制模式

???????????????? 主服務(wù)器在執(zhí)行完一次事務(wù)后,等待至少一臺(tái)從服務(wù)器同步數(shù)據(jù)完成,

???????????????? 才將結(jié)果返回給客戶端。

安裝模塊

#安裝master模塊

MySQL> INSTALL? PLUGIN? rpl_semi_sync_master?? SONAME? "semisync_master.so";

#安裝slave模塊

MySQL> INSTALL? PLUGIN? rpl_semi_sync_slave?? SONAME? "semisync_slave.so";???

?mysql> SELECT? plugin_name, plugin_status FROM? information_schema.plugins?

WHERE plugin_name LIKE "%semi%";? 查看模塊是否安裝成功

啟用模塊

#啟用master模塊

mysql> SET? GLOBAL rpl_semi_sync_master_enabled=1

#啟用slave模塊

mysql> SET? GLOBAL rpl_semi_sync_slave_enabled=1;???

查看模塊是否啟用

MySQL> SHOW? VARIABLES? LIKE? "rpl_semi_sync_%_enabled"; ? ?


方法2? 永久配置 編輯主配置文件

vim? /etc/my.cnf

[mysqld]

#安裝模塊??????? ???

plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"

#啟用模塊

rpl_semi_sync_slave_enabled=1

rpl_semi_sync_master_enabled=1

:wq


數(shù)據(jù)讀寫分離介紹

? ? ? ? ? ?把查詢請(qǐng)求select 命令 給slave服務(wù)器處理

????????????把存儲(chǔ)請(qǐng)求insert 命令 給master服務(wù)器處理

??????提供數(shù)據(jù)讀寫分離功能的中間件軟件有: mysql-proxy?? maxscale?? mycat?

使用maxscale 提供數(shù)據(jù)讀寫服務(wù)

?第1 步 :? 把host51 配置為master數(shù)據(jù)庫服務(wù)器

?第2 步 :? 把host52 配置為slave數(shù)據(jù)庫服務(wù)器

?步驟參考?? RDBMS2_day01 的 一主一從配置 例子

安裝一臺(tái)maxscale主機(jī)

修改配置文件

cp /etc/maxscale.cnf /root/? 備份主配置文件

[root@host57 ~]# vim ??? /etc/maxscale.cnf

#服務(wù)啟動(dòng)后線程的數(shù)量

[maxscale]

threads=auto

?[server1]??? 指定第1臺(tái)數(shù)據(jù)庫服務(wù)器的ip地址

type=server

address=192.168.4.51

port=3306

protocol=MySQLBackend

?[server2]?? 指定第2臺(tái)數(shù)據(jù)庫服務(wù)器的ip地址

type=server

address=192.168.4.52

port=3306

protocol=MySQLBackend

?[MySQL Monitor]?? 定義監(jiān)視的數(shù)據(jù)庫服務(wù)器

type=monitor

module=mysqlmon

servers=server1,server2??? 監(jiān)視server1和server2

user=mysqla????? 監(jiān)控用戶

passwd=123qqq...A? 連接密碼

monitor_interval=10000

?#禁止只讀服務(wù)

#[Read-Only Service]

#type=service

#router=readconnroute

#servers=server1

#user=myuser

#passwd=mypwd

#router_options=slave

?[Read-Write Service]?? 啟用讀寫分離服務(wù)

type=service

router=readwritesplit

servers=server1,server2?? 讀寫分離服務(wù)在server1和server2服務(wù)器之間進(jìn)行

user=mysqlb?? 路由用戶

passwd=123qqq...A? 連接密碼

max_slave_connections=100%

?[MaxAdmin Service]?? 管理服務(wù)(通過訪問管理服務(wù)可以查看監(jiān)控信息)

type=service

router=cli

?因?yàn)橹蛔x服務(wù)沒有啟用所有也不需要定義服務(wù)使用的端口號(hào)

#[Read-Only Listener]

#type=listener

#service=Read-Only Service

#protocol=MySQLClient

#port=4008

?[Read-Write Listener]?? 定義讀寫分離服務(wù)使用端口號(hào)

type=listener

service=Read-Write Service

protocol=MySQLClient

port=4006??? 端口號(hào)

[MaxAdmin Listener]?? 定義管理服務(wù)使用端口號(hào)

type=listener

service=MaxAdmin Service

protocol=maxscaled

socket=default

port=4016? 端口號(hào)

?配置數(shù)據(jù)庫服務(wù)器(在數(shù)據(jù)庫服務(wù)器上添加監(jiān)控用戶和路由用戶)

注意:因?yàn)槭侵鲝慕Y(jié)構(gòu) ,所以只需要在主服務(wù)器添加,從服務(wù)器會(huì)自動(dòng)同步

添加監(jiān)控用戶 mysqla 用戶

mysql> grant? replication?? slave , replication? client? on? *.*? to?

mysqla@"%" identified by "123qqq...A";

? ? ?權(quán)限說明:

replication client?? 監(jiān)視數(shù)據(jù)庫服務(wù)的運(yùn)行狀態(tài)?

replication slave ?? ?數(shù)據(jù)庫服務(wù)器的主從角色

?添加路由用戶 mysqlb 用戶

mysql> grant? select on? mysql.*? to? mysqlb@"%" identified by "123qqq...A";? #對(duì)授權(quán)庫下的表有查詢權(quán)限

[root@host57 ~]# maxscale? -f /etc/maxscale.cnf???? 啟動(dòng)服務(wù)

[root@host57 ~]# ls /var/log/maxscale/?? 查看日志文件

maxscale.log

[root@host57 ~]# netstat? -utnlp? | grep 4006??? 查看讀寫分離服務(wù)端口號(hào)

tcp6?????? 0????? 0 :::4006???????????????? :::*??????????????????? LISTEN????? 1580/maxscale? ? ??

[root@host57 ~]# netstat? -utnlp? | grep 4016? 查看讀寫分離服務(wù)端口號(hào)

?

多實(shí)例:

在一臺(tái) 服務(wù)器上允許多個(gè)數(shù)據(jù)庫服務(wù)

為什么要使用多實(shí)例? 節(jié)約運(yùn)維成本?? 提高硬件利用率

]# rpm?? -q? libaio? || yum -y install libaio

? ]# tar -xf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

? ]# mv mysql-5.7.20-linux-glibc2.12-x86_64? /usr/local/mysql

? ]# PATH=/usr/local/mysql/bin:$PATH

? ]# vim /etc/bashrc ???????????

? export? PATH=/usr/local/mysql/bin:$PATH 添加在文件的末尾

? :wq

? [root@host58 ~]# id mysql || useradd? mysql

id: mysql: no such user

[root@host58 ~]# grep mysql /etc/passwd

mysql:x:1000:1000::/home/mysql:/bin/bash

?ls /etc/my.cnf??? 如果文件有的話要?jiǎng)h除 rm? -rf? /etc/my.cnf

? ? ? ? ? ]# vim /etc/my.cnf?? 創(chuàng)建文件并編輯

???????? ?#管理多實(shí)例服務(wù) 的 運(yùn)行配置

???????? ?[mysqld_multi]

???????? ?mysqld = /usr/local/mysql/bin/mysqld_safe?? 服務(wù)啟動(dòng)的時(shí)候 執(zhí)行的是那個(gè)命令

???????? ?mysqladmin =?? /usr/local/mysql/bin/mysqladmin?? 修改數(shù)據(jù)庫管理員密碼使用的命令

???????? ?user?? =? root???? 管理服務(wù)的啟動(dòng)者?

? ? ? ? ?#定義實(shí)例1

???? ???? [mysqld1]

???????? ?datadir =? /dir1????????? 數(shù)據(jù)庫目錄

???????? ?port =? 3307?????????????? 服務(wù)的端口號(hào)

???????? ?log-error =? /dir1/mysqld1.err??? 錯(cuò)誤日志文件

???????? ?pid-file =? /dir1/mysqld1.pid?????? pid號(hào)文件

???????? ?socket =? /dir1/mysqld1.sock?????? socket文件 (在數(shù)據(jù)庫服務(wù)器本機(jī)訪問多實(shí)例時(shí)

???????? ?通過socket區(qū)分連接的實(shí)例服務(wù))

? ? ? ? ? ?#定義實(shí)例2

???????? [mysqld2]

???? ???? datadir =? /dir2????????? 數(shù)據(jù)庫目錄

???????? ?port =? 3308?????????????? 服務(wù)的端口號(hào)

???????? ?log-error =? /dir2/mysqld2.err??? 錯(cuò)誤日志文件

???????? ?pid-file =? /dir2/mysqld2.pid?????? pid號(hào)文件

???????? ?socket =? /dir2/mysqld2.sock?????? socket文件 (在數(shù)據(jù)庫服務(wù)器本機(jī)訪問多實(shí)例時(shí)

???????? ?通過socket區(qū)分連接的實(shí)例服務(wù))???

?

?mysqld_multi start? 1? ? ?啟動(dòng)實(shí)例1

?查看目錄下的文件列表

[root@host58 ~]# ls /dir1/??

auto.cnf??????? ib_logfile0? mysql??????? mysqld1.sock??????? sys

ib_buffer_pool? ib_logfile1? mysqld1.err? mysqld1.sock.lock

ibdata1???????? ibtmp1?????? mysqld1.pid ?performance_schema

?可以查看到端口

[root@host58 ~]#? netstat? -utnlp? | grep? 3307????

?使用初始密碼連接服務(wù)

[root@host58 ~]# mysql -uroot -p'avH,8dVtZnaq' -S? /dir1/mysqld1.sock

[root@host58 ~]# mysqld_multi? --user=root --password=123456 stop 1? ? 停止實(shí)例需要對(duì)應(yīng)的密碼及用戶


分庫分表

??? 1)什么是分庫分表

將存放在一臺(tái)數(shù)據(jù)庫服務(wù)器中的數(shù)據(jù),按照特定方式進(jìn)行拆分,

分散存放到多臺(tái)數(shù)據(jù)庫服務(wù)器中,以達(dá)到分散單臺(tái)服務(wù)器負(fù)載的效果

2)分庫分表分割方式

?????????? 垂直分割(縱向切分)

????????????? 按業(yè)務(wù)類型分類,將存儲(chǔ)在一個(gè)庫里的表分別存儲(chǔ)到不同的數(shù)據(jù)庫器里

?????????? 水平分割(橫向切分)

????????????? 將表記錄按行切分,分散存儲(chǔ)到多個(gè)數(shù)據(jù)庫服務(wù)器表中。

3) MyCAT軟件介紹(提供數(shù)據(jù)分庫分表存儲(chǔ)的服務(wù)軟件(中間件))

mycat是基于Java的分布式數(shù)據(jù)庫系統(tǒng)中間件,為高并發(fā)環(huán)境的分布式存儲(chǔ)提供解決方案

適合數(shù)據(jù)大量寫入的存儲(chǔ)需求

支持MySQL、Oracle、Sqlserver、Mongodb等

提供數(shù)據(jù)讀寫分離服務(wù)

提供數(shù)據(jù)分片服務(wù)

基于阿里巴巴Cobar進(jìn)行研發(fā)的開源軟件

?

? 4) 提供的10種分片規(guī)則

1 枚舉法 sharding-by-intfile

2 固定分片 rule1

3 范圍約定 auto-sharding-long

4 求模法 mod-long

5 日期列分區(qū)法 sharding-by-date

6 通配取模 sharding-by-pattern

7 ASCII碼求模通配? sharding-by-prefixpattern

8 編程指定 sharding-by-substring

9 字符串拆分hash解析 sharding-by-stringhash

10 一致性hash?? sharding-by-murmur

?

5) 分片服務(wù)器的工作過程;當(dāng)mycat收到一個(gè)SQL命令時(shí)

第一步:解析SQL命令涉及到的表

?第二步:根據(jù)表使用分片規(guī)則的計(jì)算結(jié)果(看對(duì)表的配置,如果有分片規(guī)則,則獲取SQL命令里分片字段的值,并匹配分片函數(shù),獲得分片列表)

?第三步:將SQL命令發(fā)往對(duì)應(yīng)的數(shù)據(jù)庫服務(wù)器去執(zhí)行

?第四步:最后收集和處理所有分片結(jié)果數(shù)據(jù),并返回到客戶端



部署mycat服務(wù)

?????????? 第一步:安裝軟件

[root@maxscale56 ~]# yum? -y?? install? java-1.8.0-openjdk.x86_64

[root@maxscale56 ~]# tar -xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

[root@maxscale56 ~]# mv mycat/ /usr/local/??????????

[root@maxscale56 ~]# ls /usr/local/mycat/

bin? catlet? conf? lib? logs? version.txt

?

?????????? 第二步:了解安裝目錄文件列表

bin????? ??? //mycat命令

?catlet? ???? //擴(kuò)展功能

?conf?? ????? //配置文件

???????????? .txt? 和 ..properties 結(jié)尾的是 分片規(guī)則配置文件

???????????? .xml 結(jié)尾的是 mycat服務(wù)配置文件

?lib?????? ??? //mycat使用的jar包

?logs???? ??? //mycat啟動(dòng)日志和運(yùn)行日志

?version.txt? //mycat軟件 說明文件

第三步:修改配置文件

# vim /usr/local/mycat/conf/server.xml?? (使用默認(rèn)配置即可)

??????? <user name="root"> 用戶名

??????????????? <property name="password">123456</property> 密碼

??????????????? <property name="schemas">TESTDB</property>? 虛擬庫名

??????? </user>

??????? <user name="user"> 用戶名

??????????????? <property name="password">user</property> 密碼

??????????????? <property name="schemas">TESTDB</property> 虛擬庫名

??????????????? <property name="readOnly">true</property> 只讀訪問權(quán)限

??????? </user>

?:wq


#設(shè)置分片存儲(chǔ)數(shù)據(jù)的表 schema.xml 文件格式說明

?vim /usr/local/mycat/conf/schema.xml

<?xml version="1.0"?>

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">

<mycat:schema xmlns:mycat="http://io.mycat/">

#定義分片存儲(chǔ)數(shù)據(jù)的表

<schema ....>?

???????? <table?? name="表名" dataNode="數(shù)據(jù)庫要存儲(chǔ)在幾臺(tái)服務(wù)器里" rule="分片規(guī)則名"???? / >

? ?<table ...>

???????? ????? ....

???????? </table>

</schema>

? ? ?#定義數(shù)據(jù)庫服務(wù)器主機(jī)名

<dataNode? name="第幾臺(tái)數(shù)據(jù)庫服務(wù)器"? dataHost="主機(jī)名"? database="庫名" />

? ? #定義數(shù)據(jù)庫服務(wù)器ip地址

<dataHost .....??? >

??????? ......

</dataHost>

</mycat:schema>? ?

vim? /usr/local/mycat/conf/schema.xml??? ??

?<table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />

?<table name="employee" primaryKey="ID" dataNode="dn1,dn2,dn3" rule="sharding-by-intfile" />

?<table name="customer" primaryKey="ID" dataNode="dn1,dn2,dn3"? rule="sharding-by-intfile">

? ? ?#定義3臺(tái)數(shù)據(jù)庫服務(wù)器的主機(jī)名

?<dataNode name="dn1" dataHost="mysql53" database="db1" />? 第一臺(tái)

?<dataNode name="dn2" dataHost="mysql54" database="db2" />? 第二臺(tái)

?<dataNode name="dn3" dataHost="mysql55" database="db3" />? 第三臺(tái)

#指定名稱叫mysql53主機(jī)的ip地址

<dataHost name="mysql53"?? ...... >?

???????????????? ......

??????????? <writeHost host="hostM1" url="192.168.4.53:3306" user="pljadmin" password="123qqq...A">

??????????? </writeHost>

</dataHost>

?#指定名稱叫mysql54主機(jī)的ip地址??

<dataHost name="mysql54"?? ...... >

???????????????? ......

??????????????? <writeHost host="hostM2" url="192.168.4.54:3306" user="pljadmin" password="123qqq...A">

??????????????? </writeHost>

</dataHost>?

? #指定名稱叫mysql55主機(jī)的ip地址?

<dataHost name="mysql55"?? ...... >?

???????????????? ......

???????????????? <writeHost host="hostM3" url="192.168.4.55:3306" user="pljadmin" password="123qqq...A">

???????????????? </writeHost>

</dataHost>?

:wq

修改后的內(nèi)容

<?xml version="1.0"?>

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">

<mycat:schema xmlns:mycat="http://io.mycat/">

??????? <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">

??????????????? <table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />

??????????????? <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />

??????????????? <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />

??????????????? <table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"

?????????????????????????? rule="mod-long" />

??????????????? <table name="employee" primaryKey="ID" dataNode="dn1,dn2,dn3"

?????????????????????????? rule="sharding-by-intfile" />

??????????????? <table name="customer" primaryKey="ID" dataNode="dn1,dn2,dn3"

?????????????????????????? rule="sharding-by-intfile">

??????????????????????? <childTable name="orders" primaryKey="ID" joinKey="customer_id"

??????????????????????????????????????????????? parentKey="id">

??????????????????????????????? <childTable name="order_items" joinKey="order_id"

??????????????????????????????????????????????????????? parentKey="id" />

??????????????????????? </childTable>

??????????????????????? <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"

??????????????????????????????????????????????? parentKey="id" />

??????????????? </table>

??????? </schema>

?

??????? <dataNode name="dn1" dataHost="mysql53" database="db1" />

??????? <dataNode name="dn2" dataHost="mysql54" database="db2" />

??????? <dataNode name="dn3" dataHost="mysql55" database="db3" />

? <dataHost name="mysql53" maxCon="1000" minCon="10" balance="0"

????????????????????????? writeType="0" dbType="mysql" dbDriver="native" switchType="1"? slaveThreshold="100">

??????????????? <heartbeat>select user()</heartbeat>

??????????????? <writeHost host="hostM1" url="192.168.4.53:3306" user="pljadmin" password="123qqq...A">

??????????????? </writeHost>

??????? </dataHost>

<dataHost name="mysql54" maxCon="1000" minCon="10" balance="0"

????????????????????????? writeType="0" dbType="mysql" dbDriver="native" switchType="1"? slaveThreshold="100">

??????????????? <heartbeat>select user()</heartbeat>

??????????????? <writeHost host="hostM2" url="192.168.4.54:3306" user="pljadmin" password="123qqq...A">

??????????????? </writeHost>

??????? </dataHost>

? ?<dataHost name="mysql55" maxCon="1000" minCon="10" balance="0"

????????????????????????? writeType="0" dbType="mysql" dbDriver="native" switchType="1"? slaveThreshold="100">

??????????????? <heartbeat>select user()</heartbeat>

??????????????? <writeHost host="hostM3" url="192.168.4.55:3306" user="pljadmin" password="123qqq...A">

??????????????? </writeHost>

??????? </dataHost>

</mycat:schema>


配置數(shù)據(jù)庫服務(wù)器

?# 添加用戶pljadmin

?mysql -uroot -p123qqq...A? -e?? 'grant all on? *.*? to pljadmin@"%" identified by "123qqq...A" #幾臺(tái)都要添加

啟動(dòng)mycat服務(wù)

[root@host56 mycat]# /usr/local/mycat/bin/mycat? start? ??

查看日志 文件

[root@host56 mycat]# ls /usr/local/mycat/logs/? ? ?

查看端口號(hào)

[root@host56 mycat]# netstat? -utnlp? | grep 8066??

停止服務(wù)的命令

[root@host56 mycat]# /usr/local/mycat/bin/mycat? stop?

服務(wù)沒有啟動(dòng)的排錯(cuò)方法

? 查看日志文件獲取報(bào)錯(cuò)信息? ? ? ? ?

??????? vim? /usr/local/mycat/logs/wrapper.log


建表存儲(chǔ)數(shù)據(jù)(驗(yàn)證分片規(guī)則)

??? ? 配置思路:

???????????????? 1 確定使用分片規(guī)則的表叫什么名字名?

(查看schema.xml 文件里 <table> )

???????????????? 2 確定分片字段的表頭名

(查看rule.xml文件里的?? <tableRule> )

???????????? ?? ??3 確定分片規(guī)則使用的配置文件,定義分片字段的值

(查看rule.xml文件里的?? <function> )

?? ?????????????????4 修改分片規(guī)則的配置文件,定義分片字段的值

???????????????? 5 重啟mycat服務(wù)

???????????????? 6 根據(jù)分片規(guī)則建表存儲(chǔ)數(shù)據(jù)

???????????????? 5? 在數(shù)據(jù)庫服務(wù)器本機(jī)查看存儲(chǔ)的數(shù)據(jù)


#找使用sharding-by-intfile分片規(guī)則的表名

?vim? /usr/local/mycat/conf/schem.xml?

?<table name="employee" primaryKey="ID" dataNode="dn1,dn2,dn3" rule="sharding-by-intfile" />

? #創(chuàng)建employee分片字段名? sharding_id

??? vim?? /usr/local/mycat/conf/rule.xml

<tableRule name="sharding-by-intfile"> 分片規(guī)則名

??????????????? <rule>

??????????????????????? <columns>sharding_id</columns> 分片字段名

??????????????????????? <algorithm>hash-int</algorithm>?? 算法名? (存儲(chǔ)數(shù)據(jù)的計(jì)算規(guī)則)

??????????????? </rule>

??????? </tableRule>

? #找到sharding-by-intfile分片規(guī)則配置文件partition-hash-int.txt

??? <function name="hash-int"?? 算法名

??????????????? class="io.mycat.route.function.PartitionByFileMap">? 算法調(diào)用的執(zhí)行程序

??????????????? <property name="mapFile">partition-hash-int.txt</property> 配置文件名

?? </function>

? ?#編輯 sharding-by-intfile分片規(guī)則配置文件partition-hash-int.txt 設(shè)置 分片字段的值

??? host56]# vim? /usr/local/mycat/conf/partition-hash-int.txt

分片字段值=第幾臺(tái)數(shù)據(jù)庫服務(wù)器

10000=0????? # 0對(duì)應(yīng)dn1???? mysql53??? 192.168.4.53?? db1

10010=1????? # 1對(duì)應(yīng)dn2???? mysql54??? 192.168.4.54?? db2

10020=2????? # 2對(duì)應(yīng)dn3????? mysql54??? 192.168.4.54?? db3

:wq

?[root@host56 conf]# /usr/local/mycat/bin/mycat restart


mod-long分片規(guī)則工作過程: 根據(jù)分片字段值與設(shè)定的數(shù)字求模結(jié)果存儲(chǔ)數(shù)據(jù)

???????????????????? ?當(dāng)余數(shù)是 0? 數(shù)據(jù)存儲(chǔ)到? dn1

???????????????????? ?當(dāng)余數(shù)是 1? 數(shù)據(jù)存儲(chǔ)到? dn2

???????????????????? ?當(dāng)余數(shù)是 2? 數(shù)據(jù)存儲(chǔ)到? dn3

???????????????????? ?

創(chuàng)建表:必須根據(jù)表使用的分片規(guī)則建表,具體操作如下:

????????????? ??? 1 確定使用分片規(guī)則的表叫什么名字名? (查看schema.xml 文件里 <table> )

[root@maxscale56 ~]# vim /usr/local/mycat/conf/schema.xml???????????????????

<table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"

?????????????????????????? rule="mod-long" />

? ? ? ? ? ? ? ? ? ?2 確定分片字段的表頭名 (查看rule.xml文件里的?? <tableRule> )

[root@maxscale56 ~]# vim /usr/local/mycat/conf/rule.xml?????????????????

???? <tableRule name="mod-long"> 分片規(guī)則名

??????????????? <rule>

??????????????????????? <columns>id</columns> 分片字段名

??????????????????????? <algorithm>mod-long</algorithm> 算法

??????????????? </rule>

??????? </tableRule>

? ? ? ? ? ?3 確定分片規(guī)則使用的配置文件,定義分片字段的值 (查看rule.xml文件里的?? <function> )

?[root@maxscale56 ~]# vim /usr/local/mycat/conf/rule.xml

???? <function name="mod-long" class="io.mycat.route.function.PartitionByMod">

??????????????? <!-- how many data nodes -->

??????????????? <property name="count">3</property> 定義分片字段 做求模計(jì)算的數(shù)字(是數(shù)據(jù)庫服務(wù)器的臺(tái)數(shù))

??????? </function>

? ? ? ? ? ? ? ? ? 4 重啟mycat服務(wù)? (配置文件沒有做過修改 ,服務(wù)不需要重啟)

????????????????? 5 創(chuàng)建表并存儲(chǔ)數(shù)據(jù)




mysql主從同步 數(shù)據(jù)分離 分庫分表的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
中卫市| 中方县| 龙川县| 二连浩特市| 汉中市| 左云县| 永济市| 北碚区| 隆回县| 讷河市| 卫辉市| 黄龙县| 志丹县| 黔江区| 武安市| 固始县| 雅江县| 鄂托克前旗| 城固县| 黑河市| 招远市| 荆州市| 临夏市| 岗巴县| 建阳市| 龙口市| 武鸣县| 宁波市| 阳江市| 荣昌县| 六盘水市| 罗山县| 蕲春县| 历史| 右玉县| 开封市| 鄂伦春自治旗| 县级市| 射洪县| 犍为县| 镇赉县|