注意:请根据自己的实际情况进行相应的更改
D.1多实例结构
D.2多实例安装
D.2.1依赖包
# yum install ncurses-devel -y
# yum install libaio-devel -y
D.2.2通过二进制方法安装mysql
注意:参考“mysql二进制包安装”这一章节
请查考MySQL系列之B------MySQL安装准备、MySQL系列之B------MySQL安装
D.2.3建立mysql账号
# groupadd mysql
# useradd -s /sbin/nologin -g mysql -Mmysql
#注意:useradd命令参数简要说明
#1、-s /sbin/nologin 表示禁止该用户登录,加强安全
#2、-g 指定属于mysql组
#3、-M表示不创建用户家目录
D.2.4创建目录
# mkdir -p /data/{3307,3308}/data
目录解析:
/data实例的总目录
/data/33073307实例的目录
/data/33083308实例的目录
/data/3307/data3307实例的数据目录
/data/3308/data3308实例的数据目录
注意:生产硬件配置:MEM 32g、双CPU8core、硬盘6*600g sas 15k/rpm 2-3个实例
D.2.5初始化
# cd /mysql/mysql
# scripts/mysql_install_db--basedir=/mysql --datadir=/data/3307/data --user=mysql
# scripts/mysql_install_db--basedir=/mysql --datadir=/data/3308/data --user=mysql
D.3多实例配置文件
1、3307配置文件/data/3307/
[client]
port=3307
socket=/data/3307/mysql.sock
prompt=\\u@hostname \\r:\\m:\\s->
[mysqld]
basedir =/mysql
datadir =/data/3307/data
port =3307
server_id =2
socket =/data/3307/mysql.sock
log-bin=/data/3307/mysql_bin
binlog_format=mixed
#binlog-do-db=
#binlog-ignore-db=
long_query_time=1
relay-log=/data/3307/relay_bin
relay-log-info-file=/data/3307/relay_log.info
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/data/3307/mysql_err.log
pid-file=/data/3307/mysql_pid.pid
2、3308配置文件/data/3308/
[client]
port=3308
socket=/data/3308/mysql.sock
prompt=\\u@hostname \\r:\\m:\\s->
[mysqld]
basedir =/mysql
datadir =/data/3308/data
port =3308
server_id =3
socket =/data/3308/mysql.sock
log-bin=/data/3308/mysql_bin
binlog_format=mixed
#binlog-do-db=
#binlog-ignore-db=
long_query_time=1
relay-log=/data/3308/relay_bin
relay-log-info-file=/data/3308/relay_log.info
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/data/3308/mysql_err.log
pid-file=/data/3308/mysql_pid.pid
D.4多实例启动文件
D.4.1创建mysql文件在/data/3307中
#!/bin/bash
############################################################
#this scripts is created by liuweixinat 2015-06-18
#liuweixin QQ:1433203077
############################################################
#init
port=3307
mysql_user="root"
mysql_pwd="oracle"
CmdPath="/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_start_mysql()
{
if[ ! -e "$mysql_sock" ];then
printf"Starting MySQL...\n"
/bin/sh${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
else
printf"MySQL is running...\n"
exit
fi
}
#stop function()
function_stop_mysql()
{
if[ ! -e "$mysql_sock" ];then
printf"Stoping MySQL...\n"
exit
else
printf "Stoping Mysql...\n"
${CmdPath}/mysqladmin-u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
#restart function
function_restart_mysql()
{
printf"Restarting MySQL...\n"
function_stop_mysql
sleep2
function_start_mysql
}
#function_kill_mysql()
#{
# kill-9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
# kill-9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
#}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
#kill)
# function_kill_mysql;;
restart)
function_restart_mysql;;
*)
printf"Usage:/data/${port}/mysql {start|stop|restart}\n"
esac
D.4.2创建mysql文件在/data/3308中
#!/bin/bash
############################################################
#this scripts is created by liuweixinat 2015-06-18
#liuweixin QQ:1433203077
############################################################
#init
port=3308
mysql_user="root"
mysql_pwd="oracle"
CmdPath="/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_start_mysql()
{
if[ ! -e "$mysql_sock" ];then
printf"Starting MySQL...\n"
/bin/sh${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
else
printf"MySQL is running...\n"
exit
fi
}
#stop function()
function_stop_mysql()
{
if[ ! -e "$mysql_sock" ];then
printf"Stoping MySQL...\n"
exit
else
printf "Stoping Mysql...\n"
${CmdPath}/mysqladmin-u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
#restart function
function_restart_mysql()
{
printf"Restarting MySQL...\n"
function_stop_mysql
sleep2
function_start_mysql
}
#function_kill_mysql()
#{
# kill-9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
# kill-9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
#}
case $1 in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
#kill)
# function_kill_mysql;;
restart)
function_restart_mysql;;
*)
printf"Usage:/data/${port}/mysql {start|stop|restart}\n"
esac
D.5启动多实例
[root@mysql03 mysql]# /data/3307/mysql start
Starting MySQL...
[root@mysql03 mysql]# /data/3308/mysql start
Starting MySQL...
[root@mysql03 mysql]# netstat -lnt 330
Active Internet connections (onlyservers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::3307:::* LISTEN
tcp 0 0 :::3308:::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
D.6登录多实例
登录有两种方法:
mysql -uroot -p -S/data/3307/mysql.sock
mysql -h127.0.0.1 -uroot -p --port=330X
注意:如果指定socket文件,则不需要-h127.0.0.1或者-h localhost,同时,也不需要指定--port=330X端口号