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

MySQL5.6 Replication主從復制(讀寫分離) 配置完整版

 更新時間:2016年04月17日 12:12:58   投稿:mdxy-dxy  
這篇文章主要介紹了MySQL5.6 Replication主從復制(讀寫分離) 配置完整版,需要的朋友可以參考下

MySQL5.6主從復制(讀寫分離)教程

1、MySQL5.6開始主從復制有兩種方式:

基于日志(binlog);
基于GTID(全局事務標示符)。

需要注意的是:GTID方式不支持臨時表!所以如果你的業(yè)務系統(tǒng)要用到臨時表的話就不要考慮這種方式了,至少目前最新版本MySQL5.6.12的GTID復制還是不支持臨時表的。

所以本教程主要是告訴大家如何通過日志(binlog)方式做主從復制!

2、MySQL官方提供的MySQL Replication教程:

http://dev.mysql.com/doc/refman/5.6/en/replication.html

第一步:準備工作

主服務器: 192.168.1.100
從服務器: 192.168.1.101

MySQL軟件版本:

MySQL-server-advanced-5.6.18-1.el6.x86_64.rpm
MySQL-cient-advanced-5.6.18-1.el6.x86_64.rpm

第二步:在主服務器和從服務器上安裝MySQL數(shù)據庫軟件

安裝方法,請參見 http://www.fzitv.net/article/82542.htm

MySQL數(shù)據庫軟件安裝完成后,不要急著做mysql啟動操作。建議把mysql初始化生成的/usr/my.cnf
(如果是從源文件編譯安裝時,路徑應該是在/usr/local/mysql/mysql.cnf)刪除,然后把優(yōu)化好的mysql
配置文件my.cnf放到/etc下。
第三步:修改主數(shù)據庫的配置文件/usr/my.cnf

復制代碼 代碼如下:

[mysqld]
 
server-id=1
log-bin=mysqlmaster-bin.log
sync_binlog=1

innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
 
lower_case_table_names=1
log_bin_trust_function_creators=1

第四步:修改從數(shù)據庫配置文件/usr/my.cnf
 

復制代碼 代碼如下:

server-id=2
log-bin=mysqlslave-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1

第五步:在主數(shù)據庫和從數(shù)據庫服務器上分別執(zhí)行以下命令重新啟動主數(shù)據庫和從數(shù)據庫

復制代碼 代碼如下:

[root@master ~]# service mysql restart
[root@slave ~]# service mysql restart

第六步:在主數(shù)據庫上創(chuàng)建用于主從復制的賬戶

復制代碼 代碼如下:

[root@master ~]# mysql -uroot -p
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.101' IDENTIFIED BY '111111';
Query OK, 0 rows affected (0.00 sec)

注意:以上命令中的IP地址,是從數(shù)據庫服務器的IP地址。

第七步:主數(shù)據庫鎖表(禁止再插入數(shù)據以獲取主數(shù)據庫的的二進制日志坐標)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
 

第八步:查看主數(shù)據庫的狀態(tài)(并記錄下File字段和Position字段的值,在配置從服務器時有用到)

mysql> show master status;
+------------------------+----------+--------------+------------------+-------------------+
| File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+----------+--------------+------------------+-------------------+
| mysqlmaster-bin.000004 |      327 |              |                  |                  |
+------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

第九步:創(chuàng)建主數(shù)據庫的快照文件
[root@master ~]# cd /usr/bin/
# ./mysqldump -uroot -p -h127.0.0.1 -P3306 --all-databases --triggers --routines --events >>/mnt/windows/all.sql
上面命令中的紅色部分,是一個共享目錄,這個目錄可以同時被主數(shù)據庫服務器和從數(shù)據庫服務器訪問到。
如果沒有這樣的共享目錄,可以將all.sql放在其它任何目錄下,然后使用scp命令復制到遠程從數(shù)據庫服務器的某個目錄中
這條命令的執(zhí)行時間根據數(shù)據量的不同,會有所不同,如果主數(shù)據庫的數(shù)據量很大,可能需要很長時間,那么在這種情況下,就最好在晚上沒有業(yè)務的時候進行這個操作,否則第七步中的鎖表操作會對業(yè)務系統(tǒng)造成很大的影響
第十步:解鎖主數(shù)據庫的鎖表操作
[root@master ~]# mysql -uroot -p    (本命令在主數(shù)據庫服務器上執(zhí)行)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
第十一步:在從數(shù)據庫服務器上導入第七步創(chuàng)建的快照文件到從數(shù)據庫中
[root@slave ~]# mysql -uroot -p -h127.0.0.1 -P3306 < /mnt/windows/all.sql
第十二步:在從數(shù)據庫服務器上設置主數(shù)據庫服務器向從數(shù)據庫服務器同步
[root@slave ~]# mysql -uroot -p
mysql> change master to master_host = '192.168.1.100',master_user='repl',master_password='111111',master_log_file='mysqlmaster-bin.000004',master_log_pos=327;
注意:紅色部分的值,是在第八步中查出來的,這里不能弄錯了
第十三步:啟動從數(shù)據庫復制線程
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
第十四步:查詢從數(shù)據庫的復制線程狀態(tài)

