程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> Linux下Mysql互為主從

Linux下Mysql互為主從

編輯:MySQL綜合教程

Linux下Mysql互為主從


 

系統環境:CentOS 6.5
master1:192.168.100.204
master2:192.168.100.205
mysql版本:mysql-5.6.19
注:mysql的5.5以下的版本和5.5以上的版本配置方法不一樣

以下兩個節點安裝
1:防火牆打開相應端口
# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
# /etc/rc.d/init.d/iptables save

2:安裝相關依賴包
# yum -y install gcc gcc-c++ ncurses ncurses-devel openssl openssl-devel cmake perl lsof bison

3:刪除自帶的mysql
# rpm -qa|grep mysql
# rpm -e --allmatches --nodeps mysql-libs-5.1.71-1.el6.x86_64

4:創建相關目錄
# mkdir /doiido/soft
# mkdir -p /doiido/mysql/data/

5:創建mysql用戶
# /usr/sbin/groupadd mysql
# /usr/sbin/useradd -s /sbin/nologin -M -g mysql mysql
# chown -R mysql:mysql /doiido/mysql/

6:安裝mysql
# cd /doiido/soft
# tar -zxvf mysql-5.6.19.tar.gz
# cd mysql-5.6.19
# cmake -DCMAKE_INSTALL_PREFIX=/doiido/server/mysql -DMYSQL_DATADIR=/doiido/mysql/data -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DSYSCONFDIR=/etc/ -DWITH_SSL=yes -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=on
# make && make install

7:修改相關目錄權限並創建軟連接
# chmod +w /doiido/server/mysql
# chown -R mysql:mysql /doiido/server/mysql/
# ln -s /doiido/server/mysql/lib/lib* /usr/lib/
# ln -s /doiido/server/mysql/bin/mysql /usr/bin

8:修改配置文件
# cp /doiido/server/mysql/support-files/my-default.cnf /etc/my.cnf
# vi /etc/my.cnf
[mysqld]
character-set-server = utf8
default-storage-engine = MyISAM
basedir = /doiido/server/mysql
datadir = /doiido/mysql/data
log-error = /doiido/mysql/mysql_error.log
pid-file = /doiido/mysql/mysql.pid
max_allowed_packet = 32M
explicit_defaults_for_timestamp = true 

9:安裝mysql數據庫
# /doiido/server/mysql/scripts/mysql_install_db --basedir=/doiido/server/mysql --datadir=/doiido/mysql/data --user=mysql

10:設置mysql開機自動啟動服務
# cp /doiido/server/mysql/support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig --level 345 mysqld on

11:修改mysqld文件並啟動mysql
# vi /etc/init.d/mysqld
#修改mysqld文件中的下面兩項
basedir=/doiido/server/mysql
datadir=/doiido/mysql/data
# service mysqld start

12:創建需同步的數據庫
# mysql -uroot -p
mysql> create database doiido;
mysql> GRANT ALL PRIVILEGES ON doiido.* TO doiido@"%" IDENTIFIED BY 'doiido123';
mysql> exit
--------------------以下在服務器master1上操作
修改配置文件
# vi /etc/my.cnf
[mysqld]
#默認是1,master和slave不能相同即可
server-id=1
log-bin=bin.log
port=3306
binlog-do-db =doiido
#要復制的數據庫
replicate-do-db =doiido

重啟mysql
# service mysqld restart

建立用於同步的賬戶
# mysql -uroot -p
mysql> grant replication slave on *.* to 'dodo'@'192.168.100.205' identified by 'jjjjjj';

查看File對應值,並記錄下來
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| bin.000001       |      120 | doiido       |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
mysql> exit
--------------------以下在服務器master2上操作
2.修改slave上的配置文件my.cnf
# vi /etc/my.cnf
[mysqld]
server-id=2
port=3306
log-bin=bin.log
binlog-do-db =doiido
replicate-do-db =doiido

重啟mysql
# service mysqld restart

在從服務器上建立相應的數據庫
# mysql -uroot -p

將192.168.100.204設置為自己的主服務器
mysql> CHANGE MASTER TO
MASTER_HOST='192.168.100.204',MASTER_PORT=3306,
MASTER_USER='dodo',
MASTER_PASSWORD='jjjjjj',
MASTER_LOG_FILE='bin.000001',
MASTER_LOG_POS=120;

啟動從服務器復制功能
mysql> start slave;

mysql> show slave status\G;
...................
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...................

注:

#Slave_IO_Running:連接到主庫,並讀取主庫的日志到本地,生成本地日志文件
#Slave_SQL_Running:讀取本地日志文件,並執行日志裡的SQL命令。
上面兩個必須都是yes,其中一個NO均屬錯誤

mysql> exit
至此,單向主從配置完畢,如需要互為主從則配置如下
--------------------以下在服務器master2上操作
建立用於同步的賬戶
# mysql -uroot -p
mysql> grant replication slave on *.* to 'dodo'@'192.168.100.204' identified by 'jjjjjj';


查看File對應值,並記錄下來
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| bin.000001       |      649 | doiido       |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> exit
--------------------以下在服務器master1上操作
將192.168.100.205設置為自己的主服務器
mysql> CHANGE MASTER TO
MASTER_HOST='192.168.100.205',
MASTER_PORT=3306,
MASTER_USER='dodo',
MASTER_PASSWORD='jjjjjj',
MASTER_LOG_FILE='bin.000001',
MASTER_LOG_POS=649;

啟動從服務器復制功能

mysql> start slave;

mysql> show slave status\G;
...................
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...................
上面兩個必須都是yes,其中一個NO均屬錯誤

mysql> exit

至此互為主從配置完畢

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved