標籤:MYSQL雙活;MYSQL
配置兩台主機MySQL雙活
主機:
A:192.168.199.2
B:192.168.199.4
一、安裝服務,兩台主機執行同樣的操作:
[email protected][192.168.199.4][/tmp]# tar xvf MySQL-5.6.40-1.el6.x86_64.rpm-bundle.tar
[email protected][192.168.199.4][/tmp]# rpm -ivh MySQL-*.rpm --force --nodeps
[email protected][192.168.199.4][/tmp]# cp /usr/share/mysql/my-default.cnf /etc/my.cnf
[email protected][192.168.199.4][/tmp]# service mysql start
Starting MySQL.Logging to ‘/var/lib/mysql/e2emydb02.err‘.
[ OK ]
mysql 5.6 以後 安裝時的預設密碼存放在目錄:/root/.mysql_secret
1、查看密碼
[email protected][192.168.199.4][/root]# more /root/.mysql_secret
The random password set for the root user at Wed May 23 11:30:41 2018 (local time): 5eU4P7fz9Qj_UojJ
2、使用該密碼登陸測試:
[email protected][192.168.199.4][/root]# mysql -u root -p
3、修改密碼:
[email protected][192.168.199.4][/root]# mysqladmin -u root -p password
Enter password:
New password:
Confirm new password:
4、修改資料儲存目錄
預設安裝在/var/lib/mysql/ 目錄
修改到自己定義的目錄 /etedata
執行以下操作:
[email protected][192.168.199.4][/root]# service mysql stop
[email protected][192.168.199.4][/root]# cp -R /var/lib/mysql /etedata
把原目錄備份
[email protected][192.168.199.4][/root]# mv /var/lib/mysql /var/lib/mysql.bak
對/etedata目錄賦權
[email protected][192.168.199.4][/root]# chmod 777 -R /etedata ##否則啟動會報錯: Starting MySQL...The server quit without updating PID file [FAILED]a/mysql/e2emydb01.pid).
錯誤記錄檔:/etedata/mysql/e2emydb02.err
修改/etc/my.cnf
[email protected][192.168.199.4][/root]# vi /etc/my.cnf
[client]
port=3306
socket=/etedata/mysql/mysql.sock
[mysqld]
datadir =/etedata/mysql
socket=/etedata/mysql/mysql.sock
sql_mode=NO_ENGINE_SUBSTITUTION
儲存,重啟服務
[email protected][192.168.199.4][/root]# service mysql stop
Shutting down MySQL.... [ OK ]
[email protected][192.168.199.4][/root]# service mysql start
Starting MySQL. [ OK ]
[email protected][192.168.199.4][/tmp]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.40 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
5、設定服務端、用戶端、結果集為UTF-8
mysql> SET character_set_client = utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> SET character_set_results = utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> SET character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘%char%‘;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql> create database etedb;
Query OK, 1 row affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| etedb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> quit
二 配置從 A-->B
至此,基礎工作做好了。
下面配置A-->B
1、在A主機操作:
[email protected][192.168.199.2][/root]#more /etc/my.cnf
[client]
port=3306
socket=/etedata/mysql/mysql.sock
[mysqld]
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
read-only=0
binlog-do-db=etedb
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=test
auto-increment-increment=2
auto-increment-offset=1
datadir =/etedata/mysql
socket=/etedata/mysql/mysql.sock
完成後需要重啟伺服器
[email protected][192.168.199.2][/root]#service mysql stop
Shutting down MySQL.... [ OK ]
[email protected][192.168.199.2][/root]#service mysql start
Starting MySQL. [ OK ]
[email protected][192.168.199.2][/root]#mysql -u root -p
Enter password:
2、添加使用者
為使用者授權,只有192.168.199.4可以訪問
GRANT USAGE ON . TO ‘repl_user‘@‘192.168.199.2‘ IDENTIFIED BY ‘****‘ WITH GRANT OPTION;
grant replication slave on . to ‘repl_user‘@‘192.168.199.4‘ identified by ‘****‘;
執行情況如下:
[email protected][192.168.199.2][/root]#mysql -u root -p
Enter password:
mysql> GRANT USAGE ON . TO ‘repl_user‘@‘192.168.199.2‘ IDENTIFIED BY ‘****‘ WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on . to ‘repl_user‘@‘192.168.199.4‘ identified by ‘****‘;
Query OK, 0 rows affected (0.00 sec)
在B主機測試:
[email protected][192.168.199.4][/root]#mysql -h192.168.199.2 -urepl_user -p****
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 7
Server version: 5.6.40-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
3、擷取伺服器初態
在A伺服器執行(MYSQL命令列下)鎖定表:
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
在A主機執行匯出初態:
[email protected][192.168.199.2][/root]#mysqldump --master-data -uroot -p etedb >etedb.sql
Enter password:
並把檔案傳到B伺服器上:
[email protected][192.168.199.2][/root]#scp etedb.sql [email protected]:/home/ete
[email protected]‘s password:
然後查看初態值並解鎖
mysql> show master status\G
1. row
File: mysql-bin.000001
Position: 120
Binlog_Do_DB: etedb
Binlog_Ignore_DB: information_schema,mysql,performance_schema,test
Executed_Gtid_Set:
1 row in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
註:標紅的兩個值會在B伺服器用到
4、在B伺服器執行:
[email protected][192.168.199.4][/root]#vi /etc/my.cnf
[client]
port=3306
socket=/etedata/mysql/mysql.sock
[mysqld]
datadir =/etedata/mysql
log-bin=mysql-bin
binlog_format=mixed
server-id= 2
replicate-do-db=etedb
replicate-ignore-db=information_schema
replicate-ignore-db=mysql
replicate-ignore-db=performance_schema
replicate-ignore-db=test
relay_log=mysqld-relay-bin
log-slave-update=yes
socket=/etedata/mysql/mysql.sock
sql_mode=NO_ENGINE_SUBSTITUTION
[email protected][192.168.199.4][/root]#service mysql stop
Shutting down MySQL.... [ OK ]
[email protected][192.168.199.4][/root]#service mysql start
Starting MySQL. [ OK ]
[email protected][192.168.199.4][/root]#mysql -u root -p
mysql> CHANGE MASTER TO
-> MASTER_HOST=‘192.168.199.2‘,
-> MASTER_USER=‘repl_user‘,
-> MASTER_PASSWORD=‘****‘,
-> MASTER_LOG_FILE=‘mysql-bin.000001‘,
-> MASTER_LOG_POS=120;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
註:上面幾個值是從主伺服器得到的
mysql> show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.199.2
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 120
Relay_Log_File: mysqld-relay-bin.000003
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes ## 這兩個值是Yes才表示OK
Slave_SQL_Running: Yes ##
Replicate_Do_DB: etedb
Replicate_Ignore_DB: information_schema,mysql,performance_schema,test
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: 120
Relay_Log_Space: 457
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: dd4d3f58-5fcb-11e8-8e32-6c92bf5e6ba0
Master_Info_File: /etedata/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)
把剛才從A伺服器同步過來的初態檔案匯入:
[email protected][192.168.199.4][/home/ete]#cp etedb.sql /rootbr/>[email protected][192.168.199.4][/home/ete]#cd
[email protected][192.168.199.4][/root]#mysql -u root -p etedb<etedb.sql
Enter password:
[email protected][192.168.199.4][/root]#service mysql stop
Shutting down MySQL.... [ OK ]
[email protected][192.168.199.4][/root]#service mysql start
Starting MySQL. [ OK ]
然後可以測試了
在A主機執行建表和和插值:
mysql> use etedb;
Database changed
mysql> create table a(id int);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into a values(1);
Query OK, 1 row affected (0.00 sec)
在B主機驗證:
mysql> use etedb;
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_etedb |
+-----------------+
| a |
+-----------------+
1 row in set (0.00 sec)
mysql> select * from a;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
三、配置B-->A
1、在B主機執行:
mysql> GRANT USAGE ON . TO ‘repl_user‘@‘192.168.199.4‘ IDENTIFIED BY ‘****‘ WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on . to ‘repl_user‘@‘192.168.199.2‘ identified by ‘****‘;
Query OK, 0 rows affected (0.00 sec)
2、修改B的設定檔:
[email protected][192.168.199.4][/root]#vi /etc/my.cnf
[client]
port=3306
socket=/etedata/mysql/mysql.sock
[mysqld]
datadir =/etedata/mysql
log-bin=mysql-bin
binlog_format=mixed
server-id= 2
read-only=0
binlog-do-db=etedb
binlog-ignore-db=information_schema
binlog-ignore-db=test
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
auto-increment-increment=2
auto-increment-offset=2
replicate-do-db=etedb
replicate-ignore-db=information_schema
replicate-ignore-db=mysql
replicate-ignore-db=performance_schema
replicate-ignore-db=test
relay_log=mysqld-relay-bin
log-slave-update=yes
socket=/etedata/mysql/mysql.sock
sql_mode=NO_ENGINE_SUBSTITUTION
3、查看初態參數
mysql> show master status\G
1. row
File: mysql-bin.000005
Position: 120
Binlog_Do_DB: etedb
Binlog_Ignore_DB: information_schema,test,mysql,performance_schema
Executed_Gtid_Set:
1 row in set (0.00 sec)
4、在A伺服器執行:
[email protected][192.168.199.2][/root]#vi /etc/my.cnf
[client]
port=3306
socket=/etedata/mysql/mysql.sock
[mysqld]
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
read-only=0
binlog-do-db=etedb
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=test
auto-increment-increment=2
auto-increment-offset=1
replicate-do-db=etedb
replicate-ignore-db=information_schema
replicate-ignore-db=test
replicate-ignore-db=mysql
replicate-ignore-db=performance_schema
relay_log=mysqld-relay-bin
log-slave-update=yes
datadir =/etedata/mysql
socket=/etedata/mysql/mysql.sock
在資料庫執行:
[email protected][192.168.199.2][/root]#mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.40-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> CHANGE MASTER TO
-> MASTER_HOST=‘192.168.199.4‘,
-> MASTER_USER=‘repl_user‘,
-> MASTER_PASSWORD=‘****‘,
-> MASTER_LOG_FILE=‘mysql-bin.000005‘,
-> MASTER_LOG_POS=120;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
重啟伺服器:
[email protected][192.168.199.2][/root]#service mysql stop
Shutting down MySQL.... [ OK ]
[email protected][192.168.199.2][/root]#service mysql start
Starting MySQL. [ OK ]
查看資料庫的狀態:
mysql> show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.199.4
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 1324
Relay_Log_File: mysqld-relay-bin.000003
Relay_Log_Pos: 1487
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: etedb
Replicate_Ignore_DB: information_schema,test,mysql,performance_schema
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: 1324
Relay_Log_Space: 1661
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: 2
Master_UUID: 2c61b0aa-5fe9-11e8-8ef1-6c92bf5e0138
Master_Info_File: /etedata/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)
然後做雙向測試:
在B伺服器執行:
mysql> create table b(id int);
Query OK, 0 rows affected (0.00 sec)
mysql> insert into b values(2);
Query OK, 1 row affected (0.00 sec)
在A查看:
[email protected][192.168.199.2][/root]#mysql -u root -p
Enter password:
mysql> show tables;
+-----------------+
| Tables_in_etedb |
+-----------------+
| a |
| b |
+-----------------+
2 rows in set (0.00 sec)
mysql> select * from b;
+------+
| id |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
四:最後,在兩台伺服器上建立可遠端存取的使用者
mysql> GRANT USAGE ON . TO ‘ete‘@‘%‘ IDENTIFIED BY ‘**‘ WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON . TO ‘ete‘@‘%‘ IDENTIFIED BY ‘**‘ WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL雙機