[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/bashif [ $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
sleep2
cd /usr/local
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
elseexit
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);doif [ $count -eq 1 ];thenif [ -z $datadir ];then
datadir=/opt/data
fi
if [ ! -d $datadir ];thenmkdir-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/bashif [ $UID -ne 0 ];then
echo "请使用管理员来执行此脚本."
exit
fi
functionsingle()
{
if [ -z $datadir ];then
datadir=/opt/data
fi
if [ ! -d $datadir ];thenmkdir-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
sleep2
cd /usr/local
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
elseexit
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);doif [ $count -eq 1 ];thensingle
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 01280.0.0.0:220.0.0.0:*
LISTEN 080*:3306*:*
LISTEN 0128 [::]: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 endwith;or \g.
Your MySQL connection id is4
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' toclear the current input statement.
mysql> quit
Bye
[root@SYL7 mysql]#
5. 编写多实例
[root@SYL7 mysql]# cat install.sh
#!/bin/bashif [ $UID -ne 0 ];then
echo "请使用管理员来执行此脚本."
exit
fi
port=3306functionsingle()
{
if [ -z $datadir ];then
datadir=/opt/data
fi
if [ ! -d $datadir ];thenmkdir-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
sleep2
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);doif [ $count -eq 1 ];thensingle
fi
if [ $count -gt 1 ];thenif [ -z $datadir ];then
datadir=/opt/data
fi
if [ ! -d ${datadir}/$port ];thenmkdir-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
sleep5/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/bashif [ $UID -ne 0 ];then
echo "请使用管理员来执行此脚本."
exit
fi
functionsingle()
{
if [ ! -d $datadir ];thenmkdir-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 ];thenmkdir-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
sleep5
${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=3306if [ -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
sleep2
cd /usr/local
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
elseexit
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);doif [ $count -eq 1 ];thensingle
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 01280.0.0.0:220.0.0.0:*
LISTEN 080*:3306*:*
LISTEN 080*:3307*:*
LISTEN 080*:3308*:*
LISTEN 0128 [::]: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 endwith;or \g.
Your MySQL connection id is3
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' toclear 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 endwith;or \g.
Your MySQL connection id is3
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' toclear 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 endwith;or \g.
Your MySQL connection id is3
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' toclear the current input statement.
mysql> quit
Bye
[root@node1 mysql]#