MySQL多執行個體配置
實驗環境:RHEL6.4為迷你安裝,mysql安裝包為通用二進位安裝包,版本為mysql-5.6.26
1.建立mysql使用者
#useradd –M –s /sbin/nologin mysql
#yum –y install ncurses-devel libaio-devel
#安裝mysql的依賴包,否則下面無法初始化成功
2.軟體包解壓縮
# tar xf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz -C /usr/local
# mv /usr/local/mysql-5.6.26-linux-glibc2.5-x86_64/ /usr/local/mysql
3.建立目錄
# mkdir /data/{3306,3307}/data –pv
# mkdir /data/{3306,3307}/log –pv
# tree /data/ #查看分類樹
4. /data/3306中建立my.cnf
# cd /data/3306/
# vim my.cnf
[client]
port = 3306
socket = /data/3306/mysql.sock
[mysqld]
port=3306
socket = /data/3306/mysql.sock
pid-file = /data/3306/data/mysql.pid
basedir = /usr/local/mysql
datadir = /data/3306/data
server-id=1
#log-bin=mysql-bin
#log-bin-index= mysql-bin.index
# LOGGING
log_error=/data/3306/log/mysql-error.log
slow_query_log_file=/data/3306/log/mysql-slow.log
slow_query_log=1
5. /data/3307中建立my.cnf
# cd ../3307/
# vim my.cnf
[client]
port = 3307
socket = /data/3307/mysql.sock
[mysqld]
port=3307
socket = /data/3307/mysql.sock
pid-file = /data/3307/data/mysql.pid
basedir = /usr/local/mysql
datadir = /data/3307/data
server-id=2
#log-bin=mysql-bin
#log-bin-index= mysql-bin.index
# LOGGING
log_error=/data/3307/log/mysql-error.log
slow_query_log_file=/data/3307/log/mysql-slow.log
slow_query_log=1
6. 在/data/3306中建立mysql開機檔案
# cd /data/3306/
# vim mysql
#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
port=3306
mysql_user="root"
mysql_pwd=""
mysql_sock="/data/${port}/mysql.sock"
CmdPath="/usr/local/mysql/bin"
#startup function
usage(){
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
}
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
action "Starting MySQL..." /bin/true
else
printf "MySQL is running...\n"
exit
fi
}
#stop function
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
action "Stoping MySQL..." /bin/true
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
#restart function
function_restart_mysql()
{
function_stop_mysql &>/dev/null
sleep 2
function_start_mysql &>/dev/null
action "Restarting MySQL..." /bin/true
}
if [ $# -ne 1 ];then
usage
fi
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
usage
esac
#chmod +x mysql
7、在/data/3307中建立mysql開機檔案
# cd /data/3307/
# vim mysql
#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
port=3307
mysql_user="root"
mysql_pwd=""
mysql_sock="/data/${port}/mysql.sock"
CmdPath="/usr/local/mysql/bin"
usage(){
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
}
#startup function
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
action "Starting MySQL..." /bin/true
else
printf "MySQL is running...\n"
exit
fi
}
#stop function
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
action "Stoping MySQL..." /bin/true
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
#restart function
function_restart_mysql()
{
function_stop_mysql &>/dev/null
sleep 2
function_start_mysql &>/dev/null
action "Restarting MySQL..." /bin/true
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
usage
esac
#chmod +x mysql
8. 修改檔案擁有者和許可權
#chown -R mysql:mysql /data
9. 添加mysql啟動路徑
#echo 'export PATH=$PATH:/usr/local/mysql/bin' >>/etc/profile
#source /etc/profile
10. 初始化資料庫
# cd /usr/local/mysql/scripts/
#./mysql_install_db --defaults-file=/data/3306/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data
#./mysql_install_db --defaults-file=/data/3307/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data
11. 啟動mysql
#/data/3306/mysql start
#/data/3307/mysql start
#netstat -lntp | grep 330 #查看是否啟動進程
12. 登陸mysql
①# mysql -S /data/3306/mysql.sock #剛安裝完的mysql是沒有登陸密碼的
#如果不成功,檢查/data/3306/log目錄下的mysql-error.log日誌,逐一排除錯誤
如果登陸成功,下面就修改登入密碼
mysql> update mysql.user set password=password("123456") where user='root';
mysql> flush privileges;
不建議在shell環境下修改密碼,否則別人只要查看命令曆史就能看到密碼。當然你也可以是shell下進行,但是記得要清楚曆史命令記錄。
②同理,使用上面的方法修改3307的登陸密碼
# mysql -S /data/3307/mysql.sock
mysql> update mysql.user set password=password("123456") where user='root';
mysql> flush privileges;
③要把上面更改後的密碼回寫到mysql的開機檔案中(否則每次啟動、關閉、重啟mysql都要輸入密碼)
# sed -i 's/mysql_pwd=\"\"/mysql_pwd=\"123456\"/g' /data/3306/mysql
#sed -i 's/mysql_pwd=\"\"/mysql_pwd=\"123456\"/g' /data/3307/mysql
也可以手動進行修改
#vim /data/3306/mysql
# vim /data/3307/mysql
13. 重啟mysql
#/data/3306/mysql restart
#/data/3307/mysql restart
#netstat -lntp | grep 330
14.知識點
進入mysql時,要記得加-S 指定mysql通訊端的路徑
# mysql –u root –p -S /data/3307/mysql.sock
下面的命令可以平滑關閉mysql
# mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown
本文永久更新連結地址: