mysql學習(3)-linux下mysql主從複製

前言:爲何MySQL要作主從複製(讀寫分離)?
通俗來說,若是對數據庫的讀和寫都在同一個數據庫服務器中操做,業務系統性能會下降。
爲了提高業務系統性能,優化用戶體驗,能夠經過作主從複製(讀寫分離)來減輕主數據庫的負載。
並且若是主數據庫宕機,可快速將業務系統切換到從數據庫上,可避免數據丟失。html

本文提綱java

一.主從複製的概念mysql

二.配置主從複製sql

 

MySQL主從複製(讀寫分離)和集羣的區別:數據庫

一.主從複製相關概念

(讀寫分離):通常須要兩臺及以上數據庫服務器便可(一臺用於寫入數據,一臺用於同步主的數據並用於數據查詢操做)。
侷限性:
(1)配置好主從複製以後,同一張表,只能對一個服務器寫操做。若是在從上執行了寫操做,而以後主也操做了這張表,或致使主從不一樣步;聽說能夠配置成主主方式,但我尚未研究到。
(2)主數據庫服務器宕機,須要手動將業務系統切換到從數據庫服務器。沒法作到高可用性(除非再經過部署keepalive作成高可用方案)。
2.集羣是由N臺數據庫服務器組成,數據的寫入和查詢是隨機到任意一臺數據庫服務器的,其餘數據庫服務器會自動同步數據庫的操做。
任何一臺數據庫宕機,不會對整個集羣形成大的影響。
centos

二.配置主從複製

1.準備工做:
(1)配置MySQL主從複製(讀寫分離)以前,須要在主從兩臺服務器先安裝好MySQL5.6。
(2)目前最新的MySQL5.6 GA版本是MySQL5.6.22
操做系統:centos6.3
服務器

以前有講過源碼包安裝mysql5.6,這裏不作解釋:dom

主數據庫IP:192.168.100.241
從數據庫IP:192.168.100.242
socket

2.修改主從數據庫的my.cnf文件性能

先修改主配置文件模版 /etc/my.cnf(最好先備份原配置文件)

# 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=/var/lib/mysql/mysql-241.sock default-character-set=utf8 [mysqld] explicit_defaults_for_timestamp sync_binlog=1 server-id=1 port=3306 socket=/var/lib/mysql/mysql-241.sock pid-file=/var/lib/mysql/mysql-241.pid #user=mysql datadir=/usr/local/mysql/data log-bin=/usr/local/mysql/data/mysqlmaster-bin log-error=/usr/local/mysql/logs/error.log slow_query_log_file=/usr/local/mysql/logs/slow.log inlog_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=/usr/local/mysql/data innodb_log_group_home_dir=/usr/local/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

3.從配置文件模版

# 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=/var/lib/mysql/mysql-242.sock default-character-set=utf8 [mysqld] sync_binlog=1 server-id=2 port=3306 socket=/var/lib/mysql/mysql-242.sock pid-file=/var/lib/mysql/mysql-242.pid user=mysql datadir=/usr/local/mysql/data tmpdir=/usr/local/mysql/temp/ log-bin=/usr/local/mysql/data/mysqlslave-bin log-error=/usr/local/mysql/logs/error.log slow_query_log_file=/usr/local/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=/usr/local/mysql/data innodb_log_group_home_dir=/usr/local/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

修改以後要重啓mysql:

/etc/init.d/mysql restart 或者service mysql restart (前提是安裝了服務)

4.解決配置過程當中遇到的問題

注意:修改my.cnf文件以後重啓服務通常會遇到問題

MySQL: Starting MySQL….. ERROR! The server quit without updating PID file
1.問題
[root@localhost mysql]# /etc/rc.d/init.d/mysql status MySQL is not running, but lock file (/var/lock/subsys/mysql[FAILED] [root@localhost mysql]# /etc/rc.d/init.d/mysql start Starting MySQL...The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid). [FAILED] 2.緣由 沒有初始化權限表

