链接
链接2
CentOS7 yum方式安装MySQL5.7
下载

1、yum 安装

1.1、下载并安装MySQL官方的 Yum Repository

  1. wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
  2. yum -y install mysql57-community-release-el7-10.noarch.rpm
  3. yum -y install mysql-community-server

1.2、MySQL数据库设置

启动服务

  1. systemctl start mysqld.service
  2. systemctl status mysqld.service

此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:

  1. grep "password" /var/log/mysqld.log
  2. 2021-02-19T03:04:01.737914Z 1 [Note] A temporary password is generated for root@localhost: nQIrSNKWZ1%:

登录数据库

  1. mysql -uroot -pnQIrSNKWZ1%:

输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:

  1. 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的值有关:
image.png
MySQL完整的初始密码规则可以通过如下命令查看:

  1. mysql> SHOW VARIABLES LIKE 'validate_password%';
  2. +--------------------------------------+--------+
  3. | Variable_name | Value |
  4. +--------------------------------------+--------+
  5. | validate_password_check_user_name | OFF |
  6. | validate_password_dictionary_file | |
  7. | validate_password_length | 8 |
  8. | validate_password_mixed_case_count | 1 |
  9. | validate_password_number_count | 1 |
  10. | validate_password_policy | MEDIUM |
  11. | validate_password_special_char_count | 1 |
  12. +--------------------------------------+--------+
  13. 7 rows in set (0.00 sec)

密码的长度是由validate_password_length决定的,而validate_password_length的计算公式是:

  1. validate_password_length = validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)

可以通过如下命令修改:

  1. mysql> set global validate_password_policy=0;
  2. mysql> set global validate_password_length=1;

此时密码就可以设置的很简单,例如1234之类的。到此数据库的密码设置就完成了。
但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:

  1. [root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch


2、安装脚本

脚本和tar包需要在同一个目录下
vim mysql-server.sh

  1. #!/bin/bash
  2. offversion=`basename mysql-*.tar.gz .tar.gz | awk -F '-' '{print$2"-"$3"-"$4"-"$5}'`
  3. PACKAGE_FULL_WAY=`pwd`
  4. INSTALL_HOME=$PACKAGE_FULL_WAY/mysql
  5. log_info() {
  6. echo "[$(date -d today +"%Y-%m-%d %H:%M:%S %:::z")] $1"
  7. }
  8. # 新建mysql用户
  9. add_user() {
  10. #create group mysql
  11. grep "^mysql" /etc/group &> /dev/null
  12. if [[ $? -ne 0 ]]; then
  13. groupadd mysql
  14. fi
  15. id mysql &> /dev/null
  16. if [[ $? -ne 0 ]]; then
  17. useradd -g mysql mysql
  18. chage -M 99999 mysql
  19. fi
  20. }
  21. # 安装Mysql
  22. install_mysql() {
  23. # 安装Mysql
  24. tar -zxvf mysql-${offversion}.tar.gz
  25. mv mysql-${offversion} mysql
  26. cd ${INSTALL_HOME}
  27. # 新建数据库目录
  28. mkdir -p ${INSTALL_HOME}/data
  29. chown -R mysql:mysql ./
  30. #编辑my.cnf
  31. cat << EOF > /etc/my.cnf
  32. [mysqld]
  33. port=3306
  34. basedir=${INSTALL_HOME}
  35. datadir=${INSTALL_HOME}/data
  36. socket=/tmp/mysql.sock
  37. user=mysql
  38. tmpdir=/tmp
  39. bind-address = 0.0.0.0
  40. max_connections=1024
  41. lower_case_table_names=1
  42. character-set-server=utf8mb4
  43. collation-server=utf8mb4_general_ci
  44. default-storage-engine=INNODB
  45. innodb_buffer_pool_size=64MB
  46. max_allowed_packet=16M
  47. [mysqld_safe]
  48. log-error=${INSTALL_HOME}/data/error.log
  49. pid-file=${INSTALL_HOME}/data/mysql.pid
  50. [mysql]
  51. default-character-set=utf8mb4
  52. [client]
  53. socket=/tmp/mysql.sock
  54. default-character-set=utf8mb4
  55. !includedir /etc/my.cnf.d
  56. EOF
  57. echo '修改mysql配置文件成功';
  58. #加入环境变量,方便使用mysql命令,但是需要source /etc/profile
  59. #echo "###MYSQL_PATH_ENV_S" >>/etc/profile
  60. echo "export PATH=${INSTALL_HOME}/bin:\$PATH" >> /etc/profile
  61. #echo "###MYSQL_PATH_ENV_E" >> /etc/profile
  62. ./bin/mysqld --user=mysql --basedir=${INSTALL_HOME} --datadir=${INSTALL_HOME}/data --initialize > ${INSTALL_HOME}/out.txt 2>&1
  63. cd ${INSTALL_HOME}
  64. # 启动Mysql
  65. ${INSTALL_HOME}/support-files/mysql.server start
  66. password=$(cat $INSTALL_HOME/out.txt | grep password | awk -F " " {'print $NF'})
  67. ${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;"
  68. echo mysql init successful!!!
  69. }
  70. # 安装Mysql
  71. install() {
  72. add_user
  73. log_info "添加用户"
  74. log_info "开始安装mysql"
  75. install_mysql
  76. log_info "install_mysql finish"
  77. }
  78. # 停止Mysql
  79. stop() {
  80. ${INSTALL_HOME}/support-files/mysql.server stop
  81. }
  82. # 启动Mysql
  83. start() {
  84. ${INSTALL_HOME}/support-files/mysql.server start
  85. }
  86. usage() {
  87. echo "Usage: $PROG_NAME {start|stop|install|uninstall}"
  88. exit 2
  89. }
  90. PROG_NAME=$0
  91. ACTION=$1
  92. case "$ACTION" in
  93. start)
  94. start
  95. ;;
  96. stop)
  97. stop
  98. ;;
  99. restart)
  100. stop
  101. start
  102. ;;
  103. install)
  104. install
  105. ;;
  106. uninstall)
  107. uninstall
  108. ;;
  109. *)
  110. usage
  111. ;;
  112. esac