/etc/mysql
能夠找到其結構以下/etc/mysql ├── conf.d │ ├── mysql.cnf │ └── mysqldump.cnf ├── debian.cnf #自動生成的系統腳本,不要動它 ├── debian-start #5.7前該腳本用於檢查奔潰的表和無密碼的帳戶,5.7之後只是個空腳本 ├── my.cnf -> /etc/alternatives/my.cnf #全局配置文件 ├── my.cnf.fallback #用於回退全局配置文件 ├── mysql.cnf └── mysql.conf.d ├── mysqld.cnf #配置的模板,能夠拷貝至/etc/mysql/my.cnf和~/.my.cnf └── mysqld_safe_syslog.cnf #系統日誌配置若是須要定製則註釋該文件配置再my.cnf中配置
#使用下面命令查看 /usr/sbin/mysqld --verbose --help | grep -A -1 2 'Default options'
/etc/my.cnf
、/etc/mysql/my.cnf
、~/.my.cnf
Default options are read from the following files in the given order:/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
my.cnf
文件中存在以下兩句話,表示配置包含!includedir
文件夾中的配置... !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/
/etc/mysql/my.cnf
[mysqld] server-id=1 #服務ID須要確保惟一性 log-bin=mysql-bin #指定日誌目錄 ubuntu 默認 /var/lib/mysql/mysql-bin目錄 #使事務的複製中盡最大可能的保證持久性和一致性 innodb_flush_log_at_trx_commit=1 #事務日誌直接寫入文件,性能下降,但高可靠 sync_binlog=1
/etc/init.d/mysql restart
CREATE USER 'repl'@'192.168.1.102' IDENTIFIED BY 'slavepass'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.102';
--master加鎖 FLUSH TABLES WITH READ LOCK; --查看master狀態信息 SHOW MASTER STATUS;
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |--|--|--|--|--| |mysql-bin.000001|619|-|-|-|mysql
/etc/mysql/my.cnf
[mysqld] server-id=2
/etc/init.d/mysql restart
UNLOCK TABLES;
CHANGE MASTER TO MASTER_HOST='192.168.1.101',MASTER_USER='repl',MASTER_PASSWORD='slavepass',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=619;
mysql> START SLAVE;
show slave status\G
其中Slave_IO_Running
和Slave_SQL_Running
均爲YES
表示slave已經啓動同步sql
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.101 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 619 Relay_Log_File: iZuf60xdp6y5avwotnjkm9Z-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000003 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: 154 Relay_Log_Space: 545 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: Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates 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 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
create database test; use test; create table a(a int);
插入測試數據數據庫
insert into a(a)alues(1); insert into a(a)alues(2); insert into a(a)alues(3);
show databases; use test; select * from a;
獲得結果ubuntu
|a| |--| |1| |2| |3|bash
本文是最簡單的主從配置,一些高級特性須要參考MySQL手冊進行定製,請讀者自行查閱。性能