实验:一台centos7.5主机实现多实例mysql
规划:
3306 3307 3308 端口,共3个实例
目录:
/data/mysql{3306,3307,3308}
/data/mysql/330{6,7,8}/{etc,log,data,pid,bin,socket}
具体实现:
[root@c7-7 ~]# yum install mariadb-server
[root@c7-7 ~]# rpm -ql mariadb-server
/usr/libexec/mysqld //二进制程序
/usr/bin/mysql_install_db //生成初始化数据库脚本
[root@c7-7 data]# cd /data/
[root@c7-7 data]# mkdir mysql/{3306,3307,3308}/{etc,data,socket,log,bin,pid} -pv、
[root@c7-7 ~]# ls /var/lib/mysql/ //默认数据库文件路径
[root@c7-7 data]# chown -R mysql.mysql mysql/
[root@c7-7 data]# /usr/bin/mysql_install_db --datadir=/data/mysql/3306/data --user=mysql //在(330{6,7,8})实例目录下生成初始化数据库文件。
[root@c7-7 ~]# cp /etc/my.cnf /data/mysql/3306/etc/
[root@c7-7 ~]# vi /data/mysql/3306/etc/my.cnf //把该配置文件分别复制到3307,3308实例目录并修改成3307和3308
[mysqld]
port=3306
datadir=/data/mysql/3306/data
socket=/data/mysql/3306/socket/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/data/mysql/3306/log/mariadb.log
pid-file=/data/mysql/3306/pid/mariadb.pid
[root@c7-7 /data/mysql/3306/bin]# rz mysqld //上传服务脚本到330{6,7,8}/bin/目录下并修改端口
主要修改下面两项即可:
port=3306
mysql_basedir="/data/mysql"
[root@c7-7 3306]# bin/mysqld start //启动成功
[root@c7-7 3307]# bin/mysqld start //启动成功
[root@c7-7 3308]# bin/mysqld start //启动成功
[root@c7-7 ~]# mysql -S /data/mysql/3308/socket/mysql.sock //客户端连接到某一个服务端。

            myslqd脚本:
                    #!/bin/bash
                    #chkconfig: 345 20 80
                    # chkconfig --add mysqld3306  ......
                    port=3306
                    mysql_user="root"
                    mysql_pwd=""
                    cmd_path="/usr/bin"
                    mysql_basedir="/mysqldb"
                    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