localhost mysql]# mysql -uroot -p'lnh@321'
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]# cat mysql_install.sh
#!/bin/bash
#设置执行权限
if [ $UID -ne 0 ];then
echo "请以管理员用户进行执行"
exit
fi
function init(){
id mysql &> /dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
else
echo "用户已存在"
fi
#安装依赖包
dnf -y install ncurses-compat-libs --allowerasing
#解压软件包,修改目录和所属组
if [ ! -d $mysql_install_dir ];then
echo "解压软件包"
tar xf files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
else
echo 'mysql已安装,不需要重复安装'
exit
fi
chown -R mysql.mysql ${mysql_install_dir}
#设置环境变量
echo 'export PATH=${mysql_install_dir}/lib' > /etc/ld.so.conf.d/mysql.conf
#做头文件
ln -s ${mysql_install_dir}/include /usr/include/mysql
#配置lib
echo '${mysql_install_dir}/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
#设置man文档
grep '${mysql_install_dir}/man' /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
sed -i "22a MANDATORY_MANPATH ${mysql_install_dir}/man" /etc/man_db.conf
fi
}
function init2(){
if [ -z $datadir ];then
datadir=/opt/xbz
fi
if [ ! -d $datadir ];then
mkdir -p $datadir
fi
chown -R mysql.mysql $datadir
}
function single(){
init
#建立数据存放目录
for i in $(seq $count);do
if [ $count -eq 1 ];then
init2
#判断是否已格式化
content=$(ls -l $datadir | grep -v total | wc -l)
if [ $content -eq 0 ];then
${mysql_install_dir}/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/passwd
else
echo '不需要重复初始化'
exit
fi
#生成数据配置文件
cat > /etc/my.cnf << EOF
[mysqld]
basedir = ${mysql_install_dir}
datadir = $datadir
socket = /tmp/mysql.sock
port = 3306
pid-file = $datadir/mysql.pid
user = mysql
skip-name-resolve
EOF
#配置服务启动脚本
if [ ! -f /etc/init.d/mysqld ];then
cp -a ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
sed -ri '/^basedir=/c basedir=${mysql_install_dir}' /etc/init.d/mysqld
sed -ri "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
fi
chmod +x /etc/init.d/mysqld
#启动mysql并设置开机自启
service mysqld start
sleep 6
chkconfig --add mysqld
ln -s ${mysql_install_dir}/bin/mysql /usr/bin
password=$(grep 'password' /tmp/passwd |awk '{print $NF}')
mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
echo "mysql安装成功,数据库的密码是: $passwd"
fi
done
}
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_instal