[root@localhost ~]# cd /usr/local/mysql

 
 

[root@localhost mysql]# chown -R mysql.mysql .
[root@localhost mysql]# su - mysql
[mysql@localhost ~]$ cd /usr/local/mysql
[mysql@localhost mysql]$ scripts/mysql_install_db
Installing MySQL system tables...

OK
Filling help tables...
OK

 
 

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

 
 

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

 
 

Alternatively you can run:
./bin/mysql_secure_installation

 
 

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

 
 

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

 
 

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

 
 

Please report any problems with the ./bin/mysqlbug script!

 
 

[mysql@localhost mysql]$ /usr/local/mysql/bin/mysqld_safe --user=mysql &
[1] 11767

[mysql@localhost mysql]$ 120502 07:01:17 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
120502 07:01:17 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
[mysql@localhost mysql]$ /etc/rc.d/init.d/mysql status
MySQL running (11830)                                      [  OK  ]

[mysql@localhost mysql]$ /etc/rc.d/init.d/mysql start
Starting MySQL                                             [  OK  ]

注意:master和slave的server-id必定不能相同,這裏master:server-id=1,slave:server-id=2

配置時由於粗心將兩個server-id=1,查了好多資料才解決。

5.設置主從同步

登陸到主數據庫:
(1).在主數據庫上建立用於主從複製的帳戶(192.168.100.242換成你的從數據庫IP):

GRANT REPLICATION SLAVE ON *.* TO 'java'@'192.168.100.242' IDENTIFIED BY 'java@123456';

(2).主數據庫鎖表(禁止再插入數據以獲取主數據庫的的二進制日誌座標):

FLUSH TABLES WITH READ LOCK;

(3).打開MySQL命令行:

 #mysql -uroot -p mysql> SHOW MASTER STATUS;

+------------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------------+----------+--------------+------------------+-------------------+

| mysqlmaster-bin.000002 |      120 |              |                  |                   |
+------------------------+----------+--------------+------------------+-------------------+ row in set (0.00 sec) mysql>exit;

查詢結果顯示:在這個例子中,二進制日誌文件是mysqlmaster-bin.000002,位置是120,記錄下這兩個值,稍後要用到。

(4).在主數據庫上使用mysqldump命令備份一下數據庫:

mysqldump -uroot -p jobs > /usr/local/mysql/jobs.sql

SSH登陸到從數據庫:

(1).經過FTP、SFTP或其餘方式,將上一步備份的主數據庫快照jobs.sql上傳到從數據庫某個路徑,例如我放在了/user/local/mysql目錄下;
(2).從導入主庫的備份:

#建立數空據庫
CREATE DATABASE `jobs` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
#mysqldump 恢復數據
mysql -uroot -p -B jobs </usr/local/mysql/jobs.sql

(3).給從數據庫設置複製的主數據庫信息(注意修改MASTER_LOG_FILE和MASTER_LOG_POS的值)

#中止從數據庫的複製線程 stop slave; #給從數據庫設置複製的主數據庫信息 CHANGE MASTER TO MASTER_HOST='192.168.100.241',MASTER_USER='java',MASTER_PASSWORD='java@1228',MASTER_LOG_FILE='mysqlmaster-bin.000002',
MASTER_LOG_POS=120;
#啓動從數據庫的複製線程
start slave;

(4).接着查詢數據庫的slave狀態:

mysql> SHOW slave STATUS \G #若是下面兩個參數都是Yes,則說明主從配置成功! Slave_IO_Running:Yes Slave_SQL_Running:Yes

(4).接下來你能夠在主數據庫上建立數據庫、表、插入數據,而後看從數據庫是否同步了這些操做.

 可能會遇到 "slave_io_running: no"問題

解決辦法

mysql>stop slave ; mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; mysql>start slave ; 

而後在查看主從狀態

mysql> SHOW slave STATUS \G #若是下面兩個參數都是Yes,則說明主從配置成功! Slave_IO_Running:Yes Slave_SQL_Running:Yes
相關文章
相關標籤/搜索