MySQL二进制包自行百度,晚上很多查找办法
#!/bin/bash
#二进制安装mysql并初始化密码为123456
mysql_name=mysql-5.7.31-linux-glibc2.12-x86_64
id mysql 2>&1 /dev/null
if [ $? -eq 0 ];then
echo "mysql用户存在"
else
useradd -r -s /sbin/nologin mysql
fi
cd /root/
if [ -f $mysql_name.tar.gz ];then
echo "二进制包不存在,请检查mysql二进制包"
exit 1
else
echo "二进制包存在,继续下一步"
fi
tar -xf $mysql_name.tar.gz
cp -a $mysql_name /usr/local/mysql
mkdir -p /usr/local/mysql/data
touch /usr/local/mysql/mysql_error.log
touch /usr/local/mysql/mysql.pid
chown -R mysql:mysql /usr/local/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
cat << eof >/etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user = mysql
tmpdir = /tmp
symbolic-links=0
port=3306
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
default_password_lifetime=0
explicit_defaults_for_timestamp=on
[mysqld_safe]
log-error=/usr/local/mysql/mysql_error.log
pid-file=/usr/local/mysql/mysql.pid
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
eof
if [ ! -f /root/initialize.log ];then
/usr/local/mysql/bin/mysqld --initialize --user=mysql > /root/initialize.log 2>&1
else
echo "已经初始化完成,继续下一步"
cat /root/initialize.log | grep 'root@localhost:' | awk -F ' ' '{print $11}' | sed 's/^[\t ]*//g' > /root/mysql_passwd
cat << EOF >> /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
EOF
source /etc/profile
netstat -an | grep 3306 2>&1 > /dev/null
if [ $? -eq 0 ]
echo "mysql已经启动"
else
/etc/init.d/mysqld
mysqladmin -uroot -p`cat /root/mysql_passwd` password "123456"
mysql -uroot -p123456 -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' identified by 'root_123456' WITH GRANT OPTION; "
mysql -uroot -p123456 -e "FLUSH PRIVILEGES"