MySQL多主一從同步

來源:互聯網
上載者:User

MySQL多主一從同步

實驗準備:主機A和主機B作為主,其IP地址分別為192.168.131.129和192.168.131.130,主機C作為從伺服器,在從伺服器上面配置MySQL多執行個體,其IP地址為192.168.131.136,三台伺服器均關閉防火牆和SELINUX,MySQL版本為5.6.26,為通用二進位包

1.主機A和主機B主伺服器MySQL通用二進位包安裝和初始化

# tar xf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz

# mv mysql-5.6.26-linux-glibc2.5-x86_64 /usr/local/mysql

# useradd -M -s /sbin/nologin mysql

# chown -R root:root /usr/local/mysql/*

# chown -R mysql:mysql /usr/local/mysql/data/

# yum -y install libaio

# cd /usr/local/mysql/scripts/

# ./mysql_install_db --datadir=/usr/local/mysql/data/ --user=mysql --basedir=/usr/local/mysql/

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

# echo 'export PATH=$PATH:/usr/local/mysql/bin' >>/etc/profile

# source /etc/profile

# vim /etc/my.cnf

[mysqld]

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock

user=mysql

symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

# service mysqld start

#chkconfig mysqld on

# ss -tunlp | grep 3306

# mysqladmin -uroot password '123456' #設定資料庫密碼

2、主機A和主機B配置

①主機A

# vim /etc/my.cnf

server-id=129

port=3306

log-bin=mysql-bin

log-bin-index=mysql-bin.index

max-binlog-size=100M

# service mysqld restart

# mysql -uroot –p

mysql> GRANT REPLICATION SLAVE ON *.* TO 'wdd'@'192.168.131.136' IDENTIFIED BY '123456';

mysql> SHOW MASTER STATUS;

②主機B

# vim /etc/my.cnf

server-id=130

port=3306

log-bin=mysql-bin

log-bin-index= mysql-bin.index

max-binlog-size=100M

# service mysqld restart

# mysql -u root –p

mysql> GRANT REPLICATION SLAVE ON *.* TO 'wdd'@'192.168.131.136' IDENTIFIED BY '123456';

mysql> SHOW MASTER STATUS;

3、主機c從伺服器安裝MySQL多執行個體

# yum -y install libaio

# tar xf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz

# mv mysql-5.6.26-linux-glibc2.5-x86_64 /usr/local/mysql

# useradd -M -s /sbin/nologin mysql

# mkdir /data/{3306,3307} –pv

# chown -R mysql:mysql /data/

#vim /etc/my.cnf

[mysqld_multi]

mysqld=/usr/local/mysql/bin/mysqld_safe

mysqladmin=/usr/local/mysql/bin/mysqladmin

#user=mysql

#password=123456

log=/data/multi.log

[mysqld129]

port=3306

pid-file=/data/3306/mysql.pid

datadir=/data/3306/data

socket=/data/3306/mysql.sock

server-id=136

user=mysql

[mysqld130]

port=3307

pid-file=/data/3307/mysql.pid

datadir=/data/3307/data

socket=/data/3307/mysql.sock

server-id=136

user=mysql

1.# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/3306/data --basedir=/usr/local/mysql/

2.# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/3307/data --basedir=/usr/local/mysql/

3.# mysqld_multi --defaults-file=/etc/my.cnf start 129

# mysqld_multi --defaults-file=/etc/my.cnf start 130

4.# mysqld_multi --defaults-extra-file=/etc/my.cnf report

5.# ss -tunlp | grep 330

註:為了安全應該給MySQL執行個體設定密碼,命令如下

# mysqladmin -uroot -S /data/3306/mysql.sock password '123456'

# mysqladmin -uroot -S /data/3307/mysql.sock password '123456'

4.主機c多執行個體實驗操作

①# mysql -uroot -S /data/3306/mysql.sock

mysql> CHANGE MASTER TO

MASTER_HOST='192.168.131.129',

MASTER_USER='wdd',

MASTER_PASSWORD='123456',

MASTER_LOG_FILE='mysql-bin.000004',

MASTER_LOG_POS=120;

mysql> START SLAVE;

mysql>SHOW SLAVE STATUS;

②# mysql -u root -S /data/3307/mysql.sock

mysql> CHANGE MASTER TO

MASTER_HOST='192.168.131.130',

MASTER_USER='wdd',

MASTER_PASSWORD='123456',

MASTER_LOG_FILE='mysql-bin.000003',

MASTER_LOG_POS=331;

mysql> START SLAVE;

mysql>SHOW SLAVE STATUS;

5、測試

①登入主機A

mysql> create database wdd;

mysql> SHOW DATABASES;

②登入主機B

mysql> create database info;

mysql> show databases;

③查看MySQL執行個體,執行個體129和130

執行個體129

# mysql -uroot -S /data/3306/mysql.sock

mysql> START SLAVE;

執行個體130

# mysql -u root -S /data/3307/mysql.sock

mysql> show databases;

註:MySQL多執行個體登入時,其實不需要指定連接埠,也就是說可以省略-p選項,即使使用-p選項,連接埠也可以隨意指定,也就是說可以隨便寫一個連接埠號碼,只要通訊端不變,那麼登入的還是同一個MySQL執行個體。

MySQL 多主多活 Galera 叢集部署使用 

MySQL 5.5主從關於‘複製過濾’的深入探究 

MySQL資料庫主從不同步的解決方案 

MySQL主從同步配置記錄 

MySQL 5.5 主從雙向同步

MySQL 5.5主從同步排錯

MySQL主從複製非同步半同步執行個體

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.