復制代碼 代碼如下:

mysql> show slave status \G
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysqlmaster-bin.000004
          Read_Master_Log_Pos: 327
              Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 289
        Relay_Master_Log_File: mysqlmaster-bin.000004
          Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
          Replicate_Do_Table:
      Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                  Last_Errno: 0
                  Last_Error:
                Skip_Counter: 0
          Exec_Master_Log_Pos: 327
              Relay_Log_Space: 462
              Until_Condition: None
              Until_Log_File:
                Until_Log_Pos: 0
          Master_SSL_Allowed: No
          Master_SSL_CA_File:
          Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
              Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
              Last_SQL_Errno: 0
              Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
            Master_Server_Id: 1
                  Master_UUID: 2e5e1b22-f0a9-11e3-bbac-000c297799e0
            Master_Info_File: /var/lib/mysql/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
          Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
    Last_SQL_Error_Timestamp:
              Master_SSL_Crl:
          Master_SSL_Crlpath:
          Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

如果Slave_IO_Running和Slave_SQL_Running兩項都為yes,就表示主從復制配置成功了.
下面可以開始測試配置是否成功了,首先在主數(shù)據庫的test數(shù)據庫中新建一張表,然后插入幾條數(shù)據,然后到從數(shù)據庫看看是否同步過來了。
注意:當從數(shù)據庫有大量的查詢時,可以暫時將從數(shù)據庫的復制線程關閉掉,等查詢量降下來了,再打開,這樣也不會丟失數(shù)據。

附:一個優(yōu)化好后的主數(shù)據庫配置文件和從數(shù)據配置文件內容如下:

復制代碼 代碼如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[client]
port=3306
socket=/usr/local/mysql/mysql.sock
default-character-set=utf8
 
[mysqld]
sync_binlog=1
server-id=1
port=3306
socket=/usr/local/mysql/mysql.sock
pid-file=/home/mysql/temp/my3306.pid
user=mysql
datadir=/home/mysql/data
tmpdir=/home/mysql/temp/
log-bin=/home/mysql/data/mysqlmaster-bin
log-error=/home/mysql/logs/error.log
slow_query_log_file=/home/mysql/logs/slow.log
binlog_format=mixed
slow_query_log
long_query_time=10
wait_timeout=31536000
interactive_timeout=31536000
max_connections=500
max_user_connections=490
max_connect_errors=2
character_set_server=utf8
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 5M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 4
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
replicate_ignore_db=mysql
replicate_ignore_db=information_schema
expire-logs-days=10
skip-slave-start
skip-name-resolve
lower_case_table_names=1
log_bin_trust_function_creators=1
 
# InnoDB
innodb_data_home_dir=/home/mysql/data
innodb_log_group_home_dir=/home/mysql/logs
innodb_data_file_path=ibdata1:128M:autoextend
innodb_buffer_pool_size=2G
innodb_log_file_size=10M
innodb_log_buffer_size=8M
innodb_lock_wait_timeout=50
innodb_file_per_table
innodb_flush_log_at_trx_commit=1
 
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
 

 
 
一個優(yōu)化好的從數(shù)據庫的配置文件如下:

復制代碼 代碼如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[client]
port=3306
socket=/usr/local/mysql/mysql.sock
default-character-set=utf8
 
[mysqld]
sync_binlog=1
server-id=2
port=3306
socket=/usr/local/mysql/mysql.sock
pid-file=/home/mysql/temp/my3306.pid
user=mysql
datadir=/home/mysql/data
tmpdir=/home/mysql/temp/
log-bin=/home/mysql/data/mysqlslave-bin
log-error=/home/mysql/logs/error.log
slow_query_log_file=/home/mysql/logs/slow.log
binlog_format=mixed
slow_query_log
long_query_time=10
wait_timeout=31536000
interactive_timeout=31536000
max_connections=500
max_user_connections=490
max_connect_errors=2
character_set_server=utf8
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 5M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 4
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
replicate_ignore_db=mysql
replicate_ignore_db=information_schema
expire-logs-days=10
#skip-slave-start
skip-name-resolve
lower_case_table_names=1
log_bin_trust_function_creators=1
 
