mysql主從搭建:mysql
怎麼安裝mysql數據庫,這裏不講,只說它的主從複製,步驟以下:sql
一、主從服務器分別做如下操做:
1.一、版本一致
1.二、初始化表,並在後臺啓動mysql
1.三、修改root的密碼
二、修改主服務器master:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必須]啓用二進制日誌
server-id=1 //[必須]服務器惟一ID
三、修改從服務器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必須]啓用二進制日誌
server-id=2 //[必須]服務器惟一ID
四、重啓兩臺服務器的mysql
/etc/init.d/mysql restart
五、在主服務器上創建賬戶並受權slave:
#/usr/local/mysql/bin/mysql -uroot -p
mysql>GRANT REPLICATION SLAVE ON *.* to 'test'@'%' identified by '123456'; //通常不用root賬號,「%」表示全部客戶端均可能連,只要賬號,密碼正確,此處可用具體客戶端IP代替,如192.168.0.146,增強安全。
六、登陸主服務器的mysql,查詢master的狀態
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 308 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注:執行完此步驟後不要再操做主服務器MYSQL,防止主服務器狀態值變化
七、配置從服務器Slave:
mysql>change master to master_host='192.168.0.144',master_user='test',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=308; //注意不要斷開,「308」無單引號。
Mysql>start slave; //啓動從服務器複製功能
八、檢查從服務器複製功能狀態:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.144 //主服務器地址
Master_User: test //受權賬戶名,儘可能避免使用root
Master_Port: 3306 //數據庫端口,部分版本沒有此行
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 600 //#同步讀取二進制日誌的位置,大於等於>=Exec_Master_Log_Pos
Relay_Log_File: ddte-relay-bin.000003
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes //此狀態必須YES
Slave_SQL_Running: Yes //此狀態必須YES
......
注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,不然都是錯誤的狀態(如:其中一個NO均屬錯誤)。
以上操做過程,主從服務器配置完成。
九、主從服務器測試:
主服務器Mysql,創建數據庫,並在這個庫中建表插入一條數據:
mysql> create database hi_db;
Query OK, 1 row affected (0.00 sec)
mysql> use hi_db;
Database changed
mysql> create table hi_tb(id int(3),name char(10));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into hi_tb values(001,'bobu');
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hi_db |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
從服務器Mysql查詢:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hi_db | //I'M here,你們看到了吧
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use hi_db
Database changed
mysql> select * from hi_tb; //能夠看到在主服務器上新增的具體數據
+------+------+
| id | name |
+------+------+
| 1 | bobu |
+------+------+
1 row in set (0.00 sec)數據庫
mysql主從故障切換:安全
首先保證主從數據庫都開啓二進制日誌,方法是在my.cnf中的[mysqld]節中加入log-bin=log-bin-name服務器
而後查看是否開啓ide
MariaDB [(none)]> show variables like 'log_bin';測試
+---------------+-------+spa
| Variable_name | Value |rest
+---------------+-------+日誌
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
爲了在切換時不會漏掉數據庫的更新,必須將主數據庫停掉,設爲只讀:set global read_only=1;
MariaDB [(none)]> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)
show variables; 確認 read_only ON
MariaDB [(none)]> show variables like 'read_only';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | ON |
+---------------+-------+
1 row in set (0.00 sec)
而後在主庫
mysql> flush logs; 刷新一下log-bin,在從庫中查看mysql> show slave status \G 是否徹底更新
主庫:
MariaDB [(none)]> flush logs;
Query OK, 0 rows affected (0.34 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 365 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
從庫:
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.146
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 365
Relay_Log_File: jinbi2-relay-bin.000004
Relay_Log_Pos: 652
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
確認主庫和從庫的file和pos是一致的。
查看主庫的狀態mysql> show master status \G 是否還有數據更新,若是沒有就能夠停掉從庫
MariaDB [(none)]> stop slave;
更改從庫mysql的身份爲master
MariaDB [(none)]> reset master;
Query OK, 0 rows affected (0.16 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 312 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
這時能夠關掉新主庫的只讀屬性
mysql> set global read_only=0;
建立一個新的庫,一會從庫連上後查看是否同步:
MariaDB [(none)]> create database t1;
Query OK, 1 row affected (0.00 sec)
更改主庫mysql的身份爲slave
MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.16 sec)
新從庫改變鏈接的主庫信息:
MariaDB [(none)]> change master to master_host='192.168.0.144', master_user='test', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=312;
Query OK, 0 rows affected (0.16 sec)
查看新從庫狀態:
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.144
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 429
Relay_Log_File: jinbi4-relay-bin.000002
Relay_Log_Pos: 652
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
查看剛纔新主庫建立的數據庫是否已經同步過來:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| bai |
| information_schema |
| mysql |
| performance_schema |
| t1 |
| test |
+--------------------+
主從切換完成!