您好,登錄后才能下訂單哦!
一 前戲
#安裝
yum install -y mysql-server mysql mysql-devel
#不同實(shí)例目錄
mkdir /data/{3306,3307} -p
# 授權(quán)
chown -R mysql.mysql /data
二 配置文件與啟動(dòng)
#刪除或者重命名原/etc/my.cnf配置文件
mv /etc/my.cnf /etc/my.cnf.bak
#創(chuàng)建配置文件 vi /data/3306/my.cnf (以下為配置文件內(nèi)容)
# 配置文件從這里開始
[client]
port = 3306
socket = /data/3306/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /usr #編譯和二進(jìn)制安裝按實(shí)際目錄填寫路徑(/usr為yum安裝的)
datadir = /data/3306/data
open_files_limit = 1024
back_log = 600
max_connections = 800
max_connect_errors = 3000
table_cache = 614
external-locking = FALSE
max_allowed_packet =8M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 100
thread_concurrency = 2
query_cache_size = 2M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
thread_stack = 192K
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
pid-file = /data/3306/mysql.pid
log-bin = /data/3306/mysql-bin #主從同步的關(guān)鍵點(diǎn),從庫上不需要開啟
relay-log = /data/3306/relay-bin
relay-log-info-file = /data/3306/relay-log.info
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
expire_logs_days = 7
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
server-id = 1 #主庫從庫ID 不可相同
[mysqldump]
quick
max_allowed_packet = 2M
[mysqld_safe]
log-error=/data/3306/mysql3306.err
pid-file=/data/3306/mysqld.pid
# 配置文件到這里結(jié)束
=========================================
3 創(chuàng)建啟動(dòng)腳本 vi /data/3306/mysql
#啟動(dòng)腳本從這里開始
#!/bin/sh
port=3306
user="root"
pwd="123456" #按實(shí)際密碼填寫
Path="/usr/bin" #按實(shí)際安裝指定mysql可執(zhí)行程序的路徑(可用which mysql查找)
sock="/data/${port}/mysql.sock"
start_mysql()
{
if [ ! -e "$sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${Path}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
stop_mysql()
{
if [ ! -e "$sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${Path}/mysqladmin -u ${user} -p${pwd} -S /data/${port}/mysql.sock shutdown
fi
}
restart_mysql()
{
printf "Restarting MySQL...\n"
stop_mysql
sleep 2
start_mysql
}
case $1 in
start)
start_mysql
;;
stop)
stop_mysql
;;
restart)
restart_mysql
;;
*)
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac
#啟動(dòng)腳本到此結(jié)束
chmod +x /data/3306/mysql #授權(quán)可執(zhí)行
===============================================================
4 為3307也創(chuàng)建一樣的一份,注意把所有3306替換3307 修改不同的server-id
cp /data/3306/mysql /data/3307/
cp /data/3306/my.cnf /data/3307/
sed -i 's/server-id = 1/server-id = 2/g' /data/3307/my.cnf
sed -i 's/3306/3307/g' /data/3307/my.cnf
sed -i 's/3306/3307/g' /data/3307/mysql
三 初始化2個(gè)實(shí)例
#初始化3306端口數(shù)據(jù)庫
mysql_install_db --datadir=/data/3306/data --user=mysql
#啟動(dòng)3306數(shù)據(jù)庫
/data/3306/mysql start
#修改3306實(shí)例登錄密碼
mysqladmin -uroot password '123456' -S /data/3306/mysql.sock
mysql_install_db --datadir=/data/3307/data --user=mysql
/data/3307/mysql start
mysqladmin -uroot password '123456' -S /data/3307/mysql.sock
四 配置主庫
#登錄3306實(shí)例(主庫)
mysql -uroot -p123456 -S /data/3306/mysql.sock
#授權(quán)同步的帳號(hào)密碼
grant replication slave on *.* to rep@'%' identified by'66888888';
#刷新權(quán)限
flush privileges;
#退出
exit
#主庫鎖表
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "flush tables with read lock;"
#查看主庫binlog狀態(tài)
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "show master status;" >/mysql.log
#備份主庫
mysqldump -uroot -p123456 -S /data/3306/mysql.sock -A -B |gzip >/mysql.sql.gz
#主庫解鎖
mysql -uroot -p123456 -S /data/3306/mysql.sock -e "unlock tables;"
備注:執(zhí)行完此步驟后不要再操作主服務(wù)器MYSQL,防止主服務(wù)器狀態(tài)值變化
五 配置從庫
#導(dǎo)入主庫的備份
gzip -d /mysql.sql.gz
mysql -uroot -p'123456' -S /data/3307/mysql.sock < /mysql.sql
mysql -uroot -p'123456' -S /data/3307/mysql.sock
#設(shè)置同步信息
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.7', #服務(wù)器IP
-> MASTER_PORT=3306, #主庫端口
-> MASTER_USER='rep', #同步的用戶
-> MASTER_PASSWORD='66888888', #同步的用戶密碼
-> MASTER_LOG_FILE='mysql-bin.000002', #binlog文件(這個(gè)信息在/mysql.log里哦)
-> MASTER_LOG_POS=424; #位置點(diǎn)(這個(gè)信息在/mysql.log里哦)
備注:可以一次設(shè)置以上信息change master to master_host='192.168.145.222',master_user='mysync',master_password='q123456',
master_log_file='mysql-bin.000004',master_log_pos=308;
#開啟從模式
mysql> start slave;
#查看狀態(tài)
mysql -uroot -p'123456' -S /data/3307/mysql.sock -e "show slave status\G"|egrep "Seconds_Behind_Master|_Running"
#2個(gè)Yes表示成功,如不成功最多可能是上一步帳號(hào)同步設(shè)置可能有問題。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。