標籤:mysql--簡單篇
Mysql基礎拓撲圖:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/72/B5/wKioL1Xr9GGS_ppoAAE_lZxWv1w826.jpg" title="mysql.png" alt="wKioL1Xr9GGS_ppoAAE_lZxWv1w826.jpg" />
Mysql環境準備:
一台mysql主伺服器(安裝mysql)
兩台mysql從伺服器(安裝mysql)
一台mysql代理(安裝amoeba和java)
一台mysql用戶端(mysql用戶端)
部署前先關閉所有的iptables,selinux
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/72/B6/wKioL1Xr-DXwUT65AAEThvZK0QU107.jpg" title="2015-09-06_161456.png" alt="wKioL1Xr-DXwUT65AAEThvZK0QU107.jpg" />
Mysql的主從複製讀寫分離所需安裝包:
cmake-2.8.6.tar.gz
mysql-5.5.22.tar.gz
amoeba-mysql-binary-2.2.0.tar.gz
jdk-7u65-linux-x64.tar.gz
jdk-6u14-linux-x64.bin
ncurses-devel
部署一個時間伺服器來進行時間同步:
yum -y install ntp
編輯設定檔添加如下行:
server 127.127.1.0
fudge 127.127.1.0 stratum 8
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/72/B9/wKiom1Xr9PmidTetAAIjMt-xhlo436.jpg" title="2015-09-06_161006.png" alt="wKiom1Xr9PmidTetAAIjMt-xhlo436.jpg" />
重啟服務:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/72/B6/wKioL1Xr93_hKC4pAABthFx3G88403.jpg" title="2015-09-06_161145.png" alt="wKioL1Xr93_hKC4pAABthFx3G88403.jpg" />
用戶端安裝軟體同步:
yum -y install ntp-date
/usr/sbin/ntpdate 99.99.99.22
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/B6/wKioL1Xr-NOy9bx5AACmi6jBW-I891.jpg" title="2015-09-06_161731.png" alt="wKioL1Xr-NOy9bx5AACmi6jBW-I891.jpg" />
安裝mysql獨特的編譯安裝軟體cmake:
tar zxf /root/cmake-2.8.6.tar.gz -C /root/
cd /root/cmake-2.8.6
./configure && gmake && gmake install
安裝mysql依賴包:
yum -y install ncurses-devel
編譯安裝mysql
tar zxf /root/mysql-5.5.22.tar.gz -C /usr/src
cd /usr/src/mysql-5.5.22
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all
make && make install
授權資料庫使用者:
useradd -M -s /sbin/nologin mysql -g mysql
chown -R mysql:mysql /usr/local/mysql
初始化資料庫:
/usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data/
最佳化資料庫
cp -rf /usr/src/$MY_Q/support-files/my-medium.cnf /etc/my.cnf
cp -rf /usr/src/$MY_Q/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
啟動資料庫並設定密碼:
echo "PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile
/etc/init.d/mysqld start
mysqladmin -uroot password ‘123123‘
配置Mysql主伺服器
vim /etc/my.cnf
server-id = 11 #改
log-bin=master-bin #改
log-slave-updates=true #添加
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/72/B6/wKioL1Xr_ETzMvlAAAE1xD8Dnss071.jpg" title="2015-09-06_163214.png" alt="wKioL1Xr_ETzMvlAAAE1xD8Dnss071.jpg" />
重啟服務:
/etc/init.d/mysqld restart
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/BA/wKiom1Xr-mvCwWNLAACHm3Yqql0554.jpg" title="2015-09-06_163332.png" alt="wKiom1Xr-mvCwWNLAACHm3Yqql0554.jpg" />
登陸Mysql給伺服器授權
[[email protected] ~]# mysql -uroot -p123123Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.5.22-log Source distributionCopyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql>grant replication slave on *.* to ‘myslave‘@‘99.99.99.%‘ identified by ‘123123‘;Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> show master status ;+-------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+-------------------+----------+--------------+------------------+| master-bin.000002 | 336 | | |+-------------------+----------+--------------+------------------+1 row in set (0.00 sec)
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/72/B7/wKioL1Xr_wXyre1PAAPdCtfVTfA908.jpg" title="2015-09-06_164401.png" alt="wKioL1Xr_wXyre1PAAPdCtfVTfA908.jpg" />
從伺服器的資料庫與主伺服器一致,不需要配置任何命令。
配置第一個從伺服器添加如下行:
vim /etc/my.cnf
server-id = 22 #改
relay-log=relay-log-bin #添加
relay-log-index=slave-relay-bin.index #添加
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/BA/wKiom1Xr_eqjQawjAAFCV-bBNC4636.jpg" title="2015-09-06_164650.png" alt="wKiom1Xr_eqjQawjAAFCV-bBNC4636.jpg" />
重啟服務:
service mysqld restart
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/B7/wKioL1XsAJTDdzYYAAB-sYSfvQM129.jpg" title="2015-09-06_165021.png" alt="wKioL1XsAJTDdzYYAAB-sYSfvQM129.jpg" />
設定資料庫:
[[email protected] ~]# mysql -uroot -p123123Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.5.22-log Source distributionCopyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql> change master to master_host=‘99.99.99.22‘,master_user=‘myslave‘,master_password=‘123123‘,master_log_file=‘master-bin.000001‘,master_log_pos=336;start slave;show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/72/B7/wKioL1XsAabh6P3CAAPkuI6IoRA303.jpg" title="2015-09-06_165507.png" alt="wKioL1XsAabh6P3CAAPkuI6IoRA303.jpg" />
配置第二個從伺服器添加如下行:
vim /etc/my.cnf
server-id = 33 #改
relay-log=relay-log-bin #添加
relay-log-index=slave-relay-bin.index #添加
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/BC/wKiom1XsEinSpu5hAAHzPIrM7KU348.jpg" title="2015-09-06_181449.png" alt="wKiom1XsEinSpu5hAAHzPIrM7KU348.jpg" />
重啟服務:
/etc/init.d/mysqld restart
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/72/B8/wKioL1XsFPODqVWqAACA6eR8TfM792.jpg" title="2015-09-06_181733.png" alt="wKioL1XsFPODqVWqAACA6eR8TfM792.jpg" />
配置mysql資料庫:
[[email protected] ~]# mysql -uroot -p123123Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.5.22-log Source distributionCopyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql> change master to master_host=‘99.99.99.22‘,master_user=‘myslave‘,master_password=‘123123‘,master_log_file=‘master-bin.000001‘,master_log_pos=336;start slave;show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/B8/wKioL1XsFB-yoLqwAAM3DggNPGI909.jpg" title="2015-09-06_181358.png" alt="wKioL1XsFB-yoLqwAAM3DggNPGI909.jpg" />
驗證主從複製的效果
登陸主伺服器,建立資料庫:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/72/BC/wKiom1XsEyfTDYaeAAELoTFk-y0710.jpg" title="2015-09-06_181904.png" alt="wKiom1XsEyfTDYaeAAELoTFk-y0710.jpg" />
從伺服器查看:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/72/BC/wKiom1XsE1SCslGdAADHUqiEFTI651.jpg" title="2015-09-06_181949.png" alt="wKiom1XsE1SCslGdAADHUqiEFTI651.jpg" />
實現Mysql的讀寫分離(待續):
本文出自 “某某” 部落格,請務必保留此出處http://moumou.blog.51cto.com/9995443/1691998
Mysql的主從複製讀寫分離--簡單篇