使用脚本二进制安装MySQL

使用脚本二进制安装MySQL

1. 提供软件包

[root@SYL7 mysql]# ls files/
mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@SYL7 mysql]# 

2. 编写单实例脚本

[root@SYL7 mysql]# ls
files  install.sh
[root@SYL7 mysql]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "请使用管理员来执行此脚本."
    exit
fi

read -p "请输入要创建的实例个数: " count
read -p "请输入数据存放目录的绝对路径(default: /opt/data): " datadir
read -p "请输入要为数据库设置的密码: " passwd

#创建mysql用户
id mysql &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin mysql
fi

#安装软件
yum -y install ncurses-compat-libs

#解压软件包,并修改目录及其属主
if [ ! -d /usr/local/mysql ];then
    echo "Decompressing software package..."
    tar xf files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local
    sleep 2
    cd /usr/local
    mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
else
    exit
fi
chown -R mysql.mysql /usr/local/mysql

#设置环境变量
echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

#头文件
#ln -s /usr/local/mysql/include /usr/include/mysql

#man文档
grep '/usr/local/mysql/man' /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t/usr/local/mysql/man" /etc/man_db.conf
fi

#lib
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig

for i in $(seq $count);do
    if [ $count -eq 1 ];then
        if [ -z $datadir ];then
            datadir=/opt/data
        fi
        if [ ! -d $datadir ];then
            mkdir -p $datadir
        fi
        chown -R mysql.mysql $datadir
        echo "init mysql ......"
        /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/mysqlpasswd
        cat > /etc/my.cnf << EOF
[mysqld] 
basedir = /usr/local/mysql
datadir = $datadir
socket = /tmp/mysql.sock 
port = 3306 
pid-file = $datadir/mysql.pid 
user = mysql 
skip-name-resolve 
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF
        if [ ! -f /etc/init.d/mysqld ];then
            cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
            sed -i '/^basedir=/c basedir=/usr/local/mysql' /etc/init.d/mysqld
            sed -i "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
        fi
        chmod +x /etc/init.d/mysqld
        service mysqld start
        chkconfig --add mysqld
        ss -antl
        password=$(grep 'password' /tmp/mysqlpasswd |awk '{print $NF}')
        /usr/local/mysql/bin/mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
        echo "安装成功。您的密码为: $passwd"
    fi
done
[root@SYL7 mysql]# 


chkconfig --add mysqld mysql开机自启

3. 使用函数编写脚本

[root@SYL7 mysql]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "请使用管理员来执行此脚本."
    exit
fi

function single()
{
    if [ -z $datadir ];then
        datadir=/opt/data
    fi
    if [ ! -d $datadir ];then
        mkdir -p $datadir
    fi
    chown -R mysql.mysql $datadir
    echo "init mysql ......"
    /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/mysqlpasswd
    cat > /etc/my.cnf <<EOF
[mysqld] 
basedir = /usr/local/mysql
datadir = $datadir
socket = /tmp/mysql.sock 
port = 3306 
pid-file = $datadir/mysql.pid 
user = mysql 
skip-name-resolve 
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF
    if [ ! -f /etc/init.d/mysqld ];then
        cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
        sed -i '/^basedir=/c basedir=/usr/local/mysql' /etc/init.d/mysqld
        sed -i "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
    fi
    chmod +x /etc/init.d/mysqld
    service mysqld start
    chkconfig --add mysqld
    ss -antl
    password=$(grep 'password' /tmp/mysqlpasswd |awk '{print $NF}')
    /usr/local/mysql/bin/mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
    echo "安装成功。您的密码为: $passwd"
}

read -p "请输入要创建的实例个数: " count
read -p "请输入数据存放目录的绝对路径(default: /opt/data): " datadir
read -p "请输入要为数据库设置的密码: " passwd

#创建mysql用户
id mysql &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin mysql
fi

#安装软件
yum -y install ncurses-compat-libs

#解压软件包,并修改目录及其属主
if [ ! -d /usr/local/mysql ];then
    echo "Decompressing software package..."
    tar xf files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local
    sleep 2
    cd /usr/local
    mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
else
    exit
fi
chown -R mysql.mysql /usr/local/mysql

