Mysql主從方案的實現

Mysql主從方案介紹php

 

mysql主從方案主要做用:css

讀寫分離,使數據庫能支撐更大的併發。在報表中尤爲重要。因爲部分報表sql語句很是的慢,致使鎖表,影響前臺服務。若是前臺使用master,報表使用slave,那麼報表sql將不會形成前臺鎖,保證了前臺速度。mysql

發揚不一樣表引擎的優勢。目前Myisam表的查詢速度比innodb略快,而寫入併發innodbmyIsam要好。那麼,咱們可使用innodb做爲master,處理高併發寫入,使用master做爲slave,接受查詢。或在myisam slave中創建全文索引,解決innodb無全文索引的弱點。sql

熱備,slavemaster的數據準實時同步。數據庫

準備工做。先分別安裝兩臺MYSQL服務器

 

系統環境:併發

OSRHEL5.4socket

主:192.168.10.197ide

從:192.168.10.198高併發

 

一、mysql 的安裝這裏就不介紹了,詳見安裝文檔。

二、my.cnf配置

a、配置MASTER

點擊(此處)摺疊或打開

  1. [client]
  2. port = 3306
  3. socket = /tmp/mysql.sock
  4. [mysqld]
  5. port = 3306
  6. socket = /tmp/mysql.sock
  7. basedir=/usr/local/mysql
  8. datadir=/home/mysqldata
  9. log-slow-queries=slow_query.txt
  10. log-bin=mysql-bin197
  11. long_query_time=2
  12. skip-locking
  13. skip-name-resolve
  14. skip-innodb
  15. bind-address=192.168.10.197
  16. max_allowed_packet = 256M
  17. query_cache_size=256M
  18. max_connections=2000
  19. max_connect_errors=10000
  20. key_buffer_size=6000M
  21. read_buffer_size=32M
  22. read_rnd_buffer_size = 32M
  23. myisam_sort_buffer_size=256M
  24. tmp_table_size=512M
  25. old-passwords
  26. interactive_timeout=60
  27. wait_timeout=60
  28. connect_timeout=120
  29. table_cache=8192
  30. thread_cache_size=256
  31. sort_buffer_size=64M
  32. back_log = 500
  33. thread_concurrency=32
  34. server-id=1
  35.  
  36. log-bin=mysql-bin240
  37.  
  38. binlog-do-db=phpcmsv9
  39.  
  40. binlog-ignore-db=mysql
  41.  
  42. expire_logs_days=10
  43. [mysqldump]
  44. quick
  45. max_allowed_packet = 1024M
  46. [mysql]
  47. no-auto-rehash
  48. [isamchk]
  49. key_buffer = 1024M
  50. sort_buffer_size = 32M
  51. read_buffer = 2M
  52. write_buffer = 2M
  53. [myisamchk]
  54. key_buffer = 1024M
  55. sort_buffer_size = 32M
  56. read_buffer = 2M
  57. write_buffer = 2M
  58. [mysqlhotcopy]
  59. interactive-timeout

 

註釋:紅色是修改的部分。

其中,做爲主機,server-id必須爲1.

binlog_do_db爲須要複製的db。 binlog_ignore_db爲忽略複製的db。須要增長DB的話,就增長相應的一行。

重啓master數據庫,運行檢查:

點擊(此處)摺疊或打開

  1. mysql> show master status; #檢查是否以master形式啓動了。
  2. +---------------------+----------+--------------+------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  4. +---------------------+----------+--------------+------------------+
  5. | mysql-bin240.000001 | 2342775 | phpcmsv9 | mysql |
  6. +---------------------+----------+--------------+------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> show variables like "%log%";

 

#須要看到這樣的一行,說明binlog已經開啓了: log_bin | ON

 

master上爲slave創建用戶

 

點擊(此處)摺疊或打開

  1. mysql> grant replication slave, reload, super on *.* to 'backup'@'192.168.10.198' identified by '123';

這樣,主機配置完畢。
b
slave配置

