最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL 復(fù)制詳解及簡單實例

 更新時間:2017年04月25日 09:07:47   投稿:lqh  
這篇文章主要介紹了MySQL 復(fù)制詳解及簡單實例的相關(guān)資料,需要的朋友可以參考下

MySQL 復(fù)制詳解及簡單實例

 主從復(fù)制技術(shù)在MySQL中被廣泛使用,主要用于同步一臺服務(wù)器上的數(shù)據(jù)至多臺從服務(wù)器,可以用于實現(xiàn)負(fù)載均衡,高可用和故障切換,以及提供備份等等。MySQL支持多種不同的復(fù)制技術(shù),諸如單向,半同步異步復(fù)制等以及不同級別的復(fù)制,諸如數(shù)據(jù)庫級別,表級,跨庫同步等等。本文簡要描述了一個基本的主從復(fù)制并給出示例。 

1、復(fù)制的基本原理(步驟)

    a、在主庫上把數(shù)據(jù)更改記錄的二進制日志(binary log)
    b、從庫上的I/O線程連接到主庫并請求發(fā)送其二進制日志文件(主庫上的binlog dump線程將二進制日志內(nèi)容發(fā)送到從庫)
    c、從庫上的I/O線程讀取主服務(wù)發(fā)送的二進制內(nèi)容并將其拷貝到中繼日志
    d、從庫上的SQL線程讀取中繼日志并執(zhí)行日志中包含的更新 

2、為配置文件添加復(fù)制項

# 本文的演示基于同一服務(wù)器上的多實例環(huán)境,其中3406端口用作主庫,而3506用作從庫。 
# 關(guān)于多實例的部署可參考: 
# MySQL多實例配置(一) http://blog.csdn.net/leshami/article/details/40339167 
# MySQL多實例配置(二) http://blog.csdn.net/leshami/article/details/40339295 
# 3406與3506為都為新裝且含缺省庫等,所以本文演示中未涉及先遷移主庫數(shù)據(jù)到備庫步驟 
a、主庫上的配置文件 
# more my3406.cnf  
[mysqld] 
socket = /tmp/mysql3406.sock 
port = 3406 
pid-file = /data/inst3406/data3406/my3406.pid 
user = mysql 
log-error=/data/inst3406/data3406/inst3406.err 
datadir=/data/inst3406/data3406 
basedir=/app/soft/mysql5 
 
#### for master items #### 
server-id=3406 
log_bin=/data/inst3406/log/bin/inst3406bin 
innodb_flush_log_at_trx_commit=1 
sync_binlog=1 
 
b、從庫上的配置文件 
# more my3506.cnf  
[mysqld] 
socket = /tmp/mysql3506.sock   # Author : Leshami 
port = 3506            # Blog  : <a target="_blank"  rel="external nofollow" >http://blog.csdn.net/leshami 
pid-file</a> = /data/inst3506/data3506/my3506.pid 
user = mysql 
log-error=/data/inst3506/data3506/inst3506.err 
datadir=/data/inst3506/data3506 
basedir=/app/soft/mysql5 
 
#### for slave items #### 
server-id=3506 
relay_log=/data/inst3506/log/relay/relay-bin 
read_only=1 

3、創(chuàng)建復(fù)制賬號

#啟動端口為3406的實例并添加賬戶 
[mysql@app ~]$ mysqld_safe --defaults-file=/data/inst3406/data3406/my3406.cnf & 
[mysql@app ~]$ mysql -P3406  #登陸到3406 
 
master@localhost[(none)]> show variables like 'server_id'; 
+---------------+-------+ 
| Variable_name | Value | 
+---------------+-------+ 
| server_id   | 3406 | 
+---------------+-------+ 
 
#創(chuàng)建用于復(fù)制的賬戶 
master@localhost[(none)]> grant replication slave,replication client on *.* 
  -> to repl@'192.168.1.177' identified by 'repl'; 
 
#初始化主庫日志文件,生成環(huán)境慎用reset 
master@localhost[(none)]> reset master; 
Query OK, 0 rows affected (0.01 sec) 
 
#查看主庫的狀態(tài),日志初始化至000001, 
master@localhost[(none)]> show master status,Position為120 
+--------------------+----------+--------------+------------------+-------------------+ 
| File        | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | 
+--------------------+----------+--------------+------------------+-------------------+ 
| inst3406bin.000001 |   120 |       |         |          | 
+--------------------+----------+--------------+------------------+-------------------+ 

4、配置主從同步

#啟動端口為3506的實例 
[mysql@app ~]$ mysqld_safe --defaults-file=/data/inst3506/data3506/my3506.cnf & 
 
[mysql@app ~]$ msyql -P3506 
 
