mysql 一主机多实例

基于centos6.5    mysql5.6.13 

1.依赖包安装

yum install pcre pcre-devel gcc-c++ gcc zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel zlib openssl openssl-devel gcc gcc-c++ make lrzsz cmake ncurses-devel libaio-devel bzip2-devel tree -y

2.创建用户

groupadd mysql
useradd -s /sbin/nologin -g mysql -M mysql

 

3.解压,编译 安装

tar zxf mysql-5.6.13.tar.gz && cd mysql-5.6.13

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci

make&&make install

chown -R mysql.mysql /usr/local/mysql/

mkdir -p /data/{3306,3307}/data

cp /usr/local/mysql/support-files/my-default.cnf /data/3306/my.cnf
cp /usr/local/mysql/support-files/my-default.cnf /data/3307/my.cnf

sed -i '/# log_bin/aport = 3306' /data/3306/my.cnf
sed -i '/# log_bin/asocket = /data/3306/mysql.sock' /data/3306/my.cnf
sed -i '/# log_bin/adatadir =/data/3306/data' /data/3306/my.cnf
sed -i '/# log_bin/abasedir =/usr/local/mysql' /data/3306/my.cnf

sed -i '/# log_bin/aport = 3307' /data/3307/my.cnf
sed -i '/# log_bin/asocket = /data/3307/mysql.sock' /data/3307/my.cnf
sed -i '/# log_bin/adatadir =/data/3307/data' /data/3307/my.cnf
sed -i '/# log_bin/abasedir =/usr/local/mysql' /data/3307/my.cnf

4. 初始化

cd /usr/local/mysql/scripts/

./mysql_install_db --defaults-file=/data/3306/my.cnf --basedir=/usr/local/mysql --datadir=/data/3306/data --user=mysql
 ./mysql_install_db --defaults-file=/data/3307/my.cnf --basedir=/usr/local/mysql --datadir=/data/3307/data --user=mysql

echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile

source /etc/profile

5.启动脚本

#!/bin/sh
#init
port=3306
mysql_user="root"
mysql_pwd="123456"
CmdPath="/usr/local/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 "MySQL is stopped...\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
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

 

[root@web1 ~]# chown -R mysql.mysql /data

[root@web1 ~]# chmod +x /data/3306/mysql
[root@web1 ~]# chmod +x /data/3307/mysql

更改权限 700
[root@web1 data]# chmod 700 /data/3306/mysql
[root@web1 data]# chmod 700 /data/3307/mysql

启动:
[root@web1 data]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &
[1] 3704
[root@web1 data]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null &
[2] 3862


设置密码
[root@web1 data]# mysqladmin -uroot password 123456 -S /data/3306/mysql.sock
[root@web1 data]# mysqladmin -uroot password 123456 -S /data/3307/mysql.sock

关闭数据库
[root@web1 data]# mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown
Warning: Using a password on the command line interface can be insecure.
[1]- Done /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null


[root@web1 data]# mysqladmin -uroot -p123456 -S /data/3307/mysql.sock shutdown
Warning: Using a password on the command line interface can be insecure.
[2]+ Done /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf 2>&1 > /dev/null

启动
[root@web1 scripts]# /data/3306/mysql start
[root@web1 scripts]# /data/3307/mysql start

登录mysql
[root@web1 data]# mysql -uroot -p123456 -S /data/3307/mysql.sock


开机自动启动
echo "/data/3306/mysql start" >> /etc/rc.local
echo "/data/3307/mysql start" >> /etc/rc.local

猜你喜欢

转载自www.cnblogs.com/augustyang/p/9329024.html