mysql主從同步--讀寫分離。


1.mysql 安裝參考

https://www.cnblogs.com/ttzzyy/p/9063737.html

2. 主mysql,從mysql

指定配置文件啟動

mysqld --defaults-file=/etc/my.cnf --user=root

2.1主mysql配置文件my.cnf修改(修改完重啟)

#添加在[mysqld]后
# # server-id 唯一的服務辨識號,數值位於 1 到 2^32-1之間.
# # 此值在master和slave上都需要設置.
# # 如果 “master-host” 沒有被設置,則默認為1, 但是如果忽略此選項,MySQL不會作為master生效.
server-id=10 #[必須]服務器唯一ID一般取本機IP最后一段,默認是1

# # log-bin 打開二進制日志功能.                                                                                #
# # 在復制(replication)配置中,作為MASTER主服務器必須打開此項                                                   #
# # 如果你需要從你最后的備份中做基於時間點的恢復,你也同樣需要二進制日志.                                       #
log-bin=/var/log/mysql/mysql-bin.log #[必須]啟用二進制日志   (需要建立文件夾)                                                                #
# # 需要開啟生成二進制日志記錄相關配置,配置在需要被復制的服務器上,即:master                                 #
binlog-do-db=test_db #指定對名稱為test_db的數據庫記錄二進制日志                                              # 
binlog-ignore-db=mysql #指定不對名稱為mysql的數據庫記錄二進制日志                                            #
binlog-ignore-db=information_schema #指定不對名稱為information_schema的數據庫記錄二進制日志                  #
binlog-ignore-db=performance_schema #指定不對名稱為performance_schema的數據庫記錄二進制日志
binlog_format=mixed #binlog日志格式,mysql默認采用,如果從服務器slave有別的slave要復制那么該slave也需要這一項#
expire_logs_days=7 #超過7天的binlog刪除###                                                                   #

#skip-external-locking #MySQL選項以避免外部鎖定。該選項默認開啟
default-storage-engine=InnoDB #默認存儲引擎
lower_case_table_names=1 #忽略表大小寫

2.2從mysql配置文件my.cnf修改(修改完重啟)

#添加在[mysqld]后
# # server-id 唯一的服務辨識號,數值位於 1 到 2^32-1之間.
# # 此值在master和slave上都需要設置.
# # 如果 “master-host” 沒有被設置,則默認為1, 但是如果忽略此選項,MySQL不會作為master生效.
server-id=11 #[必須]服務器唯一ID,默認是1

# # replicate-do-db 需要做復制的數據庫,如果復制多個數據庫,重復設置這選項即可master上不需要此項,slave上需要   #
replicate-do-db=test_db #復制名稱為test_db的數據庫
replicate-do-db=test_db1 #復制名稱為test_db1的數據庫                                                           
# # replicate-ignore-db 不需要復制的數據庫,如果要忽略復制多個數據庫,重復設置這個選項即可                     #
replicate-ignore-db=mysql #不需要(忽略)復制名稱為mysql的數據庫                                             #
replicate-ignore-db=information_schema #不需要(忽略)復制名稱為information_schema的數據庫                   #
replicate-ignore-db=performance_schema #不需要(忽略)復制名稱為information_schema的數據庫
#                                                                                                              #
# 如果你在使用鏈式從服務器結構的復制模式 (A->B->C),                                                            #
# # 你需要在服務器B上打開此項.                                                                                 #
# # 此選項打開在從線程上重做過的更新的日志,                                                                    #
# # 並將其寫入從服務器的二進制日志.                                                                            #
# # 默認值為OFF;設置log_slave_updates = 1即表示開啟                                                            #   
#log_slave_updates = 1                                                                                         #

#skip-external-locking #MySQL選項以避免外部鎖定。該選項默認開啟
default-storage-engine=InnoDB #默認存儲引擎
lower_case_table_names=1 #忽略表大小寫

2.3連接主mysql

show master status;
show variables like '%log_bin%';

2.4連接從mysql

change master to master_user='root', master_password='root', master_host='192.168.192.10',master_port=3306, master_log_file='mysql-bin.000002',master_log_pos=120;

start slave;

注意:上面的關聯配置的信息都是主服務器上的,但是要在slave從服務器上配置,如果master_log_file和master_log_pos不知道可以在master服務器上使用show master status;查看

……………………(省略部分)
Slave_IO_Running: Yes //此狀態必須YES
Slave_SQL_Running: Yes //此狀態必須YES
……………………(省略部分)

2.5然后可以在主庫添加數據了,從庫會自動增加數據

