MySQL单机多实例配置(2)

###3306
# The following options will be passed to all MySQL clients
[client]
#password      = your_password
port            = 3306
socket          = /tmp/mysql_3306.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql_3306.sock
pid-file        = /mydata/data/3306/mysql.pid
user            = mysql
basedir        = /usr/local/mysql
datadir        = /mydata/data/3306

###########################################################

###3307
# The following options will be passed to all MySQL clients
[client]
#password      = your_password
port            = 3307
socket          = /tmp/mysql_3307.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3307
socket          = /tmp/mysql_3307.sock
pid-file        = /mydata/data/3307/mysql.pid
user            = mysql
basedir        = /usr/local/mysql
datadir        = /mydata/data/3307
5、修改数据目录的属主、属组
[root@localhost mysql]# chown -R mysql:mysql /mydata/data/3306
[root@localhost mysql]# chown -R mysql:mysql /mydata/data/3307
6、把mysql/bin目录添加到PATH
[root@localhost mysql]# vi /etc/profile.d/mysql.sh
# 添加
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]# . /etc/profile.d/mysql.sh
7、初始化
# 初始化 实例1
[root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/3306 --user=mysql
# 初始化 实例2
[root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/3307 --user=mysql
8、启动/关闭 实例


这里有一个问题,每个实例如何读取各自的my.cnf配置文件呢? 我们需要手动指定


/usr/local/mysql/bin/mysqld_safe


--defaults-file    手动指定配置文件


### 启动实例
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe  --defaults-file=/mydata/data/3306/my.cnf &>/dev/null &
[1] 1526
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe  --defaults-file=/mydata/data/3307/my.cnf &>/dev/null &
[2] 1832

[root@localhost mysql]# netstat -tulpn | grep -i mysql
tcp        0      0 0.0.0.0:3306          0.0.0.0:*          LISTEN      1815/mysqld       
tcp        0      0 0.0.0.0:3307          0.0.0.0:*          LISTEN      2121/mysqld

### 关闭实例, 先不要操作哈
[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -uroot -p -S /tmp/mysql_3306.sock shutdown
[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -uroot -p -S /tmp/mysql_3307.sock shutdown
# 注意:这里mysql的root用户并没有设置密码,密码提示直接敲 Enter即可
OK, 我们看到mysqld 监听在3306,3307两个端口上。

这里,我们也可以提供一个服务启动脚本来进行管理,大家去看看support-files/mysql.server这个服务启动脚本吧。

下面提供一个脚本模板:(大家根据需求更改)
#!/bin/bash

. /etc/init.d/functions

PORT=$2

USER=root
PASSWD=

MYSQLBIN='/usr/local/mysql/bin'
SOCKETFILE="/tmp/mysql_${PORT}.sock"
PIDFILE="/mydata/data/${PORT}/mysql.pid"
MYCNF="/mydata/data/${PORT}/my.cnf"

[[ $# -eq 2 ]] || {
 echo "Usage: $0 {start|stop|restart|reload}  {PORT}"
 exit 1
}


mysql_start() {
 [[ -e "$SOCKETFILE" ]] && {
  action "MySQL port: $PORT IS already running" /bin/false
  exit 0
 } || {
  action "Starting MySQL...  please wait" /bin/true
  $MYSQLBIN/mysqld_safe --defaults-file=$MYCNF &> /dev/null &
 }

[[ "$?" == "0" ]] && {
  action "MySQL has been Started" /bin/true
 } || {
  action "MySQL Started" /bin/false
 }
}


mysql_stop() {
 [[ ! -e "$SOCKETFILE" ]] && {
  action "MySQL port:$PORT was already down" /bin/false
 } || {
  $MYSQLBIN/mysqladmin -u $USER -p$PASSWD -S $SOCKETFILE shutdown &>/dev/null
 }
 [[ "$?" == 0 ]] && {
  action  "MySQL port:$PORT has been Stopped" /bin/true
 }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/18c75b1f5a618ec03cca73da386860a8.html