美文网首页
Mysql-5.7.26 多实例

Mysql-5.7.26 多实例

作者: Odven | 来源:发表于2020-05-11 15:11 被阅读0次

    1) 下载mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

    wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
    

    2) 解压,配置环境变量

    mkdir /opt/mysql_process/ -p
    
    tar -xf  mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz  -C /opt/mysql_process/
    cd /opt/mysql_process/
    mv mysql-5.7.26-linux-glibc2.12-x86_64/ mysql
    
    echo '''
    
    MYSQL_BASE_DIR=/opt/mysql_process/mysql
    export PATH=$PATH:${MYSQL_BASE_DIR}/bin
    '''  >> /etc/profile
    
    source /etc/profile 
    

    3) 创建数据目录,启动用户,修改权限

    useradd -s /sbin/nologin mysql
    mkdir /data/mysql/3306/data -p
    
    # 修改权限
    chown -R mysql:mysql  /data/mysql/
    chown -R mysql:mysql  /opt/mysql_process/
    

    4) 初始化msyql

    mysqld --initialize-insecure  --user=mysql --basedir=/opt/mysql_process/mysql --datadir=/data/mysql/3306/data
    

    5) 配置my.cnf

    rpm -qa | grep mariadb
    mariadb-libs-5.5.56-2.el7.x86_64
    
    yum remove mariadb-libs-5.5.56-2.el7.x86_64
    
    cat > /etc/my.cnf << EOF
    [mysqld]
    user = mysql
    bind-address = 0.0.0.0
    port = 3306
    basedir = /opt/mysql_process/mysql
    datadir = /data/mysql/3306/data
    socket = /data/mysql/3306/mysqld.sock
    pid-file = /data/mysql/3306/mysqld.pid
    log_error = /data/mysql/3306/error.log
    
    [mysql]
    socket = /data/mysql/3306/mysqld.sock
    
    EOF
    

    6) systemd管理

    cat > /usr/lib/systemd/system/mysqld.service << EOF
    [Unit]
    Description=MySQL5.7.26  Server
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/opt/mysql_process/mysql/bin/mysqld --defaults-file=/etc/my.cnf
    LimitNOFILE = 5000
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    systemctl daemon-reload
    systemctl restart mysqld.service
    

    7) 多实例就是重复上面的3,4,5,6步 不用执行 useradd -s /sbin/nologin mysql

    1. 要创建各自的数据目录并设置权限
    2. 初始化的时候要各自初始化各自的数据目录
    3. 修改配置文件名并修改里面的 port datadir  socket pid-file log-error 到相应的位置
    4. 修改systemd的 --defaults-file 指向各自的配置文件 并修改 mysqld.service 为自己方便管理的名字
    

    8) 部署Mysql多实例的脚本(执行这个脚本之前要完成上面的1,2步)

    # 部署3380 3381 3382
    
    
    
    cat > deploy_mysql_multi.sh << EOF
    #!/bin/bash
    
    for i in {0..2}
    do
    
    mkdir /data/mysql/338\${i}/{data,log,pid} -p
    
    echo '''
    [mysqld]
    user = mysql
    bind-address = 0.0.0.0
    port = 3306
    basedir = /opt/mysql_process/mysql
    datadir = /data/mysql/3306/data
    socket = /data/mysql/3306/pid/mysqld.sock
    pid-file = /data/mysql/3306/pid/mysqld.pid
    log_error = /data/mysql/3306/log/error.log
    
    [mysql]
    socket = /data/mysql/3306/pid/mysqld.sock
    
    '''  > /etc/my_338\${i}.cnf
    
    sed -i "s/3306/338\${i}/g" /etc/my_338\${i}.cnf 
    
    echo '''
    [Unit]
    Description=MySQL5.7.26  Server
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/opt/mysql_process/mysql/bin/mysqld --defaults-file=/etc/my.cnf
    LimitNOFILE = 5000
    
    [Install]
    WantedBy=multi-user.target
    
    ''' > /usr/lib/systemd/system/mysqld338\${i}.service
    
    sed -i "s#my.cnf#my_338\${i}.cnf#g" /usr/lib/systemd/system/mysqld338\${i}.service
    
    done
    
    num=$(grep mysql /etc/passwd | wc -l)
    [ $num -ne 1 ] && useradd -s /sbin/nologin mysql 2&>/dev/null
    
    chown -R mysql:mysql  /data/mysql/
    chown -R mysql:mysql  /opt/mysql_process/
    
    
    # 初始化
    mysqld --initialize-insecure  --user=mysql --basedir=/opt/mysql_process/mysql --datadir=/data/mysql/3380/data
    mysqld --initialize-insecure  --user=mysql --basedir=/opt/mysql_process/mysql --datadir=/data/mysql/3381/data
    mysqld --initialize-insecure  --user=mysql --basedir=/opt/mysql_process/mysql --datadir=/data/mysql/3382/data
    
    systemctl daemon-reload
    systemctl restart mysqld3380.service
    systemctl restart mysqld3381.service
    systemctl restart mysqld3382.service
    
    EOF
    

    9) 查看启动情况

    ps aux | grep mysql
    
    mysql      2408  0.0  4.4 1117764 171104 ?      Ssl  21:24   0:00 /opt/mysql_process/mysql/bin/mysqld --defaults-file=/etc/my.cnf
    mysql      3108  0.1  4.4 1117356 171112 ?      Ssl  21:39   0:00 /opt/mysql_process/mysql/bin/mysqld --defaults-file=/etc/my_3380.cnf
    mysql      3115  0.0  4.4 1117356 171112 ?      Ssl  21:39   0:00 /opt/mysql_process/mysql/bin/mysqld --defaults-file=/etc/my_3381.cnf
    mysql      3122  0.0  4.4 1117356 171112 ?      Ssl  21:39   0:00 /opt/mysql_process/mysql/bin/mysqld --defaults-file=/etc/my_3382.cnf
    

    10) 登录Mysql

    mysql -S /data/mysql/3380/pid/mysqld.sock
    

    相关文章

      网友评论

          本文标题:Mysql-5.7.26 多实例

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