欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql 多实例部署

程序员文章站 2022-09-27 14:07:27
Centos7.6 部署3个Mariadb 实例 ......

centos7.6 部署3个mariadb 实例

[root@localhost ~]# yum install mariadb-server -y

# 创建对应的目录文件
[root@localhost ~]# mkdir /mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid} -p

chown -r mysql.mysql /mysql

# 初始化数据库文件
[root@localhost ~]# mysql_install_db --datadir=/mysql/3306/data/ --user=mysql
[root@localhost ~]# mysql_install_db --datadir=/mysql/3307/data/ --user=mysql
[root@localhost ~]# mysql_install_db --datadir=/mysql/3308/data/ --user=mysql

# 创建对应配置文件
[root@localhost ~]# cp /etc/my.cnf /mysql/3306/etc
# 修改对应的配置文件
[mysqld]
port=3306
datadir=/mysql/3306/data
socket=/mysql/3306/socket/mysql.sock
symbolic-links=0

[mysqld_safe]
log-error=/mysql/3306/log/mariadb.log
pid-file=/mysql/3306/pid/mariadb.pid

# 另外的2个实例调整对应的端口即可

# 准备启动脚本 另外的实例修改对应的端口即可
[root@localhost ~]#  vim /mysql/3306/bin/mysqld
#!/bin/bash
#chkconfig: 345 80 2
port=3306
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "starting mysql...\n"
      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &
    else
      printf "mysql is running...\n"
      exit
    fi
}

function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "mysql is stopped...\n"
       exit
    else
       printf "stoping mysql...\n"
       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -s ${mysql_sock} shutdown
   fi
}

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: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac

# 启动服务
[root@localhost ~]# /mysql/3306/bin/mysqld start
starting mysql...
[root@localhost ~]# /mysql/3307/bin/mysqld start
starting mysql...
[root@localhost ~]# /mysql/3308/bin/mysqld start
starting mysql...

# 连接测试
mysql -s /mysql/3308/socket/mysql.sock -e 'select version()'
+----------------+
| version()      |
+----------------+
| 5.5.60-mariadb |
+----------------+

# 关闭数据库
[root@localhost ~]# /mysql/3306/bin/mysqld stop
stoping mysql...
enter password: