美文网首页
数据库多实例搭建

数据库多实例搭建

作者: 荆俊玮 | 来源:发表于2019-11-22 17:49 被阅读0次

    一、环境规划

    1.端口 :

    3307,3308,3309

    2.数据存储路径 :

    /data/330{7..9}/data

    3.配置文件主信息:

    /data/330{7..9}/my.cnf
    port=3307/3308/3309
    server_id=7/8/9
    datadir=/data/330{7..9}/data
    ocket=/tmp/mysql330{7..9}.sock

    二、 搭建部署

    1. 创建关键目录并授权

    mkdir -p /data/330{7..9}/data 
    chown -R mysql.mysql /data/*
    

    2.初始化数据

    mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data  
    mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3308/data  
    mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3309/data  
    

    补充:若在搭建多实例前,服务器本身存在一台MySQL数据库,则需先将本有数据库关闭,并将默认配置文件my.cnf文件存放位置迁移,多实例创建完成后再将其迁回,并重启原有数据库

    mv /etc/my.cnf  /tmp
    

    3.准备配置文件

    cat > /data/3307/my.cnf <<EOF
    [mysqld]
    user=mysql
    basedir=/usr/local/mysql
    datadir=/data/3307/data
    port=3307
    server_id=7
    socket=/tmp/mysql3307.sock
    EOF
    
    cat > /data/3308/my.cnf <<EOF
    [mysqld]
    user=mysql
    basedir=/server/tools/mysql
    datadir=/data/3308/data
    port=3308
    server_id=8
    socket=/tmp/mysql3308.sock
    EOF
    
    cat > /data/3309/my.cnf <<EOF
    [mysqld]
    user=mysql
    basedir=/usr/local/mysql
    datadir=/data/3309/data
    port=3309
    server_id=9
    socket=/tmp/mysql3309.sock
    EOF
    

    4.准备启动脚本

    cat > /etc/systemd/system/mysqld3307.service <<EOF
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
    LimitNOFILE = 5000
    EOF
    
    cat > /etc/systemd/system/mysqld3308.service <<EOF
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
    LimitNOFILE = 5000
    EOF
    
    cat > /etc/systemd/system/mysqld3309.service <<EOF
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
    LimitNOFILE = 5000
    EOF
    

    5.启动多实例

    systemctl start mysqld3307
    systemctl start mysqld3308
    systemctl start mysqld3309
    

    检查是否启动

    netstat -tulnp|grep mysqld
    tcp6       0      0 :::3307                 :::*                    LISTEN      10327/mysqld        
    tcp6       0      0 :::3308                 :::*                    LISTEN      10361/mysqld        
    tcp6       0      0 :::3309                 :::*                    LISTEN      10395/mysqld   
    

    6.登录数据库

    mysql -S /tmp/mysql3307.sock
    mysql -S /tmp/mysql3308.sock
    mysql -S /tmp/mysql3309.sock
    

    说明:使用多实例登陆到不同的数据库时,一定要指定该数据库的本地socket文件路径

    相关文章

      网友评论

          本文标题:数据库多实例搭建

          本文链接:https://www.haomeiwen.com/subject/eumqwctx.html