ot -p"$password" --connect-expired-password -e "set password = password('$passwd');"
echo "mysql安装成功,数据库的密码是: $passwd"
fi
done
}
#多实例
function multi(){
init
init2
port=3306
#配置配置文件/etc/my.cnf
cat > /etc/my.cnf <<EOF
[mysqld_multi]
mysqld = ${mysql_install_dir}/bin/mysqld_safe
mysqladmin = ${mysql_install_dir}/bin/mysqladmin
EOF
#创建各实例数据存放的目录并初始化各实例
for i in $(seq $count);do
mkdir -p ${datadir}/$port
chown -R mysql.mysql ${datadir}
content=$(ls -l ${datadir}/$port | grep -v total | wc -l)
if [ $content -eq 0 ];then
echo "正在初始化各实例"
${mysql_install_dir}/bin/mysqld --initialize --user=mysql --datadir=${datadir}/$port &> /tmp/passwd
#取出临时数据库密码
password=$(grep 'password' /tmp/passwd |awk '{print $NF}')
else
let port++
continue
fi
#配置配置文件/etc/my.cnf
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
#启动各实例并修改数据库密码
ln -s ${mysql_install_dir}/bin/my_print_defaults /usr/bin
${mysql_install_dir}/bin/mysqld_multi start ${port}
sleep 6
ln -s ${mysql_install_dir}/bin/mysql /usr/bin
mysql -uroot -p"$password" -h127.0.0.1 -P${port} --connect-expired-password -e "set password = password('$passwd');"
let port++
done
echo "mysql安装成功,数据库的密码是: $passwd"
}
read -p "请输入要创建的实例个数: " count
read -p "请输入安装目录,(默认路径:/usr/local/mysql):" mysql_install_dir
read -p "请输入数据存放目录(默认路径: /opt/xbz): " datadir
read -p "请输入要为数据库设置的密码(默认密码123456): " passwd
#判断安装目录合法性
echo $mysql_install_dir | grep -E '^/[a-z][a-z]*(/[a-z][a-z]*)*$' &> /dev/null
if [ $? -eq 0 ];then
if [ ! -d $mysql_install_dir ];then
mkdir -p $mysql_install_dir
fi
else
mysql_install_dir=/usr/local/mysql
fi
#判断安装目录是否为空
if [ -z $mysql_install_dir ];then
mysql_install_dir=/usr/local/mysql
fi
#判断数据存放目录是否为空
if [ -z $datadir ];then
datadir=/opt/xbz
fi
#判断数据存放目录是否合法
echo $datadir | grep "^/[a-z][a-z]*\(/[a-z][a-z]*\)*$" &> /dev/null
if [ $? -ne 0 ];then
datadir=/opt/xbz
fi
#设置数据库密码格式
if [ -z $passwd ];then
passwd=123456
else
echo $passwd | grep -E '[a-z]+' | grep -E '[A-Z]+' | grep -E '[0-9]+' | grep -E '_+' &> /dev/null
if [ $? -ne 0 ] || [ ${#passwd} -lt 8 ];then
passwd=123456
fi
fi
#创建用户
if [ $count -eq 1 ];then
single
ss -antl
else
multi
ss -antl
fi
验证:
[root@localhost mysql]# mysql -uroot -p'123456' -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> exit
Bye
[root@localhost mysql]# mysql -uroot -p'123456' -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)
Copy |