什么是MySQL多实例呢?首先说说MySQL实例是什么,一个MySQL实例的启动就包含了进程,线程和内存结构,当然这里还包含了数据,那么多实例就是多套MySQL实例同时启动,说得再简单一点就是,在同一套MySQL软件下启动多个MySQL服务进程。

构建多套MySQL服务思路:

  1. 启动多个MySQL进程。
  2. 规划多套数据。
  3. 规划多个端口。
  4. 规划多套日志路径。
     

多实例配置:(建议主机分配内存在2G以上)

  1. 创建多套目录(以端口号命名)
    [root@db01 data]# mkdir -p /data/330{7,8,9}
    [root@db01 data]# ll
    total 12
    drwxr-xr-x 2 root root 4096 Aug  2 13:34 3307
    drwxr-xr-x 2 root root 4096 Aug  2 13:34 3308
    drwxr-xr-x 2 root root 4096 Aug  2 13:34 3309
    [root@db01 data]# 
  2. 准备多套配置文件。
    3307:
    [root@db01 data]# vim /data/3307/my.cnf
    
    [mysqld]
    basedir=/application/mysql
    datadir=/data/3307
    server-id=3307
    port=3307
    log-bin=/data/3307/mysql-bin
    socket=/data/3307/mysql.sock
    log-error=/data/3307/mysql.log

    3308:

    [root@db01 data]# vim /data/3308/my.cnf
    
    [mysqld]
    basedir=/application/mysql
    datadir=/data/3308
    server-id=3308
    port=3308
    log-bin=/data/3308/mysql-bin
    socket=/data/3308/mysql.sock
    log-error=/data/3308/mysql.log

    3309:

    [root@db01 data]# vim /data/3309/my.cnf
    
    [mysqld]
    basedir=/application/mysql
    datadir=/data/3309
    server-id=3309
    port=3309
    log-bin=/data/3309/mysql-bin
    socket=/data/3309/mysql.sock
    log-error=/data/3309/mysql.log

     

  3. 初始化多套数据

    /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/data/3307 --user=mysql
    /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/data/3308 --user=mysql
    /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/data/3309 --user=mysql

     

  4. 启动多个实例

    [root@db01 ~]# mysqld_safe --defaults-file=/data/3307/my.cnf &
    [1] 1266
    [root@db01 ~]# 190802 13:58:36 mysqld_safe Logging to '/data/3307/mysql.log'.
    190802 13:58:36 mysqld_safe Starting mysqld daemon with databases from /data/3307
    
    [root@db01 ~]# mysqld_safe --defaults-file=/data/3309/my.cnf &
    [1] 1457
    [root@db01 ~]# 190802 13:59:07 mysqld_safe Logging to '/data/3309/mysql.log'.
    190802 13:59:07 mysqld_safe Starting mysqld daemon with databases from /data/3309
    
    [root@db01 ~]# mysqld_safe --defaults-file=/data/3308/my.cnf &
    [2] 1631
    [root@db01 ~]# 190802 13:59:13 mysqld_safe Logging to '/data/3308/mysql.log'.
    190802 13:59:13 mysqld_safe Starting mysqld daemon with databases from /data/3308

     

  5. 查看启动端口

    [root@db01 ~]# netstat -lntup|grep 330
    tcp        0      0 :::3307                     :::*                        LISTEN      1415/mysqld         
    tcp        0      0 :::3308                     :::*                        LISTEN      1780/mysqld         
    tcp        0      0 :::3309                     :::*                        LISTEN      1609/mysqld         
    [root@db01 ~]# 

     

  6. 分别连接测试

    [root@db01 ~]# mysql -S /data/3307/mysql.sock -e "show variables like 'server_id';"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 3307  |
    +---------------+-------+
    [root@db01 ~]# mysql -S /data/3308/mysql.sock -e "show variables like 'server_id';"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 3308  |
    +---------------+-------+
    [root@db01 ~]# mysql -S /data/3309/mysql.sock -e "show variables like 'server_id';"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 3309  |
    +---------------+-------+
    [root@db01 ~]# 


     

是不是很简单,以后就不用安装多个数据库了,直接启动多个实例。

最后修改于 2019-08-02 11:10:42
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