linux6.4搭建mysql主從複製
阿新 • • 發佈:2019-01-04
1 複製概述
Mysql內建的複製功能是構建大型,高效能應用程式的基礎。將Mysql的資料分佈到多個系統上去,這種分佈的機制,是通過將Mysql的某一臺主機的資料複製到其它主機(slaves)上,並重新執行一遍來實現的。複製過程中一個伺服器充當主伺服器,而一個或多個其它伺服器充當從伺服器。主伺服器將更新寫入二進位制日誌檔案,並維護檔案的一個索引以跟蹤日誌迴圈。這些日誌可以記錄傳送到從伺服器的更新。當一個從伺服器連線主伺服器時,它通知主伺服器從伺服器在日誌中讀取的最後一次成功更新的位置。從伺服器接收從那時起發生的任何更新,然後封鎖並等待主伺服器通知新的更新。
(1):基於語句的複製: 在主伺服器上執行的SQL語句,在從伺服器上執行同樣的語句。MySQL預設採用基於語句的複製,效率比較高。
一旦發現沒法精確複製時, 會自動選著基於行的複製。
(2):基於行的複製:把改變的內容複製過去,而不是把命令在從伺服器上執行一遍. 從mysql5.0開始支援
(3):混合型別的複製: 預設採用基於語句的複製,一旦發現基於語句的無法精確的複製時,就會採用基於行的複製。
1.2 . 複製解決的問題
MySQL複製技術有以下一些特點:
(1) 資料分佈 (Data distribution )
(2) 負載平衡(load balancing)
(3) 備份(Backups)
(4) 高可用性和容錯行 High availability and failover
1.3 複製如何工作
整體上來說,複製有3個步驟:
(1) master將改變記錄到二進位制日誌(binary log)中(這些記錄叫做二進位制日誌事件,binary log events);
(2) slave將master的binary log events拷貝到它的中繼日誌(relay log);
(3) slave重做中繼日誌中的事件,將改變反映它自己的資料。
環境準備:
192.168.20.10 master
192.168.20.11 slave
第一、主機上安裝mysql
1、檢視是否預設安裝了mysql
[ [email protected] ~]# rpm -qa|grep mysql
libdbi-dbd-mysql-0.8.3-5.1.el6.x86_64
mysql-libs-5.1.66-2.el6_3.x86_64
mysql-bench-5.1.66-2.el6_3.x86_64
mysql-connector-java-5.1.17-6.el6.noarch
mysql-test-5.1.66-2.el6_3.x86_64
mod_auth_mysql-3.0.0-11.el6_0.1.x86_64
mysql-connector-odbc-5.1.5r1144-7.el6.x86_64
mysql-5.1.66-2.el6_3.x86_64
mysql-devel-5.1.66-2.el6_3.x86_64
php-mysql-5.3.3-22.el6.x86_64
rsyslog-mysql-5.8.10-6.el6.x86_64
mysql-server-5.1.66-2.el6_3.x86_64
qt-mysql-4.6.2-25.el6.x86_64
dovecot-mysql-2.0.9-5.el6.x86_64
2、使用yum解除安裝mysql
[ [email protected] ~]# yum -y remove mysql-5.1.66-2.el6_3.x86_64
Loaded plugins: aliases, changelog, downloadonly, kabi, presto, product-id, security, subscription-manager, tmprepo, verify,
: versionlock
..........................
Complete!
[[email protected] ~]# rpm -qa|grep mysql
mysql-libs-5.1.66-2.el6_3.x86_64
mysql-connector-java-5.1.17-6.el6.noarch
mod_auth_mysql-3.0.0-11.el6_0.1.x86_64
mysql-connector-odbc-5.1.5r1144-7.el6.x86_64
php-mysql-5.3.3-22.el6.x86_64
rsyslog-mysql-5.8.10-6.el6.x86_64
qt-mysql-4.6.2-25.el6.x86_64
dovecot-mysql-2.0.9-5.el6.x86_64
[[email protected] ~]# yum -y remove mysql-libs-5.1.66-2.el6_3.x86_64
.........
Complete!
[[email protected] ~]# rpm -qa|grep mysql
mysql-connector-java-5.1.17-6.el6.noarch
[[email protected] ~]# yum -y remove mysql-connector-java-5.1.17-6.el6.noarch
Loaded plugins: aliases, changelog, downloadonly, kabi, presto, product-id, security, subscription-manager, tmprepo, verify,
: versionlock
...........
Complete!
3、主從節點安裝mysql:
[[email protected] install]# ls
MySQL-5.6.37-1.el6.x86_64.rpm-bundle.tar
[[email protected] install]# tar xvf MySQL-5.6.37-1.el6.x86_64.rpm-bundle.tar
MySQL-shared-5.6.37-1.el6.x86_64.rpm
MySQL-shared-compat-5.6.37-1.el6.x86_64.rpm
MySQL-server-5.6.37-1.el6.x86_64.rpm
MySQL-test-5.6.37-1.el6.x86_64.rpm
MySQL-client-5.6.37-1.el6.x86_64.rpm
MySQL-devel-5.6.37-1.el6.x86_64.rpm
MySQL-embedded-5.6.37-1.el6.x86_64.rpm
[[email protected] install]# rpm -ivh MySQL-server-5.6.37-1.el6.x86_64.rpm
warning: MySQL-server-5.6.37-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-server ########################################### [100%]
2017-07-24 12:01:48 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-07-24 12:01:48 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-07-24 12:01:48 0 [Note] /usr/sbin/mysqld (mysqld 5.6.37) starting as process 36859 ...
2017-07-24 12:01:48 36859 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-07-24 12:01:48 36859 [Note] InnoDB: The InnoDB memory heap is disabled
2017-07-24 12:01:48 36859 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-07-24 12:01:48 36859 [Note] InnoDB: Memory barrier is not used
2017-07-24 12:01:48 36859 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-07-24 12:01:48 36859 [Note] InnoDB: Using Linux native AIO
2017-07-24 12:01:48 36859 [Note] InnoDB: Using CPU crc32 instructions
2017-07-24 12:01:48 36859 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-07-24 12:01:48 36859 [Note] InnoDB: Completed initialization of buffer pool
2017-07-24 12:01:48 36859 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2017-07-24 12:01:48 36859 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2017-07-24 12:01:48 36859 [Note] InnoDB: Database physically writes the file full: wait...
2017-07-24 12:01:48 36859 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2017-07-24 12:01:48 36859 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2017-07-24 12:01:48 36859 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2017-07-24 12:01:48 36859 [Warning] InnoDB: New log files created, LSN=45781
2017-07-24 12:01:48 36859 [Note] InnoDB: Doublewrite buffer not found: creating new
2017-07-24 12:01:48 36859 [Note] InnoDB: Doublewrite buffer created
2017-07-24 12:01:48 36859 [Note] InnoDB: 128 rollback segment(s) are active.
2017-07-24 12:01:48 36859 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-07-24 12:01:48 36859 [Note] InnoDB: Foreign key constraint system tables created
2017-07-24 12:01:48 36859 [Note] InnoDB: Creating tablespace and datafile system tables.
2017-07-24 12:01:48 36859 [Note] InnoDB: Tablespace and datafile system tables created.
2017-07-24 12:01:48 36859 [Note] InnoDB: Waiting for purge to start
2017-07-24 12:01:48 36859 [Note] InnoDB: 5.6.37 started; log sequence number 0
A random root password has been set. You will find it in '/root/.mysql_secret'.
2017-07-24 12:01:49 36859 [Note] Binlog end
2017-07-24 12:01:49 36859 [Note] InnoDB: FTS optimize thread exiting.
2017-07-24 12:01:49 36859 [Note] InnoDB: Starting shutdown...
2017-07-24 12:01:50 36859 [Note] InnoDB: Shutdown completed; log sequence number 1625977
2017-07-24 12:01:50 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-07-24 12:01:50 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-07-24 12:01:50 0 [Note] /usr/sbin/mysqld (mysqld 5.6.37) starting as process 36885 ...
2017-07-24 12:01:50 36885 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-07-24 12:01:50 36885 [Note] InnoDB: The InnoDB memory heap is disabled
2017-07-24 12:01:50 36885 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-07-24 12:01:50 36885 [Note] InnoDB: Memory barrier is not used
2017-07-24 12:01:50 36885 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-07-24 12:01:50 36885 [Note] InnoDB: Using Linux native AIO
2017-07-24 12:01:50 36885 [Note] InnoDB: Using CPU crc32 instructions
2017-07-24 12:01:50 36885 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-07-24 12:01:50 36885 [Note] InnoDB: Completed initialization of buffer pool
2017-07-24 12:01:50 36885 [Note] InnoDB: Highest supported file format is Barracuda.
2017-07-24 12:01:50 36885 [Note] InnoDB: 128 rollback segment(s) are active.
2017-07-24 12:01:50 36885 [Note] InnoDB: Waiting for purge to start
2017-07-24 12:01:50 36885 [Note] InnoDB: 5.6.37 started; log sequence number 1625977
2017-07-24 12:01:50 36885 [Note] Binlog end
2017-07-24 12:01:50 36885 [Note] InnoDB: FTS optimize thread exiting.
2017-07-24 12:01:50 36885 [Note] InnoDB: Starting shutdown...
2017-07-24 12:01:52 36885 [Note] InnoDB: Shutdown completed; log sequence number 1625987
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database.
This is strongly recommended for production servers.
See the manual for more instructions.
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
[[email protected] install]# rpm -ivh MySQL-client-5.6.37-1.el6.x86_64.rpm
warning: MySQL-client-5.6.37-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
[[email protected] mysql]# id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)
[[email protected] mysql]#
第二、配置主從複製
master節點:
修改/etc/my.cnf
[mysqld]
datadir = /u02/mysql
server_id = 1
port = 3306
socket = /var/lib/mysql/mysql.sock
log-error=/u02/mysql/mysqld.log
log-bin = /u02/mysql/master-bin
binlog_format = row
skip-name-resolve
sync_binlog = 1
default_storage_engine = innodb
log_slave_updates = 1
read_only = 0
[[email protected] mysql]# service mysql start
Starting MySQL.Logging to '/var/lib/mysql/master.err'.
[OK]
新增測試資料:
-bash-4.1$ mysql -p -u root
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.37
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database dtxx;
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| dtxx |
| hello |
| #mysql50#master-bin |
| mysql |
| performance_schema |
| test |
+---------------------+
8 rows in set (0.00 sec)
mysql> use dtxx
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_dtxx |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
建立具有複製許可權的賬號
mysql>grant replication slave on *.* to [email protected]'192.168.20.%' identified by 'repluser';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
備份master資料庫
[[email protected] mysql]$ mysqldump --single-transaction --all-databases --master-data=2 > myback.sql #完全備份
[[email protected] mysql]$scp myback.sql [email protected]:/u02/mysql
配置從庫:
[mysqld]
datadir = /u02/mysql
port = 3306
server_id = 2
socket = /var/lib/mysql/mysql.sock
sync_binlog = 1
skip-name-resolve
relay-log = /u02/mysql/slave-relay #啟用中繼日誌
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
read_only = on #只讀
在從庫上恢復主庫:
[[email protected] ~]# service mysqld start
Starting MySQL SUCCESS!
[[email protected] ~]# mysql -p12345 -uroot < myback.sql #先將主庫的備份資料恢復至從庫
檢視備份日誌裡主庫的log_file跟log_pos:
-bash-4.1$ grep "^-- CHANGE" myback.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000004', MASTER_LOG_POS=1361;
使用有複製許可權的賬號連線master:
mysql> change master to master_host='192.168.20.10',master_user='repluser',master_password='repluser',\
-> master_log_file='master-bin.000004',master_log_pos=1361;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
檢視狀態:
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.81.35.2
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000004
Read_Master_Log_Pos: 1361
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: master-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: 1361
Relay_Log_Space: 453
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: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
Master_Info_File: /u02/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)
-bash-4.1$ more /u02/mysql/master.info
23
master-bin.000004
1361
10.81.35.2
repluser
repluser
3306
60
0
0
1800.000
0
cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
86400
0
測試:
mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)
mysql> use testdb;
Database changed
mysql> create table students (Name char(30),Gender enum('m','f'));
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| dtxx |
| #mysql50#master-bin |
| mysql |
| performance_schema |
| test |
| testdb |
+---------------------+
7 rows in set (0.00 sec)
mysql> show tables from testdb;
+------------------+
| Tables_in_testdb |
+------------------+
| students |
+------------------+
1 row in set (0.00 sec)
mysql> show processlist;
+----+----------+--------------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+----------+--------------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
| 16 | repluser | master.cdterp.com:22298 | NULL | Binlog Dump | 195 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 17 | root | localhost | testdb | Query | 0 | init | show processlist |
+----+----------+--------------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
從庫:
-bash-4.1$ more /u02/mysql/master.info
23
master-bin.000004
1597
10.81.35.2
repluser
repluser
3306
60
0
0
1800.000
0
cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
86400
0
-bash-4.1$ mysql -p12345 -uroot
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.6.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show slave status \GG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.81.35.2
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000004
Read_Master_Log_Pos: 1597 --這個從1361到了1597
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 520
Relay_Master_Log_File: master-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: 1597
Relay_Log_Space: 689
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: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
Master_Info_File: /u02/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)
mysql> SHOW DATABASES;
+----------------------+
| Database |
+----------------------+
| information_schema |
| dtxx |
| #mysql50#master-bin |
| mysql |
| performance_schema |
| #mysql50#slave-relay |
| test |
| testdb |
+---------------------+
7 rows in set (0.00 sec)
Mysql內建的複製功能是構建大型,高效能應用程式的基礎。將Mysql的資料分佈到多個系統上去,這種分佈的機制,是通過將Mysql的某一臺主機的資料複製到其它主機(slaves)上,並重新執行一遍來實現的。複製過程中一個伺服器充當主伺服器,而一個或多個其它伺服器充當從伺服器。主伺服器將更新寫入二進位制日誌檔案,並維護檔案的一個索引以跟蹤日誌迴圈。這些日誌可以記錄傳送到從伺服器的更新。當一個從伺服器連線主伺服器時,它通知主伺服器從伺服器在日誌中讀取的最後一次成功更新的位置。從伺服器接收從那時起發生的任何更新,然後封鎖並等待主伺服器通知新的更新。
請注意當你進行復制時,所有對複製中的表的更新必須在主伺服器上進行。否則,你必須要小心,以避免使用者對主伺服器上的表進行的更新與對從伺服器上的表所進行的更新之間的衝突。
1.1 mysql支援的複製型別:
(1):基於語句的複製: 在主伺服器上執行的SQL語句,在從伺服器上執行同樣的語句。MySQL預設採用基於語句的複製,效率比較高。
一旦發現沒法精確複製時, 會自動選著基於行的複製。
(2):基於行的複製:把改變的內容複製過去,而不是把命令在從伺服器上執行一遍. 從mysql5.0開始支援
(3):混合型別的複製: 預設採用基於語句的複製,一旦發現基於語句的無法精確的複製時,就會採用基於行的複製。
1.2 . 複製解決的問題
MySQL複製技術有以下一些特點:
(1) 資料分佈 (Data distribution )
(2) 負載平衡(load balancing)
(3) 備份(Backups)
(4) 高可用性和容錯行 High availability and failover
1.3 複製如何工作
整體上來說,複製有3個步驟:
(1) master將改變記錄到二進位制日誌(binary log)中(這些記錄叫做二進位制日誌事件,binary log events);
(2) slave將master的binary log events拷貝到它的中繼日誌(relay log);
(3) slave重做中繼日誌中的事件,將改變反映它自己的資料。
環境準備:
192.168.20.10 master
192.168.20.11 slave
第一、主機上安裝mysql
1、檢視是否預設安裝了mysql
[
libdbi-dbd-mysql-0.8.3-5.1.el6.x86_64
mysql-libs-5.1.66-2.el6_3.x86_64
mysql-bench-5.1.66-2.el6_3.x86_64
mysql-connector-java-5.1.17-6.el6.noarch
mysql-test-5.1.66-2.el6_3.x86_64
mod_auth_mysql-3.0.0-11.el6_0.1.x86_64
mysql-connector-odbc-5.1.5r1144-7.el6.x86_64
mysql-5.1.66-2.el6_3.x86_64
mysql-devel-5.1.66-2.el6_3.x86_64
php-mysql-5.3.3-22.el6.x86_64
rsyslog-mysql-5.8.10-6.el6.x86_64
mysql-server-5.1.66-2.el6_3.x86_64
qt-mysql-4.6.2-25.el6.x86_64
dovecot-mysql-2.0.9-5.el6.x86_64
2、使用yum解除安裝mysql
[
Loaded plugins: aliases, changelog, downloadonly, kabi, presto, product-id, security, subscription-manager, tmprepo, verify,
: versionlock
..........................
Complete!
[[email protected] ~]# rpm -qa|grep mysql
mysql-libs-5.1.66-2.el6_3.x86_64
mysql-connector-java-5.1.17-6.el6.noarch
mod_auth_mysql-3.0.0-11.el6_0.1.x86_64
mysql-connector-odbc-5.1.5r1144-7.el6.x86_64
php-mysql-5.3.3-22.el6.x86_64
rsyslog-mysql-5.8.10-6.el6.x86_64
qt-mysql-4.6.2-25.el6.x86_64
dovecot-mysql-2.0.9-5.el6.x86_64
[[email protected] ~]# yum -y remove mysql-libs-5.1.66-2.el6_3.x86_64
.........
Complete!
[[email protected] ~]# rpm -qa|grep mysql
mysql-connector-java-5.1.17-6.el6.noarch
[[email protected] ~]# yum -y remove mysql-connector-java-5.1.17-6.el6.noarch
Loaded plugins: aliases, changelog, downloadonly, kabi, presto, product-id, security, subscription-manager, tmprepo, verify,
: versionlock
...........
Complete!
3、主從節點安裝mysql:
[[email protected] install]# ls
MySQL-5.6.37-1.el6.x86_64.rpm-bundle.tar
[[email protected] install]# tar xvf MySQL-5.6.37-1.el6.x86_64.rpm-bundle.tar
MySQL-shared-5.6.37-1.el6.x86_64.rpm
MySQL-shared-compat-5.6.37-1.el6.x86_64.rpm
MySQL-server-5.6.37-1.el6.x86_64.rpm
MySQL-test-5.6.37-1.el6.x86_64.rpm
MySQL-client-5.6.37-1.el6.x86_64.rpm
MySQL-devel-5.6.37-1.el6.x86_64.rpm
MySQL-embedded-5.6.37-1.el6.x86_64.rpm
[[email protected] install]# rpm -ivh MySQL-server-5.6.37-1.el6.x86_64.rpm
warning: MySQL-server-5.6.37-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-server ########################################### [100%]
2017-07-24 12:01:48 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-07-24 12:01:48 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-07-24 12:01:48 0 [Note] /usr/sbin/mysqld (mysqld 5.6.37) starting as process 36859 ...
2017-07-24 12:01:48 36859 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-07-24 12:01:48 36859 [Note] InnoDB: The InnoDB memory heap is disabled
2017-07-24 12:01:48 36859 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-07-24 12:01:48 36859 [Note] InnoDB: Memory barrier is not used
2017-07-24 12:01:48 36859 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-07-24 12:01:48 36859 [Note] InnoDB: Using Linux native AIO
2017-07-24 12:01:48 36859 [Note] InnoDB: Using CPU crc32 instructions
2017-07-24 12:01:48 36859 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-07-24 12:01:48 36859 [Note] InnoDB: Completed initialization of buffer pool
2017-07-24 12:01:48 36859 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2017-07-24 12:01:48 36859 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2017-07-24 12:01:48 36859 [Note] InnoDB: Database physically writes the file full: wait...
2017-07-24 12:01:48 36859 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2017-07-24 12:01:48 36859 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2017-07-24 12:01:48 36859 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2017-07-24 12:01:48 36859 [Warning] InnoDB: New log files created, LSN=45781
2017-07-24 12:01:48 36859 [Note] InnoDB: Doublewrite buffer not found: creating new
2017-07-24 12:01:48 36859 [Note] InnoDB: Doublewrite buffer created
2017-07-24 12:01:48 36859 [Note] InnoDB: 128 rollback segment(s) are active.
2017-07-24 12:01:48 36859 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-07-24 12:01:48 36859 [Note] InnoDB: Foreign key constraint system tables created
2017-07-24 12:01:48 36859 [Note] InnoDB: Creating tablespace and datafile system tables.
2017-07-24 12:01:48 36859 [Note] InnoDB: Tablespace and datafile system tables created.
2017-07-24 12:01:48 36859 [Note] InnoDB: Waiting for purge to start
2017-07-24 12:01:48 36859 [Note] InnoDB: 5.6.37 started; log sequence number 0
A random root password has been set. You will find it in '/root/.mysql_secret'.
2017-07-24 12:01:49 36859 [Note] Binlog end
2017-07-24 12:01:49 36859 [Note] InnoDB: FTS optimize thread exiting.
2017-07-24 12:01:49 36859 [Note] InnoDB: Starting shutdown...
2017-07-24 12:01:50 36859 [Note] InnoDB: Shutdown completed; log sequence number 1625977
2017-07-24 12:01:50 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-07-24 12:01:50 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-07-24 12:01:50 0 [Note] /usr/sbin/mysqld (mysqld 5.6.37) starting as process 36885 ...
2017-07-24 12:01:50 36885 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-07-24 12:01:50 36885 [Note] InnoDB: The InnoDB memory heap is disabled
2017-07-24 12:01:50 36885 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-07-24 12:01:50 36885 [Note] InnoDB: Memory barrier is not used
2017-07-24 12:01:50 36885 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-07-24 12:01:50 36885 [Note] InnoDB: Using Linux native AIO
2017-07-24 12:01:50 36885 [Note] InnoDB: Using CPU crc32 instructions
2017-07-24 12:01:50 36885 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-07-24 12:01:50 36885 [Note] InnoDB: Completed initialization of buffer pool
2017-07-24 12:01:50 36885 [Note] InnoDB: Highest supported file format is Barracuda.
2017-07-24 12:01:50 36885 [Note] InnoDB: 128 rollback segment(s) are active.
2017-07-24 12:01:50 36885 [Note] InnoDB: Waiting for purge to start
2017-07-24 12:01:50 36885 [Note] InnoDB: 5.6.37 started; log sequence number 1625977
2017-07-24 12:01:50 36885 [Note] Binlog end
2017-07-24 12:01:50 36885 [Note] InnoDB: FTS optimize thread exiting.
2017-07-24 12:01:50 36885 [Note] InnoDB: Starting shutdown...
2017-07-24 12:01:52 36885 [Note] InnoDB: Shutdown completed; log sequence number 1625987
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database.
This is strongly recommended for production servers.
See the manual for more instructions.
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
[[email protected] install]# rpm -ivh MySQL-client-5.6.37-1.el6.x86_64.rpm
warning: MySQL-client-5.6.37-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
[[email protected] mysql]# id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)
[[email protected] mysql]#
第二、配置主從複製
master節點:
修改/etc/my.cnf
[mysqld]
datadir = /u02/mysql
server_id = 1
port = 3306
socket = /var/lib/mysql/mysql.sock
log-error=/u02/mysql/mysqld.log
log-bin = /u02/mysql/master-bin
binlog_format = row
skip-name-resolve
sync_binlog = 1
default_storage_engine = innodb
log_slave_updates = 1
read_only = 0
[[email protected] mysql]# service mysql start
Starting MySQL.Logging to '/var/lib/mysql/master.err'.
[OK]
新增測試資料:
-bash-4.1$ mysql -p -u root
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.37
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database dtxx;
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| dtxx |
| hello |
| #mysql50#master-bin |
| mysql |
| performance_schema |
| test |
+---------------------+
8 rows in set (0.00 sec)
mysql> use dtxx
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_dtxx |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
建立具有複製許可權的賬號
mysql>grant replication slave on *.* to [email protected]'192.168.20.%' identified by 'repluser';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
備份master資料庫
[[email protected] mysql]$ mysqldump --single-transaction --all-databases --master-data=2 > myback.sql #完全備份
[[email protected] mysql]$scp myback.sql [email protected]:/u02/mysql
配置從庫:
[mysqld]
datadir = /u02/mysql
port = 3306
server_id = 2
socket = /var/lib/mysql/mysql.sock
sync_binlog = 1
skip-name-resolve
relay-log = /u02/mysql/slave-relay #啟用中繼日誌
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
read_only = on #只讀
在從庫上恢復主庫:
[[email protected] ~]# service mysqld start
Starting MySQL SUCCESS!
[[email protected] ~]# mysql -p12345 -uroot < myback.sql #先將主庫的備份資料恢復至從庫
檢視備份日誌裡主庫的log_file跟log_pos:
-bash-4.1$ grep "^-- CHANGE" myback.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000004', MASTER_LOG_POS=1361;
使用有複製許可權的賬號連線master:
mysql> change master to master_host='192.168.20.10',master_user='repluser',master_password='repluser',\
-> master_log_file='master-bin.000004',master_log_pos=1361;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
檢視狀態:
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.81.35.2
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000004
Read_Master_Log_Pos: 1361
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: master-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: 1361
Relay_Log_Space: 453
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: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
Master_Info_File: /u02/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)
-bash-4.1$ more /u02/mysql/master.info
23
master-bin.000004
1361
10.81.35.2
repluser
repluser
3306
60
0
0
1800.000
0
cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
86400
0
測試:
mysql> create database testdb;
Query OK, 1 row affected (0.00 sec)
mysql> use testdb;
Database changed
mysql> create table students (Name char(30),Gender enum('m','f'));
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| dtxx |
| #mysql50#master-bin |
| mysql |
| performance_schema |
| test |
| testdb |
+---------------------+
7 rows in set (0.00 sec)
mysql> show tables from testdb;
+------------------+
| Tables_in_testdb |
+------------------+
| students |
+------------------+
1 row in set (0.00 sec)
mysql> show processlist;
+----+----------+--------------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+----------+--------------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
| 16 | repluser | master.cdterp.com:22298 | NULL | Binlog Dump | 195 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 17 | root | localhost | testdb | Query | 0 | init | show processlist |
+----+----------+--------------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
從庫:
-bash-4.1$ more /u02/mysql/master.info
23
master-bin.000004
1597
10.81.35.2
repluser
repluser
3306
60
0
0
1800.000
0
cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
86400
0
-bash-4.1$ mysql -p12345 -uroot
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.6.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show slave status \GG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.81.35.2
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000004
Read_Master_Log_Pos: 1597 --這個從1361到了1597
Relay_Log_File: slave-relay.000002
Relay_Log_Pos: 520
Relay_Master_Log_File: master-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: 1597
Relay_Log_Space: 689
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: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8
Master_Info_File: /u02/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)
mysql> SHOW DATABASES;
+----------------------+
| Database |
+----------------------+
| information_schema |
| dtxx |
| #mysql50#master-bin |
| mysql |
| performance_schema |
| #mysql50#slave-relay |
| test |
| testdb |
+---------------------+
7 rows in set (0.00 sec)