# InnoDB
innodb_data_home_dir=/home/mysql/data
innodb_log_group_home_dir=/home/mysql/logs
innodb_data_file_path=ibdata1:128M:autoextend
innodb_buffer_pool_size=2G
innodb_log_file_size=10M
innodb_log_buffer_size=8M
innodb_lock_wait_timeout=50
innodb_file_per_table
innodb_flush_log_at_trx_commit=1
 
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_AUTO_VALUE_ON_ZERO
 
[mysqldump]
quick
max_allowed_packet = 16M
 
[mysql]
no-auto-rehash
 
[myisamchk]
key_buffer_size = 256K
sort_buffer_size = 256K
read_buffer = 256K
write_buffer = 256K
 
[mysqlhotcopy]
interactive-timeout
 
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_AUTO_VALUE_ON_ZERO
 
[mysqldump]
quick
max_allowed_packet = 16M
 
[mysql]
no-auto-rehash
 
[myisamchk]
key_buffer_size = 256K
sort_buffer_size = 256K
read_buffer = 256K
write_buffer = 256K
 
[mysqlhotcopy]
interactive-timeout

相關文章

  • MySQL復制之GTID復制的具體使用

    MySQL復制之GTID復制的具體使用

    從MySQL 5.6.5開始新增了一種基于GTID的復制方式,本文主要介紹了MySQL復制之GTID復制的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 從ibd文件恢復MySQL數(shù)據的操作步驟及常見錯誤

    從ibd文件恢復MySQL數(shù)據的操作步驟及常見錯誤

    MySQL數(shù)據恢復是數(shù)據庫管理中的一項重要任務,尤其是在遭遇意外數(shù)據丟失、硬件故障或軟件錯誤時,下面這篇文章主要給大家介紹了關于從ibd文件恢復MySQL數(shù)據的操作步驟及常見錯誤,需要的朋友可以參考下
    2024-08-08
  • MySQL導出所有Index和約束的方法

    MySQL導出所有Index和約束的方法

    這篇文章主要介紹了MySQL導出所有Index和約束的方法,非常實用的技巧,需要的朋友可以參考下
    2014-08-08
  • mySQL中replace的用法

    mySQL中replace的用法

    MySQL replace函數(shù)我們經常用到,下面就為您詳細介紹MySQL replace函數(shù)的用法,希望對您學習MySQL replace函數(shù)方面能有所啟迪
    2012-09-09
  • mysql全連接和oracle全連接查詢、區(qū)別及說明

    mysql全連接和oracle全連接查詢、區(qū)別及說明

    這篇文章主要介紹了mysql全連接和oracle全連接查詢、區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • mysql alter table 修改表命令詳細介紹

    mysql alter table 修改表命令詳細介紹

    MYSQL ALTER TABLE命令用于修改表結構,例如添加/修改/刪除字段、索引、主鍵等等,本文章通過實例向大家介紹MYSQL ALTER TABLE語句的使用方法,需要的朋友可以參考一下。
    2016-10-10
  • mysql?sql字符串截取函數(shù)詳解

    mysql?sql字符串截取函數(shù)詳解

    mysql支持的字符串截取函數(shù)主要有?left()、right()、substring()、substring_index(),下面是這些函數(shù)的詳細使用方法
    2022-10-10
  • 淺談mysql的中文亂碼問題

    淺談mysql的中文亂碼問題

    本文主要給大家分享了本人在項目中遇到的一些mysql中文亂碼的問題的解決方法,非常簡單實用,這里推薦給大家,有需要的小伙伴可以參考下。
    2015-03-03
  • MySQL一個索引最多有多少個列?真實的測試例子

    MySQL一個索引最多有多少個列?真實的測試例子

    MySQL一個索引最多有多少個列?下面是具體的實現(xiàn)代碼。
    2009-07-07
  • SQL去重方法匯總

    SQL去重方法匯總

    這篇文章主要給大家分享了SQL去重方法匯總,在使用SQL提數(shù)的時候,常會遇到表內有重復值的時候,比如我們想得到?uv?(獨立訪客),就需要做去重。下面我們就來看看去重都有哪些方法吧
    2022-01-01

最新評論

永春县| 无为县| 永定县| 江川县| 蒙自县| 台北县| 探索| 乡城县| 杭锦后旗| 兰州市| 北京市| 同江市| 阜南县| 阳江市| 轮台县| 含山县| 伊金霍洛旗| 万山特区| 无锡市| 丰宁| 镇安县| 宁明县| 临清市| 双流县| 罗源县| 辰溪县| 阜平县| 民勤县| 乐都县| 蕉岭县| 老河口市| 怀远县| 扎赉特旗| 海安县| 英吉沙县| 井研县| 东乌珠穆沁旗| 灌南县| 贡嘎县| 松江区| 靖西县|