链接
链接2
CentOS7 yum方式安装MySQL5.7
下载
1、yum 安装
1.1、下载并安装MySQL官方的 Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
1.2、MySQL数据库设置
启动服务
systemctl start mysqld.service
systemctl status mysqld.service
此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:
grep "password" /var/log/mysqld.log
2021-02-19T03:04:01.737914Z 1 [Note] A temporary password is generated for root@localhost: nQIrSNKWZ1%:
登录数据库
mysql -uroot -pnQIrSNKWZ1%:
输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql@123';
这里有个问题,新密码设置的时候如果设置的过于简单会报错:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
原因是因为MySQL有密码设置的规范,具体是与validate_password_policy的值有关:
MySQL完整的初始密码规则可以通过如下命令查看:
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
密码的长度是由validate_password_length决定的,而validate_password_length的计算公式是:
validate_password_length = validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)
可以通过如下命令修改:
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
此时密码就可以设置的很简单,例如1234之类的。到此数据库的密码设置就完成了。
但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:
[root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch
2、安装脚本
脚本和tar包需要在同一个目录下vim mysql-server.sh
#!/bin/bash
offversion=`basename mysql-*.tar.gz .tar.gz | awk -F '-' '{print$2"-"$3"-"$4"-"$5}'`
PACKAGE_FULL_WAY=`pwd`
INSTALL_HOME=$PACKAGE_FULL_WAY/mysql
log_info() {
echo "[$(date -d today +"%Y-%m-%d %H:%M:%S %:::z")] $1"
}
# 新建mysql用户
add_user() {
#create group mysql
grep "^mysql" /etc/group &> /dev/null
if [[ $? -ne 0 ]]; then
groupadd mysql
fi
id mysql &> /dev/null
if [[ $? -ne 0 ]]; then
useradd -g mysql mysql
chage -M 99999 mysql
fi
}
# 安装Mysql
install_mysql() {
# 安装Mysql
tar -zxvf mysql-${offversion}.tar.gz
mv mysql-${offversion} mysql
cd ${INSTALL_HOME}
# 新建数据库目录
mkdir -p ${INSTALL_HOME}/data
chown -R mysql:mysql ./
#编辑my.cnf
cat << EOF > /etc/my.cnf
[mysqld]
port=3306
basedir=${INSTALL_HOME}
datadir=${INSTALL_HOME}/data
socket=/tmp/mysql.sock
user=mysql
tmpdir=/tmp
bind-address = 0.0.0.0
max_connections=1024
lower_case_table_names=1
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
default-storage-engine=INNODB
innodb_buffer_pool_size=64MB
max_allowed_packet=16M
[mysqld_safe]
log-error=${INSTALL_HOME}/data/error.log
pid-file=${INSTALL_HOME}/data/mysql.pid
[mysql]
default-character-set=utf8mb4
[client]
socket=/tmp/mysql.sock
default-character-set=utf8mb4
!includedir /etc/my.cnf.d
EOF
echo '修改mysql配置文件成功';
#加入环境变量,方便使用mysql命令,但是需要source /etc/profile
#echo "###MYSQL_PATH_ENV_S" >>/etc/profile
echo "export PATH=${INSTALL_HOME}/bin:\$PATH" >> /etc/profile
#echo "###MYSQL_PATH_ENV_E" >> /etc/profile
./bin/mysqld --user=mysql --basedir=${INSTALL_HOME} --datadir=${INSTALL_HOME}/data --initialize > ${INSTALL_HOME}/out.txt 2>&1
cd ${INSTALL_HOME}
# 启动Mysql
${INSTALL_HOME}/support-files/mysql.server start
password=$(cat $INSTALL_HOME/out.txt | grep password | awk -F " " {'print $NF'})
${INSTALL_HOME}/bin/mysql -uroot -p$password --connect-expired-password -e "set password=password('123456');use mysql;update user set user.Host='%' where user.User='root';flush privileges;"
echo mysql init successful!!!
}
# 安装Mysql
install() {
add_user
log_info "添加用户"
log_info "开始安装mysql"
install_mysql
log_info "install_mysql finish"
}
# 停止Mysql
stop() {
${INSTALL_HOME}/support-files/mysql.server stop
}
# 启动Mysql
start() {
${INSTALL_HOME}/support-files/mysql.server start
}
usage() {
echo "Usage: $PROG_NAME {start|stop|install|uninstall}"
exit 2
}
PROG_NAME=$0
ACTION=$1
case "$ACTION" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
install)
install
;;
uninstall)
uninstall
;;
*)
usage
;;
esac