mysql初始化/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --defaults-file=/etc/my3306.cnf --datadir=/home/mysql3306/mysql3306/ --us ...
mysql初始化
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --defaults-file=/etc/my3306.cnf --datadir=/home/mysql3306/mysql3306/ --user=mysql
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my3306.cnf --user=mysql & /usr/local/mysql/bin/mysql -S /tmp/mysql3306.sock 主庫,從庫打開binlog(修改binlog需要重新啟動資料庫) mysql> show variables like '%binlog%';
+-----------------------------------------+----------------------+
| Variable_name | Value |
+-----------------------------------------+----------------------+
| binlog_cache_size | 4194304 |
| binlog_checksum | CRC32 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_error_action | IGNORE_ERROR |
| binlog_format | ROW |
| binlog_gtid_simple_recovery | OFF |
| binlog_max_flush_queue_time | 0 |
| binlog_order_commits | ON |
| binlog_row_image | FULL |
| binlog_rows_query_log_events | OFF |
| binlog_stmt_cache_size | 32768 |
| binlogging_impossible_mode | IGNORE_ERROR |
| innodb_api_enable_binlog | OFF |
| innodb_locks_unsafe_for_binlog | OFF |
| max_binlog_cache_size | 8388608 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| simplified_binlog_gtid_recovery | OFF |
| sync_binlog | 1 |
+-----------------------------------------+----------------------+
19 rows in set (0.00 sec) mysql> 主庫:
mysql> show databases;清除多餘的資料庫
drop database ---- mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1196 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql> reset master;(有數據的話,就不要做這操作,緊急) mysql> select user,host,password from mysql.user; +------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | oggs | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | oggs | |
+------+-----------+----------+
6 rows in set (0.00 sec) mysql> delete from mysql.user where user='';
mysql> delete from mysql.user where host='::1';
Query OK, 1 row affected (0.01 sec) mysql> delete from mysql.user where host='oggs';
Query OK, 1 row affected (0.01 sec) mysql> select user,host,password from mysql.user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | 127.0.0.1 | |
+------+-----------+----------+
2 rows in set (0.00 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
在主伺服器上建立帳戶並授權slave: mysql> grant REPLICATION CLIENT,REPLICATION SLAVE on *.* to rep@'192.168.100.200' identified by 'rep’;(從端ip) mysql> flush privileges; 查看主伺服器狀態
查看主庫的狀態,查看主庫的binlog的?文件名字,以及?目前binlog的pos,從庫需要讀取binlog並且進?行行同步 mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
執?行行完此步驟後不要再操作主伺服器MYSQL,防?止主伺服器狀態值變化 從庫: mysql> show master status;
mysql> reset master; 從庫設置需要同步的主庫
mysql> change master to master_host='192.168.100.100',master_user='rep',master_password='rep', master_log_file='mysql-bin.000007',master_log_pos=120,MASTER_PORT=3306; 開啟複製,關閉使用stop slave
start slave; 主庫關閉讀鎖
unlock table; 檢查從伺服器複製功能狀態:
mysql> show slave status\G
*************************** 1
-----
Slave_IO_Running: Yes-----------------此狀態必須YES
Slave_SQL_Running: Yes-----------------此狀態必須YES
----- 雙主搭建:
主鍵衝突 步長要不一致,防止binlog衝突
主:1
另一主:2
------------------------
gtid 在mysql中查看server_uuid,mysql在啟動過程中,讀取auto.cnf的server__uuid
在從庫開啟GTID,開始從主庫複製數據
change master to
master_host='192.168.60.22',master_port=3306,master_user='rep',master_password='123456',master_auto_p
osition=1; -----------------------
配置半同步複製需要先配置非同步複製
載入插件
主庫和從庫都需要安裝插件
主庫
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
從庫
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
查看插件是否載入成功
show plugins;
rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL
啟動半同步複製
在安裝完插件後,半同步複製預設是關閉的,這時需設置參數來開啟半同步
主庫
SET GLOBAL rpl_semi_sync_master_enabled = 1;
從庫
SET GLOBAL rpl_semi_sync_slave_enabled = 1;
以上的啟動?式是在命令?操作,也可寫在配置?件中。
在有的?可?架構下,master和slave需同時啟動,以便在切換後能繼續使?半同步複製
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
重啟從庫的IO線程
STOP SLAVE IO_THREAD;
START SLAVE IO_THREAD;
如果沒有重啟,則預設還是非同步複製,重啟後,slave會在master上註冊為半同步複製的slave??。
這時候,主的error.log中會列印如下信息:
2017-04-19 11:09:26 28300 [Note] Semi-sync replication switched ON with slave (server_id: 28703307)
at (mysql-bin.000002, 510)
2017-04-19 11:09:26 28300 [Note] Start semi-sync binlog_dump to slave (server_id: 28703307),
pos(mysql-bin.000002, 510)
2017-04-19 11:09:26 28300 [Note] Stop asynchronous binlog_dump to slave (server_id: 28703307)
查看半同步是否在運?
主庫
mysql> show status like 'Rpl_semi_sync_master_status';
+-----------------------------+-------+
| Variable_name | Value |
+-----------------------------+-------+
| Rpl_semi_sync_master_status | ON |
+-----------------------------+-------+
1 row in set (0.00 sec)
從庫
mysql> show status like 'Rpl_semi_sync_slave_status';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.20 sec)
這兩個變數常?來監控主從是否運?在半同步複製模式下。
mysql> show status like '%semi%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 782 |
| Rpl_semi_sync_master_net_wait_time | 3130 |
| Rpl_semi_sync_master_net_waits | 4 |
| Rpl_semi_sync_master_no_times | 1 |
| Rpl_semi_sync_master_no_tx | 6 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 717 |
| Rpl_semi_sync_master_tx_wait_time | 2871 |
| Rpl_semi_sync_master_tx_waits | 4 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 4 |
+--------------------------------------------+———+
Rpl_semi_sync_master_status:指示主伺服器使?的是非同步複製模式,還是半同步複製模式。
Rpl_semi_sync_master_clients:顯示有多少個從伺服器配置成了半同步複製模式。
Rpl_semi_sync_master_yes_tx:顯示從伺服器確認的成功提交數量。
Rpl_semi_sync_master_no_tx:顯示從伺服器確認的不成功提交數量。