點擊(此處)摺疊或打開

  1. [client]
  2. port = 3306
  3. socket = /tmp/mysql.sock
  4. [mysqld]
  5. port = 3306
  6. socket = /tmp/mysql.sock
  7. basedir=/usr/local/mysql
  8. datadir=/home/mysqldata
  9. log-slow-queries=slow_query.txt
  10. log-bin=mysql-bin198
  11. long_query_time=2
  12. skip-locking
  13. bind-address=192.168.10.198
  14. skip-name-resolve
  15. skip-innodb
  16. max_allowed_packet = 256M
  17. query_cache_size=256M
  18. max_connections=2000
  19. max_connect_errors=10000
  20. key_buffer_size=6000M
  21. read_buffer_size=32M
  22. read_rnd_buffer_size = 32M
  23. myisam_sort_buffer_size=256M
  24. tmp_table_size=512M
  25. old-passwords
  26. interactive_timeout=60
  27. wait_timeout=60
  28. connect_timeout=120
  29. table_cache=8192
  30. thread_cache_size=256
  31. sort_buffer_size=64M
  32. back_log = 500
  33. thread_concurrency=8
  34.  
  35. server-id=2
  36.  
  37. master-host=192.168.10.197
  38.  
  39. master-user=backup
  40.  
  41. master-password=123
  42.  
  43. master-port=3306
  44. replicate-do-db=phpcmsv9
  45.  
  46. replicate-ignore-db=mysql
  47.  
  48. master-connect-retry=60
  49.  
  50. expire_logs_days=10
  51. [mysqldump]
  52. quick
  53. max_allowed_packet = 1024M
  54. [mysql]
  55. no-auto-rehash
  56. [isamchk]
  57. key_buffer = 1024M
  58. sort_buffer_size = 32M
  59. read_buffer = 2M
  60. write_buffer = 2M
  61. [myisamchk]
  62. key_buffer = 1024M
  63. sort_buffer_size = 32M
  64. read_buffer = 2M
  65. write_buffer = 2M
  66. [mysqlhotcopy]
  67. interactive-timeout

 

註釋:紅色部分爲修改的地方

1.啓動主服務器和從服務器服務,在從服務器192.168.17.2上使用命令slave start啓動複製;

2.隨便使用命令show slave status;

若是出現主從複製報錯了

 

點擊(此處)摺疊或打開

  1. mysql> show slave status \G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Connecting to master
  4. Master_Host: 192.168.10.197
  5. Master_User: backup
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin240.000001
  9. Read_Master_Log_Pos: 2342431
  10. Relay_Log_File: localhost-relay-bin.000001
  11. Relay_Log_Pos: 4
  12. Relay_Master_Log_File: mysql-bin240.000001
  13. Slave_IO_Running: No
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB: phpcmsv9
  16. Replicate_Ignore_DB: mysql
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 2342431
  25. Relay_Log_Space: 106
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: NULL
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 1130
  38. Last_IO_Error: error connecting to master 'backup@192.168.10.197:3306' - retry-time: 60 retries: 86400
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. 1 row in set (0.00 sec)
  42. ERROR:
  43. No query specified

 

這是因爲MASTER沒有賦予權限的關係

3在從服務器192.168.17.2運行slave stop;中止複製命令

 

4輸入

點擊(此處)摺疊或打開

  1. mysql>CHANGE MASTER to MASTER_HOST='192.168.15.197', MASTER_PORT=3306, MASTER_USER='backup', MASTER_PASSWORD='123', MASTER_LOG_FILE=’mysql-bin240.000001';, MASTER_LOG_POS=2342775;

 

 

5而後從新啓動slave start 命令後

點擊(此處)摺疊或打開

  1. mysql> show slave status \G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.10.197
  5. Master_User: backup
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin240.000001
  9. Read_Master_Log_Pos: 2342775
  10. Relay_Log_File: localhost-relay-bin.000002
  11. Relay_Log_Pos: 598
  12. Relay_Master_Log_File: mysql-bin240.000001
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB: phpcmsv9
  16. Replicate_Ignore_DB: mysql
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 2342775
  25. Relay_Log_Space: 757
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. 1 row in set (0.00 sec)
  42. ERROR:
  43. No query specified

 

在從庫192.168.17.2 select查詢,發現後面插入的兩條語句已經同步過來了,隨後繼續插入測試沒有發現問題。

相關文章
相關標籤/搜索