1. 程式人生 > >mysql6.5主從庫同步

mysql6.5主從庫同步

正常 shadow 技術分享 cto 創建用戶 cati 新建 my.cnf word

技術分享圖片
mysql主從同步
1、192.168.56.20(主)和192.168.56.21(從)安裝MYSQL6.5數據庫
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
yum repolist all | grep mysql
yum install mysql-community-server -y
2、啟動
service mysqld start
3、創建用戶具有同步權限,192.168.56.21是從庫的ip
創建數據庫
mysql> create database test1;
mysql> use test1;
mysql> set names utf8;
grant replication slave on . to ‘repl‘@‘192.168.56.21‘ identified by ‘repl‘;
flush privileges;
3、修改主節點配置文件
vim /etc/my.cnf
server-id=101 #每個節點必須不一樣
log-bin=/var/lib/mysql/mysql-bin
4、重新啟動主庫的msyqld服務
service mysqld restart
5、登錄數據庫設置數據庫讀鎖
mysql>flush tables with read lock;
6、查看binlog日誌文件名和偏移量(mysql-bin.000001和120)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
7、備份數據庫到當前目錄
ysqldump test1 > test1.sql
8、復制sql文件到從庫
scp test1.sql 192.168.56.21:/var/lib/mysql/
9、解鎖
mysql> unlock tables;

10、從庫導入sql文件到test1
mysql> create database test1;
mysql> use test1;
mysql> set names utf8;
mysql> source /var/lib/mysql/test1.sql;
11、編輯從庫配置文件
vim /etc/my.cnf
server-id=109
12、重啟從庫服務
service mysqld restart
13、配置從庫
關閉復制功能
mysql> stop slave;
mysql>change master to master_host=‘192.168.56.20‘,master_user=‘repl‘,master_password=‘repl‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=120;
啟動復制
Mysql>start slave;
檢查復制功能
mysql>show slave status\G
兩個進程需要yes才正常
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

14、在主庫新建數據庫,然後去從庫看是否同步,同步表示正常。
create database hi_db;

mysql6.5主從庫同步