slave@localhost[(none)]> show variables like 'server_id'; 
+---------------+-------+ 
| Variable_name | Value | 
+---------------+-------+ 
| server_id   | 3506 | 
+---------------+-------+ 
1 row in set (0.00 sec) 
 
#為從庫添加指向主庫的相關(guān)配置信息,該命令會生成及修改備庫上的master.info及relay-log.info文件 
slave@localhost[(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.177', 
  -> MASTER_USER='repl', 
  -> MASTER_PASSWORD='repl', 
  -> MASTER_PORT=3406, 
  -> MASTER_LOG_FILE='inst3406bin.000001', 
  -> MASTER_LOG_POS=0; 
Query OK, 0 rows affected, 2 warnings (0.04 sec) 
 
#出現(xiàn)了2個warnings,查看一下 
slave@localhost[(none)]> show warnings \G 
*************************** 1. row *************************** 
 Level: Note 
  Code: 1759 
Message: Sending passwords in plain text without SSL/TLS is extremely insecure. 
*************************** 2. row *************************** 
 Level: Note 
  Code: 1760 
Message: Storing MySQL user name or password information in the master.info repository is not secure and is therefore not recommended.  
Please see the MySQL Manual for more about this issue and possible alternatives. 
2 rows in set (0.00 sec) 
 
#此時查看從庫的狀態(tài)信息 
slave@localhost[(none)]> show slave status \G 
*************************** 1. row *************************** 
        Slave_IO_State:  
         Master_Host: 192.168.1.177 
         Master_User: repl 
         Master_Port: 3406 
        Connect_Retry: 60 
       Master_Log_File: inst3406bin.000001 
     Read_Master_Log_Pos: 4 
        Relay_Log_File: relay-bin.000001 
        Relay_Log_Pos: 4 
    Relay_Master_Log_File: inst3406bin.000001 
       Slave_IO_Running: No   #IO線程沒有運行 
      Slave_SQL_Running: No   #SQL線程沒有運行 
          ...................... 
       Master_Info_File: /data/inst3506/data3506/master.info 
 
slave@localhost[(none)]> start slave; #啟動slave 
Query OK, 0 rows affected (0.01 sec) 
 
#含義如下 
START SLAVE with no thread_type options starts both of the slave threads. The I/O thread reads 
events from the master server and stores them in the relay log. The SQL thread reads events from the 
relay log and executes them. 
 
#再次查看slave的狀態(tài) 
robin@localhost[(none)]> show slave status\G 
*************************** 1. row *************************** 
        Slave_IO_State: Waiting for master to send event 
         Master_Host: 192.168.1.177 
         Master_User: repl 
         Master_Port: 3406 
        Connect_Retry: 60 
       Master_Log_File: inst3406bin.000001 
     Read_Master_Log_Pos: 120 
        Relay_Log_File: relay-bin.000002 
        Relay_Log_Pos: 285 
    Relay_Master_Log_File: inst3406bin.000001 
       Slave_IO_Running: Yes    #IO線程處于運行狀態(tài)  
      Slave_SQL_Running: Yes    #SQL線程處于運行狀態(tài)  
           .............. 
     Exec_Master_Log_Pos: 120 
       Relay_Log_Space: 452 
           ............ 
       Master_Server_Id: 3406 
         Master_UUID: 32f53a0a-63ef-11e4-93d9-8c89a5d108ae 
       Master_Info_File: /data/inst3506/data3506/master.info 
          SQL_Delay: 0 
     SQL_Remaining_Delay: NULL   
   Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it #重要的提示信息 
 
#可以看到從庫上的2個線程,一個是用于I/O線程,用于連接到主庫請求主庫發(fā)送binlog,一個是用于執(zhí)行SQL的SQL線程。 
slave@localhost[(none)]> show processlist\G 
*************************** 1. row *************************** 
   Id: 4 
  User: system user 
  Host:  
   db: NULL 
Command: Connect 
  Time: 510993 
 State: Waiting for master to send event 
  Info: NULL 
*************************** 2. row *************************** 
   Id: 5 
  User: system user 
  Host:  
   db: NULL 
Command: Connect 
  Time: 333943 
 State: Slave has read all relay log; waiting for the slave I/O thread to update it 
  Info: NULL 

5、驗證同步情況

#下面在主庫上執(zhí)行一些操作以檢查從庫的同步情況 
master@localhost[(none)]> show variables like 'server_id'; 
+---------------+-------+ 
| Variable_name | Value | 
+---------------+-------+ 
| server_id   | 3406 | 
+---------------+-------+ 
1 row in set (0.00 sec) 
 
#主庫上Binlog Dump線程用于發(fā)送binlog日志文件到從庫,如下查詢 
master@localhost[(none)]> show processlist\G 
*************************** 1. row *************************** 
   Id: 12 
  User: repl 
  Host: 192.168.1.177:57440 
   db: NULL 
Command: Binlog Dump 
  Time: 511342 
 State: Master has sent all binlog to slave; waiting for binlog to be updated 
  Info: NULL 
   
#主庫創(chuàng)建數(shù)據(jù)庫及表 
master@localhost[(none)]> create database tempdb; 
Query OK, 1 row affected (0.01 sec) 
 
master@localhost[(none)]> use tempdb 
Database changed 
master@localhost[tempdb]> create table tb_engines as select * from information_schema.engines; 
Query OK, 9 rows affected (0.02 sec) 
Records: 9 Duplicates: 0 Warnings: 0 
 
#下面是在從庫上檢查的結(jié)果 
slave@localhost[(none)]> select count(*) from tempdb.tb_engines; 
+----------+ 
| count(*) | 
+----------+ 
|    9 | 
+----------+ 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Mysql:The user specified as a definer (''xxx@''%'') does not exist的解決方案

    Mysql:The user specified as a definer (''xxx@''%'') does not

    今天小編就為大家分享一篇關(guān)于Mysql:The user specified as a definer ('xxx@'%') does not exist的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 分享MySQL的自動化安裝部署的方法

    分享MySQL的自動化安裝部署的方法

    線上的MySQL一般都采用源碼編譯,雖然MySQL的源碼編譯挺簡單的,但是試想一下,如果你有幾百臺服務(wù)器同時要安裝MySQL,難道你還一臺臺去手動編譯、編寫配置文件嗎?這顯然太低效了,本文討論MySQL的自動化安裝部署。
    2014-07-07
  • MySQL 4.0 升級到mysql 5.0的方法

    MySQL 4.0 升級到mysql 5.0的方法

    需要從4.0直接升級到5.0,查看了一下changelog,發(fā)現(xiàn)主要有以下變化,需要升級mysql的朋友可以參考下。
    2011-02-02
  • MySQL索引失效的幾種情況匯總

    MySQL索引失效的幾種情況匯總

    這篇文章主要介紹了MySQL索引失效的幾種情況,幫助大家更好的理解和使用MySQL索引,感興趣的朋友可以了解下
    2020-09-09
  • SQL查詢語句優(yōu)化的實用方法總結(jié)

    SQL查詢語句優(yōu)化的實用方法總結(jié)

    下面小編就為大家?guī)硪黄猄QL查詢語句優(yōu)化的實用方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • MySql主從復(fù)制機制全面解析

    MySql主從復(fù)制機制全面解析

    這篇文章主要介紹了MySql主從復(fù)制機制全面解析的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用MySQL數(shù)據(jù)庫,感興趣的朋友可以了解下
    2021-04-04
  • MYSQL?SQL查詢近7天一個月的數(shù)據(jù)的操作方法

    MYSQL?SQL查詢近7天一個月的數(shù)據(jù)的操作方法

    這篇文章主要介紹了MYSQL?SQL查詢近7天一個月的數(shù)據(jù)的操作方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • 實戰(zhàn)MySQL升級的最佳方法

    實戰(zhàn)MySQL升級的最佳方法

    這篇文章給大家從理論到實戰(zhàn)詳細(xì)分享了MySQL升級的最佳方法,有需要的朋友跟著學(xué)習(xí)操作下吧。
    2017-12-12
  • mysql使用自定義序列實現(xiàn)row_number功能(步驟詳解)

    mysql使用自定義序列實現(xiàn)row_number功能(步驟詳解)

    這篇文章主要介紹了mysql使用自定義序列實現(xiàn)row_number功能,本文分步驟通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-12-12
  • Mysql ID生成策略的三種方法選擇及優(yōu)缺點

    Mysql ID生成策略的三種方法選擇及優(yōu)缺點

    mysql ID生成策略一般常用的有三種,包括自增、UUID 以及雪花算法,本文主要介紹了Mysql ID生成策略的三種方法選擇及優(yōu)缺點,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06

最新評論

扶沟县| 民权县| 铜陵市| 平罗县| 定襄县| 迁安市| 阳江市| 慈利县| 伊金霍洛旗| 康平县| 赣州市| 吉林市| 浪卡子县| 拉孜县| 娄烦县| 贞丰县| 桦川县| 湾仔区| 南阳市| 龙胜| 贵州省| 渭源县| 汾西县| 湖南省| 诸暨市| 新邵县| 抚宁县| 辽宁省| 天柱县| 北票市| 温宿县| 德格县| 三江| 凤凰县| 额尔古纳市| 洛隆县| 黄石市| 阿克| 商丘市| 延庆县| 巩义市|