#设置环境变量
echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

#头文件
ln -s /usr/local/mysql/include /usr/include/mysql &> /dev/null

#man文档
grep '/usr/local/mysql/man' /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t/usr/local/mysql/man" /etc/man_db.conf
fi

#lib
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig

for i in $(seq $count);do
    if [ $count -eq 1 ];then
        single
    fi
done
[root@SYL7 mysql]# 

4. 验证单实例

[root@SYL7 mysql]# ls /opt/data/
auto.cnf         ib_buffer_pool  mysql               public_key.pem
ca-key.pem       ib_logfile0     mysql.pid           server-cert.pem
ca.pem           ib_logfile1     node1.err           server-key.pem
client-cert.pem  ibdata1         performance_schema  sys
client-key.pem   ibtmp1          private_key.pem
[root@SYL7 mysql]# ss -antl
State  Recv-Q Send-Q   Local Address:Port   Peer Address:Port Process 
LISTEN 0      128            0.0.0.0:22          0.0.0.0:*            
LISTEN 0      80                   *:3306              *:*            
LISTEN 0      128               [::]:22             [::]:*            
[root@SYL7 mysql]# mysql -uroot -p123456
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 4
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> quit
Bye
[root@SYL7 mysql]# 

5. 编写多实例

[root@SYL7 mysql]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "请使用管理员来执行此脚本."
    exit
fi

port=3306

function single()
{
    if [ -z $datadir ];then
        datadir=/opt/data
    fi
    if [ ! -d $datadir ];then
        mkdir -p $datadir
    fi
    chown -R mysql.mysql $datadir
    echo "init mysql ......"
    /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/mysqlpasswd
    cat > /etc/my.cnf <<EOF
[mysqld] 
basedir = /usr/local/mysql
datadir = $datadir
socket = /tmp/mysql.sock 
port = 3306 
pid-file = $datadir/mysql.pid 
user = mysql 
skip-name-resolve 
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF
    if [ ! -f /etc/init.d/mysqld ];then
        cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
        sed -i '/^basedir=/c basedir=/usr/local/mysql' /etc/init.d/mysqld
        sed -i "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
    fi
    chmod +x /etc/init.d/mysqld
    service mysqld start
    chkconfig --add mysqld
    ss -antl
    password=$(grep 'password' /tmp/mysqlpasswd |awk '{print $NF}')
    /usr/local/mysql/bin/mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
    echo "安装成功。您的密码为: $passwd"
}

read -p "请输入要创建的实例个数: " count
read -p "请输入数据存放目录的绝对路径(default: /opt/data): " datadir
read -p "请输入要为数据库设置的密码: " passwd

if [ -z $passwd ];then
    passwd=123456
fi      

#创建mysql用户
id mysql &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin mysql
fi

#安装软件
yum -y install ncurses-compat-libs perl

#解压软件包,并修改目录及其属主
if [ ! -d /usr/local/mysql ];then
    echo "Decompressing software package..."
    tar xf files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local
    sleep 2
    cd /usr/local
    mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
#else
#    exit
fi
chown -R mysql.mysql /usr/local/mysql

#设置环境变量
echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

#头文件
ln -s /usr/local/mysql/include /usr/include/mysql &> /dev/null

#man文档
grep '/usr/local/mysql/man' /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t/usr/local/mysql/man" /etc/man_db.conf
fi

#lib
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig

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

for i in $(seq $count);do
    if [ $count -eq 1 ];then
        single
    fi
    if [ $count -gt 1 ];then
        if [ -z $datadir ];then
            datadir=/opt/data
        fi
        if [ ! -d ${datadir}/$port ];then
            mkdir -p ${datadir}/$port
        fi
        chown -R mysql.mysql ${datadir}/$port
        /usr/local/mysql/bin/mysqld --initialize-insecure --user mysql --datadir ${datadir}/$port 
        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
        /usr/local/mysql/bin/mysqld_multi start $port
        sleep 5
        /usr/local/mysql/bin/mysql -uroot -e "set password = password('$passwd');" -S /tmp/mysql${port}.sock
        echo "安装成功。您的密码为: $passwd"
        ss -antl
        let port++
    fi