create table student(id int not null primary key auto_increment, name varchar(100));
insert into student values(1, 'zhangsan'),(2,'lisi'),(3,'wangwu'),(4,'zhaoliu'),(5,'haha'),(6,'xixi') ,(7,'lala');
#刪除也是可以的呦

3. 讀寫分離(主庫寫,從庫讀)mycat中間件

官方網站:http://www.mycat.org.cn/

下載地址:http://dl.mycat.io/1.6-RELEASE/

  • 第一步:將Mycat-server-1.6-release-20161028204710-linux.tar.gz上傳至服務器

  • 第二步:將壓縮包解壓縮。建議將mycat放到/usr/local/mycat目錄下。

  • tar -xzvf Mycat-server-1.6-release-20161028204710-linux.tar.gz
    
    mv mycat /usr/local
    
  • 第三步:修改schema.xml文件:

  • <?xml version="1.0"?>
    <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
    <mycat:schema xmlns:mycat="http://io.mycat/">
            <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema>
            <dataNode name="dn1" dataHost="localhost1" database="test_db" />
            <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
                              writeType="0" dbType="mysql" dbDriver="native" switchType="-1"  slaveThreshold="100">
                    <heartbeat>select user()</heartbeat>
                    <!-- can have multi write hosts -->
                    <writeHost host="hostM1" url="192.168.192.10:3306" user="root" password="root">
                            <!-- can have multi read hosts -->
                            <readHost host="hostS2" url="192.168.192.11:3306" user="root" password="root" />
                    </writeHost>
            </dataHost>
    </mycat:schema>
    
    
  • 這里面,有兩個參數需要注意,balance和 switchType。

    其中,balance指的負載均衡類型,目前的取值有4種:

    1. balance="0", 不開啟讀寫分離機制,所有讀操作都發送到當前可用的writeHost上。
    2. balance="1",全部的readHost與stand by writeHost參與select語句的負載均衡,簡單的說,當雙主雙從模式(M1->S1,M2->S2,並且M1與 M2互為主備),正常情況下,M2,S1,S2都參與select語句的負載均衡。
    3. balance="2",所有讀操作都隨機的在writeHost、readhost上分發。
    4. balance="3",所有讀請求隨機的分發到wiriterHost對應的readhost執行,writerHost不負擔讀壓力

    switchType指的是切換的模式,目前的取值也有4種:

    1. switchType='-1' 表示不自動切換
    2. switchType='1' 默認值,表示自動切換
    3. switchType='2' 基於MySQL主從同步的狀態決定是否切換,心跳語句為 show slave status
    4. switchType='3'基於MySQL galary cluster的切換機制(適合集群)(1.4.1),心跳語句為 show status like 'wsrep%'。

    因此,該配置文件中的balance="1"意味着作為stand by writeHost的hostS1和hostS2將參與select語句的負載均衡,這就實現了主從的讀寫分離,switchType='-1'意味着當主掛掉的時候,不進行自動切換,即hostS1和hostS2並不會被提升為主,仍只提供讀的功能。這就避免了將數據寫進slave的可能性,畢竟,單純的MySQL主從集群並不允許將數據讀進slave中,除非配置的是雙master。

  • 第四步:編輯server.xml文件(讀寫分離這些就夠了)

  • <?xml version="1.0" encoding="UTF-8"?>
    <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); 
            - you may not use this file except in compliance with the License. - You 
            may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 
            - - Unless required by applicable law or agreed to in writing, software - 
            distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 
            WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 
            License for the specific language governing permissions and - limitations 
            under the License. -->
    <!DOCTYPE mycat:server SYSTEM "server.dtd">
    <mycat:server xmlns:mycat="http://io.mycat/">
            <system>
                    <property name="useSqlStat">1</property>  <!-- 1為開啟實時統計、0為關閉 -->
            </system>
            <user name="root">
                    <property name="password">root</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>
            </user>
    </mycat:server>
    
    
    
  • 第五步:進入mycat目錄的bin目錄,

  • #啟動mycat
    ./mycat start 或  ./mycat console
    #停止:
    ./mycat stop
    #mycat 支持的命令{ console | start | stop | restart | status | dump }
    #Mycat的默認端口號為:8066
    
  • 第六步:使用mycat連接

  • mysql -h192.168.192.10 -uroot -proot -P8066 -DTESTDB
    
  • 執行命令

  • explain select * from student;
    #看到數據是從dn1節點查的。
    
  • 修改log4j.xml監控日志mycat.log

  • select * from student
    #可以看到select 操作被路由到了192.168.192.11也就是我們的slave1節點
    
  • #插入一條數據
    insert into student value(11,@@hostname);
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM