mysql-5.7的主從配置
阿新 • • 發佈:2018-03-25
day pat 就是 tid eve nta var oot 初始
mysql的主從配置
下載最新mysql 的yum源
1、wget https://dev.mysql.com/get/mysql57-community-release-el6-11.noarch.rpm
安裝最新mysql
rpm -ivh mysql57-community-release-el6-11.noarch.rpm yum -y install mysql-server
啟動mysql數據庫
service mysqld start
提示:由於5.7初始化會自動生成密碼 :
cat /var/log/mysqld.log 2018-03-24T13:19:50.925206Z 0[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp
server option (see documentation for more details). 2018-03-24T13:19:54.106661Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-03-24T13:19:54.588874Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.2018-03-24T13:19:54.805786Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been
started. Generating a new UUID: 0abbeb4b-2f66-11e8-81f3-000c292c7cea. 2018-03-24T13:19:54.810834Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened. 2018-03-24T13:19:54.841042Z 1 [Note] A temporary password is generated for root@localhost: :kufY//k0zk& (這是隨機生成的密碼)
登錄mysql數據庫並修改密碼:
mysql -uroot -p ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘Xu19920615!@‘;
1.配置master的配置文件my.cnf
#打開日誌(主機需要打開) log-bin=mysql-bin ##服務器id(這個是唯一的) server-id=1 #給從機同步的庫(可以寫多個庫) binlog-do-db=mydb binlog-do-db=mydb2 binlog-do-db=test #自動清理5天前的log文件 expire_logs_days=5
2.修改從服務器的從數據庫slave /etc/my.cnf配置
#服務器id server-id=2 ##要從主機同步的庫 replicate-do-db=mydb replicate-do-db=mydb2 replicate-do-db=test
3.修改之後,重啟MySQL主數據庫和MySQL從數據庫的服務
service mysqld restart
4.配置主服務器的主數據庫
主數據庫授權同步賬戶
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘root‘@‘192.168.10.115‘ IDENTIFIED BY ‘Xu19920615!@‘; Query OK, 0 rows affected, 1 warning (0.01 sec)
刷新權限
FLUSH PRIVILEGES;
查看主服務狀態
mysql> show master status ; +------------------+----------+-----------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+-----------------+------------------+-------------------+ | mysql-bin.000001 | 602 | mydb,mydb2,test | | | +------------------+----------+-----------------+------------------+-------------------+ 1 row in set (0.00 sec)
5.配置從服務器的從數據庫
mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.10.116‘, -> MASTER_USER=‘root‘, -> MASTER_PASSWORD=‘Xu19920615!@‘, -> MASTER_LOG_FILE=‘mysql-bin.000001‘,\ (這個都是根據主服務器查詢出的結果) -> MASTER_LOG_POS=602;(就是主服務器 show master status;) Query OK, 0 rows affected, 2 warnings (0.13 sec)
開啟SLAVE同步
start slave;
查看下slave狀態
show slave status \G;
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.116 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 602 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes (這個位置顯示yes) Slave_SQL_Running: Yes (這個位置也顯示yes表示主從復制配置成功) Replicate_Do_DB: mydb,mydb2,test 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: 602 Relay_Log_Space: 531 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: 0abbeb4b-2f66-11e8-81f3-000c292c7cea 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: 1 row in set (0.00 sec)
當Slave_IO_Running和Slave_SQL_Running都為Yes,才說明主從復制成功
6.停止SLAVE同步
stop slave;
7.撤銷已經賦予給MySQL同步賬戶的權限
revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
GRANT REPLICATION SLAVE ON *.* TO ‘root‘@‘192.168.10.116‘ IDENTIFIED BY ‘XXXXXX‘;
REVOKE REPLICATION SLAVE ON *.* FROM ‘root‘@‘192.168.10.116‘;
8.授權賬號可以遠程登錄
GRANT ALL ON *.* TO 用戶名@‘%‘ IDENTIFIED BY ‘密碼‘ WITH GRANT OPTION;
mysql-5.7的主從配置