done
[root@SYL7 mysql]# 

6. 使用函数编写脚本,优化

[root@SYL7 mysql]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "请使用管理员来执行此脚本."
    exit
fi

function single()
{
    if [ ! -d $datadir ];then
        mkdir -p $datadir
    fi
    chown -R mysql.mysql $datadir
    echo "init mysql ......"
    ${install_dir}/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/mysqlpasswd
    cat > /etc/my.cnf <<EOF
[mysqld] 
basedir = $install_dir
datadir = $datadir
socket = /tmp/mysql.sock 
port = 3306 
pid-file = $datadir/mysql.pid 
user = mysql 
skip-name-resolve 
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF
    if [ ! -f /etc/init.d/mysqld ];then
        cp ${install_dir}/support-files/mysql.server /etc/init.d/mysqld
        sed -i "/^basedir=/c basedir=$install_dir" /etc/init.d/mysqld
        sed -i "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
    fi
    chmod +x /etc/init.d/mysqld
    service mysqld start
    chkconfig --add mysqld
    ss -antl
    password=$(grep 'password' /tmp/mysqlpasswd |awk '{print $NF}')
    ${install_dir}/bin/mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
    echo "安装成功。您的密码为: $passwd"
}

function multi()
{
    if [ ! -d ${datadir}/$port ];then
        mkdir -p ${datadir}/$port
    fi
    chown -R mysql.mysql ${datadir}/$port
    ${install_dir}/bin/mysqld --initialize-insecure --user mysql --datadir ${datadir}/$port 
    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}/bin/mysqld_multi start $port
    sleep 5
    ${install_dir}/bin/mysql -uroot -e "set password = password('$passwd');" -S /tmp/mysql${port}.sock
    echo "安装成功。您的密码为: $passwd"
    ss -antl
    let port++
}

read -p "请输入要创建的实例个数: " count
read -p "请输入要将数据库安装目录的绝对路径(default: /usr/local/mysql): " install_dir
read -p "请输入数据存放目录的绝对路径(default: /opt/data): " datadir
read -p "请输入要为数据库设置的密码: " passwd


port=3306

if [ -z $datadir ];then
    install_dir=/usr/local/mysql
fi

if [ -z $datadir ];then
    datadir=/opt/data
fi

if [ -z $passwd ];then
    passwd=123456
fi

#创建mysql用户
id mysql &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin mysql
fi

#安装软件
yum -y install ncurses-compat-libs perl

#解压软件包,并修改目录及其属主
if [ ! -d $install_dir ];then
    echo "Decompressing software package..."
    tar xf files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local 
    sleep 2
    cd /usr/local
    mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
else
    exit
fi
chown -R mysql.mysql $install_dir

#设置环境变量
echo "export PATH=$install_dir/bin:\$PATH" > /etc/profile.d/mysql.sh

#头文件
ln -s $install_dir/include /usr/include/mysql &> /dev/null

#man文档
grep "$install_dir/man" /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t$install_dir/man" /etc/man_db.conf
fi

#lib
echo "$install_dir/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig

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

for i in $(seq $count);do
    if [ $count -eq 1 ];then
        single
    fi
    if [ $count -gt 1 ];then
        multi
    fi
done
[root@SYL7 mysql]# 

7. 验证多实例

[root@node1 mysql]# ss -antl
State  Recv-Q Send-Q   Local Address:Port   Peer Address:Port Process 
LISTEN 0      128            0.0.0.0:22          0.0.0.0:*            
LISTEN 0      80                   *:3306              *:*            
LISTEN 0      80                   *:3307              *:*            
LISTEN 0      80                   *:3308              *:*            
LISTEN 0      128               [::]:22             [::]:*            
[root@node1 mysql]# 
[root@node1 mysql]# mysql -uroot -p123456 -h127.0.0.1 -P3306
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 3
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> quit
Bye
[root@node1 mysql]# mysql -uroot -p123456 -h127.0.0.1 -P3307
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 3
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> quit
Bye
[root@node1 mysql]# mysql -uroot -p123456 -h127.0.0.1 -P3308
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 3
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> quit
Bye
[root@node1 mysql]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值