03-MySql-多实例管理

87 阅读2分钟

1.多实例管理

  1. 创建数据目录
mkdir -p  /linux0224/mysql_3307
mkdir -p  /linux0224/mysql_3308
  1. 初始化2个实例数据
mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/linux0224/mysql_3307

mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/linux0224/mysql_3308
  1. 创建mysql_3307.cnf和mysql_3308.cnf 文件
cat >/etc/mysql_3307.cnf <<'EOF'
[mysqld]
port=3307
user=mysql
basedir=/opt/mysql/
datadir=/linux0224/mysql_3307/
socket=/linux0224/mysql_3307/mysql.sock
log_error=/linux0224/mysql_3307/mysql.log
EOF
cat >/etc/mysql_3308.cnf <<'EOF'

[mysqld]
port=3308
user=mysql
basedir=/opt/mysql/
datadir=/linux0224/mysql_3308/
socket=/linux0224/mysql_3308/mysql.sock
log_error=/linux0224/mysql_3308/mysql.log
EOF
  1. 检查配置文件
[root@tech-db-51 /linux0224]#ls /etc/my*
/etc/my.cnf  /etc/mysql_3307.cnf  /etc/mysql_3308.cnf

5.生成3307和3308的 启动脚本

cat > /linux0224/3307.sh <<'EOF'
port="3307"
mysql_user="mysql"
Cmdpath="/opt/mysql/bin/"
# socket用于判断程序是否运行
# 程序运行中,该socket文件存在
# 进程挂了,socket文件自动消失

mysql_sock="/linux0224/mysql_${port}/mysql.sock"
# 定义路径,mysql进程启动后,一个存储该进程pid号码的文件在哪
mysqld_pid_file_path=/linux0224/mysql_${port}/mysqld_${port}.pid

# 启动mysqld服务端的入口命令
start(){
if [ ! -e "$mysql_sock" ];then
    printf "Starting MySQL...\n"
    #  mysql的启动逻辑
    # mysqld_safe 脚本 >  mysqld 脚本 > 运行mysql进程
  
    /bin/sh ${Cmdpath}/mysqld_safe --defaults-file=/etc/mysql_${port}.cnf --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &
    sleep 3
else
    printf "MySQL is running...\n"
    exit 1
fi
}


stop(){
    if [ ! -e "$mysql_sock" ];then
        printf "MySQL is stopped...\n"
        exit 1
    else
        printf "Stoping MySQL...\n"
        mysqld_pid=`cat "$mysqld_pid_file_path"`
    if (kill -0 $mysqld_pid 2>/dev/null)
        then
        kill $mysqld_pid
        sleep 2
        fi
    fi
}



restart(){
    printf "Restarting MySQL...\n"
    stop
    sleep 2
    start
}



case "$1" in
start)
    start
;;
stop)
    stop
;;
restart)
    restart
;;
*)
    printf "Usage: /data/${port}/mysql{start|stop|restart}\n"
esac
EOF
  1. 启动 3307和3308
bash 3307.sh start

bash 3308.sh start

[root@tech-db-51 /linux0224]#netstat -tunlp|grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      2018/mysqld         
tcp6       0      0 :::3307                 :::*                    LISTEN      12514/mysqld        
tcp6       0      0 :::3308                 :::*                    LISTEN      12700/mysqld  
  1. 设置多实例的密码

多实例初始化密码的时候,需要执行-S sock套接字

==================3306=========
mysqladmin -uroot -p   -S /tmp/mysql.sock  password linux0224

# ==================3307=========

mysqladmin -uroot -p -S /linux0224/mysql_3307/mysql.sock password  linux0224


# ==================3308=========

mysqladmin -uroot -p -S /linux0224/mysql_3308/mysql.sock password  linux0224
  1. 登陆多实例
  • IP:Port登陆
mysql -uroot -plinux0224 -h127.0.0.1 -P3306
password:
mysql: show global variables like 'port';
[Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+

---------------------------------------

mysql -uroot -plinux0224 -h127.0.0.1 -P3307
password:
mysql: show global variables like 'port';
[Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3307  |
+---------------+-------+

---------------------------------------

mysql -uroot -plinux0224 -h127.0.0.1 -P3308
password:
mysql: show global variables like 'port';
[Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3308  |
+---------------+-------+

  • Sock文件方式登陆
mysql -uroot -plinux3308 -S /linux0224/mysql_3308/mysql.sock  -e "show global variables like 'port';"

mysql -uroot -plinux3307 -S /linux0224/mysql_3307/mysql.sock  -e "show global variables like 'port';"

mysql -uroot -plinux3306 -S /tmp/mysql.sock  -e "show global variables like 'port';"