Linux服务管理-mysql多实例部署

mysql多实例部署

一.手动部署

软件下载

[root@localhost ~]# cd /usr/local/
[root@localhost local]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

配置用户和组并解压二进制程序至/usr/local下

//创建用户和组
[root@localhost local]# useradd -r -M -s /sbin/nologin -u 306 mysql

//解压软件至/usr/local/
[root@localhost local]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz 
[root@localhost local]# ln -s mysql-5.7.30-linux-glibc2.12-x86_64 mysql

//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql mysql*

//配置环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# . /etc/profile.d/mysql.sh

创建各实例数据存放的目录

[root@localhost local]# cd
[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/data/
total 0
drwxr-xr-x 2 mysql mysql 6 Jun 18 09:39 3306
drwxr-xr-x 2 mysql mysql 6 Jun 18 09:39 3307
drwxr-xr-x 2 mysql mysql 6 Jun 18 09:39 3308

初始化各实例

//初始化3306实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3306 --user=mysql
2020-06-18T13:41:59.033716Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-18T13:41:59.353647Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-18T13:41:59.405124Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-18T13:41:59.463708Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7bc86e9e-b169-11ea-b191-000c290cb70c.
2020-06-18T13:41:59.464479Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-18T13:42:00.565756Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-18T13:42:00.943848Z 1 [Note] A temporary password is generated for root@localhost: IllfxBfr,8aA
[root@localhost ~]# echo 'IllfxBfr,8aA' > 3306.pass

//初始化3307实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3307 --user=mysql
2020-06-18T13:43:13.801787Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-18T13:43:14.240696Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-18T13:43:14.295089Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-18T13:43:14.356484Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: a86c29b4-b169-11ea-b443-000c290cb70c.
2020-06-18T13:43:14.357356Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-18T13:43:15.120723Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-18T13:43:16.108791Z 1 [Note] A temporary password is generated for root@localhost: vlVj5ihKw*WZ
[root@localhost ~]# echo 'vlVj5ihKw*WZ' > 3307.pass

//初始化3308实例
[root@localhost ~]# mysqld --initialize --datadir=/opt/data/3308 --user=mysql
2020-06-18T13:43:54.078727Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-18T13:43:54.439606Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-18T13:43:54.508834Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-18T13:43:54.569020Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: c0641be3-b169-11ea-b77e-000c290cb70c.
2020-06-18T13:43:54.571481Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-18T13:43:55.100018Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-18T13:43:56.036871Z 1 [Note] A temporary password is generated for root@localhost: ak&_6V_sCeEe
[root@localhost ~]# echo 'ak&_6V_sCeEe' > 3308.pass

安装perl

[root@localhost ~]# yum -y install perl

配置配置文件/etc/my.cnf

[root@localhost ~]# > /etc/my.cnf
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin

[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log

[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /opt/data/3307/mysql_3307.pid
log-error=/var/log/3307.log

[mysqld3308]
datadir = /opt/data/3308
port = 3308
socket = /tmp/mysql3308.sock
pid-file = /opt/data/3308/mysql_3308.pid
log-error=/var/log/3308.log

启动各实例

[root@localhost ~]# mysqld_multi start 3306
[root@localhost ~]# mysqld_multi start 3307
[root@localhost ~]# mysqld_multi start 3308
[root@localhost ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      100     127.0.0.1:25                          *:*                  
LISTEN      0      128             *:22                          *:*                  
LISTEN      0      100         [::1]:25                       [::]:*                  
LISTEN      0      80           [::]:3306                     [::]:*                  
LISTEN      0      80           [::]:3307                     [::]:*                  
LISTEN      0      80           [::]:3308                     [::]:*                  
LISTEN      0      128          [::]:22                       [::]:*

初始化密码

//修改3306实例密码
[root@localhost ~]# cat 3306.pass 
IllfxBfr,8aA
[root@localhost ~]# mysql -uroot -p'IllfxBfr,8aA' -S /tmp/mysql3306.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password=password('12321');    //设置密码
Query OK, 0 rows affected, 1 warning (0.01 sec)

//修改3307实例
[root@localhost ~]# cat 3307.pass 
vlVj5ihKw*WZ
[root@localhost ~]# mysql -uroot -p'vlVj5ihKw*WZ' -S /tmp/mysql3307.sock --connect-expired-password -e "set password=password('12321');"
mysql: [Warning] Using a password on the command line interface can be insecure.

//修改3308实例
[root@localhost ~]# cat 3308.pass 
ak&_6V_sCeEe
[root@localhost ~]# mysql -uroot -p'ak&_6V_sCeEe' -S /tmp/mysql3308.sock --connect-expired-password -e "set password=password('12321');"
mysql: [Warning] Using a password on the command line interface can be insecure.

二. 脚本部署

#!/bin/bash

install_dir=/usr/local
soft=mysql-5.7.30-linux-glibc2.12-x86_64
port=3306
read -p "请输入要创建的实例个数:" count
datadir=/opt/mysqldata
yum -y -q install perl

if [ ! -d $install_dir/$soft ];then
    tar xf software/${soft}.tar.gz -C $install_dir
fi

id mysql &>/dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin -u 306 mysql
fi

cd $install_dir
ln -s $soft mysql &>/dev/null
chown -R mysql.mysql $install_dir/mysql*

echo "export PATH=$install_dir/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh

cat > /etc/my.cnf <<EOF
[mysqld_multi]
mysqld = $install_dir/mysql/bin/mysqld_safe
mysqladmin = $install_dir/mysql/bin/mysqladmin
EOF

for i in $(seq $count);do

if [ ! -d $datadir/$port ];then
    mkdir -p $datadir/$port
    chown -R mysql.mysql $datadir
fi

init_status=$(ls $datadir/$port|wc -l)
if [ $init_status -eq 0 ];then
    $install_dir/mysql/bin/mysqld --initialize --user=mysql --datadir=$datadir/$port &>/tmp/pass
    passwd=$(grep 'password' /tmp/pass|awk '{print $NF}')
fi

cat >> /etc/my.cnf <<EOF

[mysqld$port]
datadir = $datadir/$port
port = $port
socket = /tmp/mysql${port}.sock
pid-file = $datadir/$port/mysql_${port}.pid
log-error=/var/log/${port}.log
EOF

$install_dir/mysql/bin/mysqld_multi start $port
read -p "请输入你要设置的密码:" password
$install_dir/mysql/bin/mysql -uroot -p"$passwd" -S /tmp/mysql${port}.sock -e "set password=password('$password');" --connect-expired-password
     let port++
done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值