MYSQL一键安装的脚本

系统环境:CENTOS7

MYSQL:mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

MYSQL的安装包的路径:/shared/app/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

[root@v75 shell]# cat installmysql.sh 
#!/bin/bash
egrep "^mysql" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    groupadd mysql
fi

egrep "^mysql" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    useradd -r -g mysql -s /bin/false mysql
fi

if [ ! -d /usr/local/mysql ];then
  mkdir -p /usr/local/mysql/var
else
  mv /usr/local/mysql /usr/local/mysqlold
  mkdir -p /usr/local/mysql/var
fi

tar -zxvf /shared/app/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql/
mv /usr/local/mysql/mysql-5.7.22-linux-glibc2.12-x86_64/* /usr/local/mysql/
cd /usr/local/
chown -R mysql:mysql mysql
chmod 750 mysql
cd mysql

if [ -f /etc/my.cnf ];
then 
mv /etc/my.cnf /etc/my.cnf.bak
fi
cat << EOF >> /etc/my.cnf
[mysqld]

character_set_server=utf8

init_connect='SET NAMES utf8'

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock


log-error=/usr/local/mysql/var/mysqld.log

pid-file=/usr/local/mysql/var/mysqld.pid
EOF

bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US

cd /usr/local/

chown -R mysql:mysql mysql

if [ -f /etc/init.d/mysqld ];
then
mv /etc/init.d/mysqld /etc/init.d/mysqld.bak
fi

cp mysql/support-files/mysql.server /etc/init.d/mysqld

echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
echo "export PATH" >> /etc/profile
source /etc/profile
str=`cat /usr/local/mysql/var/mysqld.log | grep "A temporary password is generated for"`
pwd=${str:0-12}
echo "mysql init pwd is "$pwd

执行脚本:

[root@v75 shell]# sh installmysql.sh 

注意脚本最后一行打印的是mysql的初始密码12位,保存起来或者更改

可直接运行启动命令:

[root@v75 shell]# /etc/init.d/mysqld start
Starting MySQL.[  确定  ]
[root@v75 shell]# mysql -uroot -p
Enter password: 

改密码:mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

 

安装了如果有报错

RROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

可以修改/etc/my.cnf

改掉socket这一行为

socket=/var/lib/mysql/mysql.sock

如果有密码过期不能登陆的提示

[root@v72 75shell]# mysql -uroot -p           
Enter password: 
ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.

同样修改/etc/my.cnf,加入

skip-grant-tables

重启服务

mysql -u root -p  登陆,把密码改回来,再把skip-grant-tables注释掉重启服务正常

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页