mysql資料庫主從配置

來源:互聯網
上載者:User

標籤:from   root   lob   figure   base   src   stat   做了   word   

本次實驗 需要3台centos6.5
192.168.204.139 #【主】mysql
192.168.204.138 #【從】mysql

192.168.294.129 #lamp 網站

請關閉3台機器的防火牆,和SELinux

# 關閉防火牆

/etc/init.d/iptables stop

chkconfig iptables off
chkconfig --list |grep iptables

# 關閉SELinux
sed -i /SELINUX/s/enforcing/disabled/g /etc/selinux/config

==================================================

# 配置mysql【主】伺服器192.168.204.139

yum install mysql-server mysql -y

備份mysql設定檔
cp /etc/my.cnf /etc/my.cnf_bak20170915

# 修改/etc/my.cnf如下:
------------------------------------------
[mysqld]

datadir = /data/mydata
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
log-bin=mysql-bin
server-id=1
auto_increment_offset=1
auto_increment_increment=2

[mysqld_safe]
log_error=/var/log/mysqld.err
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db=all

------------------------------------------
建立/data/mysql 資料目錄,
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql

# 重啟mysql即可,
/etc/init.d/mysqld restart

******如果重啟mysql失敗,請查看SELinux是否關閉******

mysql預設沒有密碼,命令列輸入mysql即可進入資料庫

# 在【主】伺服器上設定許可權,執行如下命令:
> grant replication slave on *.* to [email protected]‘%‘ identified by ‘123123‘;
> flush privileges;

> show master status;
此處應該有【配圖】

 

# 配置mysql從【從】伺服器 192.168.204.138

yum install mysql-server mysql -y

mv /etc/my.cnf /etc/my.cnf.bak

vim /etc/my.cnf
------------------------------------------
[mysqld]
datadir = /data/mydata
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
log-bin=mysql-bin
#
server-id=2
auto_increment_offset=2
#
auto_increment_increment=2

[mysqld_safe]
log_error=/var/log/mysqld.err
pid-file=/var/run/mysqld/mysqld.pid
#
master-host = 192.168.204.139
master-user = tongbu
master-pass = 123123
master-port = 3306
master-connect-retry = 60
#
replicate-do-db=all

------------------------------------------

重啟mysql資料庫

/etc/init.d/mysqld restart

******如果重啟mysql失敗,請查看SELinux是否關閉******

然後 【從】伺服器上 指定master IP 和同步的pos 點;
> change master to master_host=‘192.168.204.139‘, master_user=‘tongbu‘, master_password=‘123123‘, master_log_file=‘mysql-bin.000003‘, master_log_pos=415;
> slave start;
> show slave status\G;

 

# 如果 Slave_IO_Running: Yes, Slave_SQL_Running: Yes

==================================================

****** vim 常用命令 ******

# 命令列模式

:%d  #清空檔案內容
:%s/wk/wkui/g #g 全部替換
:%s/My/ my/g #替換
:%s/My/& /g#My 後面加了一個空格
:%s/^/& /g#開頭加個空格
:%s/$/& /g#結尾加個空格

==================================================

 

###另外配置 lamp,192.168.204.129

yum install httpd httpd-devel php php-mysql php-devel mysql-server mysql mysql-devel -y

# 關閉【防火牆】和【SELinux】
chkconfig --del iptables
/etc/init.d/iptables stop

sed -i /SELINUX/s/enforcing/disabled/g /etc/selinux/config

# 預設的網站發布目錄/var/www/html

cd /root/
wget http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_UTF8.zip

# 解壓discuz 程式包:
unzip Discuz_X3.2_SC_UTF8.zip -d /var/www/html/discuz
vim /etc/httpd/conf.d/discuz.conf  # httpd 預設會讀取conf.d 目錄下的檔案

輸入如下內容: 如果你是httpd-2.4 版本,還要輸入《Directory……》

------------------------------------

<VirtualHost *:80>
DocumentRoot /var/www/discuz/upload/
</VirtualHost>

------------------------------------
重啟httpd

/etc/init.d/httpd restart

# 訪問ip 開始安裝discuz 論壇

啊哈,好多紅XXX   檔案許可權不夠啦

chmod -R 777 /var/www/html/discuz/upload/    # 這是圖省事,才全部777的,實際生產中切不可這樣,否則後果自負

 

***** 在192.168.204.139【主】mysql 建立資料庫 *****
> create database discuz charset=utf8;
> grant all on discuz.* to [email protected]‘192.168.204.129‘ identified by ‘123123‘;

安裝discuz 時指定用192.168.204.139 的資料庫discuz,【配圖】

安裝完畢查看【從】mysql:192.168.204.138,是否有discuz資料庫【配圖】

# 看看discuz 的配置資訊
/var/www/html/upload/config/config_global.php
/var/www/html/upload/config/config_ucenter.php

==================================================

關閉【主】mysql,再次訪問網站,網站還是掛了

 

 

雖然做了主從了,但是一旦主mysql 掛了,網站照樣不可以訪問,你就只能哈哈了,

如何才能不受影響呢,那就是keepalived高可用,yeah

# keepalived + mysql 主從
http://www.keepalived.org/software/keepalived-1.2.1.tar.gz

tar xf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
./configure --with-kernel-dir=/usr/src/kernels/2.6.32-696.6.3.el6.x86_64/ && make && make install

# make 時候報錯了,yum install popt popt-devel -y

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

mkdir -p /etc/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/

# 修改【主】伺服器上的 keepalived.conf 設定檔,
vim /etc/keepalived/keepalived.conf

------------------------------------------
!Configuration File For keepalived

global_defs {
notification_email {
[email protected]
}

notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

# VIP1

vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 151
priority 100
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 2222
}

virtual_ipaddress {
192.168.204.150
}

}

virtual_server 192.168.204.150 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP

#
real_server 192.168.204.139 3306 {
#
weight 100
notify_down /data/sh/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306

}
}
}

------------------------------------------

MySQL【從】伺服器配置 keepalived.conf 和master 一樣,
但是需要修改三處:
1、把 Realserver IP修改為 real_server 192.168.204.138;
2、優先順序從 priority 100 給為 90;
3、層級 state MASTER 改為 BACKUP

在 master、slave 資料庫上建立/data/sh/mysql.sh 指令碼,內容為:
/etc/init.d/keepalived stop

# 上面的命令會在mysql服務停止後,停止keepalived服務

然後分別重啟兩台資料庫上的keepalived 服務即可。


最後測試停止master MySQL 服務,是否會自動切換到 Backup 上。

mysql 最佳化還可以進行讀寫分離、mysql+DRBD、拆分表等等最佳化。繼續研究吧!


==================================================

****** 一些問題 ******

# 你的機器可能缺少 ip_vs 模組
# 掛載ip_vs
modprobe ip_vs

lsmod |grep ip_vs

/etc/init.d/keepalived restart

# 遠程複製【主】mysql 的keepalived 設定檔
scp -r keepalived.conf [email protected]:/etc/keepalived/

啟動keepalived,

/etc/init.d/keepalived start

查看機器ip地址,你會發現一個新 IP:192.168.204.150

 

為【主】mysql資料庫,添加新使用者bbs

==================================================

***** keepalived + mysql主從 *****

遠程192.168.204.129,操作如下:
# 重新部署 網站discuz,輸入虛擬ip 192.168.204.150

 

# 部署成功後,新註冊兩個使用者

登入使用者

# 停止【主】mysql,重新整理網站看看是否正常???

 

mysql資料庫主從配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.