標籤:mysql 主從複製
一、環境
mysql-m:192.168.3.61
mysql-s:192.168.3.62
二、配置
查看mysql-m的server-id和log-bin
[[email protected] ~]# egrep "server-id|log-bin" /etc/my.cnf server-id= 1log-bin=mysql-bin[[email protected] ~]# ll /data/mysql/data/total 28700-rw-rw---- 1 mysql mysql 18874368 May 18 11:00 ibdata1-rw-rw---- 1 mysql mysql 5242880 May 18 11:00 ib_logfile0-rw-rw---- 1 mysql mysql 5242880 May 18 10:39 ib_logfile1drwx------ 2 mysql root 4096 May 18 10:39 mysql-rw-rw---- 1 mysql mysql 107 May 18 11:00 mysql-bin.000001-rw-rw---- 1 mysql mysql 19 May 18 11:00 mysql-bin.index-rw-r----- 1 mysql root 3369 May 18 11:00 mysql-m.err-rw-rw---- 1 mysql mysql 6 May 18 11:00 mysql-m.pidsrwxrwxrwx 1 mysql mysql 0 May 18 11:00 mysql.sockdrwx------ 2 mysql mysql 4096 May 18 10:39 performance_schemadrwx------ 2 mysql root 4096 May 18 10:39 test
查看mysql-s的server-id,確保和mysql-m的不一致
[[email protected] ~]# grep server-id /etc/my.cnf server-id= 2
三、建立主從複製用的帳號,並授權
mysql> grant replication slave on *.* to ‘rep‘@‘192.168.3.62‘ identified by ‘123456‘;Query OK, 0 rows affected (0.01 sec)mysql> flush privileges;Query OK, 0 rows affected (0.01 sec)
四、配置主從
在master查看二進位日誌和position資訊
[[email protected] ~]# mysql -u root -plyao36843 -h 127.0.0.1 -e ‘show master status;‘+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000005 | 107 | | |+------------------+----------+--------------+------------------+
在mysql-s上配置同步資訊
mysql> system hostnamemysql-smysql> change master to master_host=‘192.168.3.61‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000005‘,master_log_pos=107;Query OK, 0 rows affected (0.03 sec)
啟動slave複製
mysql> start slave;Query OK, 0 rows affected (0.00 sec)mysql> show slave status\G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.3.61 #master的IP地址 Master_User: rep #同步用的帳號 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 #同步的二進位檔案 Read_Master_Log_Pos: 107 #同步的position點 Relay_Log_File: mysql-s-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes #主從同步的IO線程 Slave_SQL_Running: Yes #主從同步的SQL線程 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: 107 Relay_Log_Space: 411 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: 0Master_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: 11 row in set (0.00 sec)
測試,在主庫建立一個資料庫linux
[[email protected] ~]# mysql -u root -plyao36843 -h 127.0.0.1 -e ‘create database linux;‘[[email protected] ~]# mysql -u root -plyao36843 -h 127.0.0.1 -e ‘show databases;‘+--------------------+| Database |+--------------------+| information_schema || git || linux || mysql || performance_schema || test |+--------------------+
從庫驗證結果
mysql> system hostnamemysql-smysql> show databases;+--------------------+| Database |+--------------------+| information_schema || linux || mysql || performance_schema || test |+--------------------+5 rows in set (0.00 sec)#從結果能看到在master上建立的linux庫,已同步到slave上
在生產中我們應該講master的資料匯入到從庫後,再啟動slave同步
本文出自 “ly36843營運” 部落格,請務必保留此出處http://ly36843.blog.51cto.com/3120113/1652236
mysql主從複製