安装环境
CentOS 7 64位
一、下载安装包
下载地址:https://downloads.mysql.com/archives/community/
选择合适的版本下载。(本文使用:mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz)
[root@localhost ~]# ll
total 644556
-rw-------. 1 root root 1235 Jun 4 07:03 anaconda-ks.cfg
-rw-r--r--. 1 root root 660017902 Aug 4 00:30 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
二、解压并重命名文件夹
# 解压到指定目录
[root@localhost ~]# tar -zxvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local
# 进入解压后的目录
[root@localhost ~]# cd /usr/local/
# 重命名文件夹
[root@localhst local]# mv mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
三、创建 mysql 用户和组并分配权限
[root@localhost bin]# groupadd mysql
[root@localhost bin]# useradd -r -g mysql mysql
[root@localhost bin]# chown -R mysql:mysql /usr/local/mysql
[root@localhost bin]# chmod -R 755 /usr/local/mysql
四、创建data目录并初始化数据库
# 创建data目录
[root@localhost local]# mkdir /usr/local/mysql/data
# 初始化数据库(最后一行的 i.N!pqvtr2QJ 为初始密码)
[root@localhost local]# cd /usr/local/mysql/bin/
[root@localhost bin]# ./mysqld --initialize --user=mysql
2020-08-04T09:23:24.883696Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-04T09:23:26.264018Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-04T09:23:26.309459Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-04T09:23:26.379397Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 26addeba-d634-11ea-a566-000c2908e7b8.
2020-08-04T09:23:26.380456Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-08-04T09:23:26.781010Z 0 [Warning] CA certificate ca.pem is self signed.
2020-08-04T09:23:26.935923Z 1 [Note] A temporary password is generated for root@localhost: i.N!pqvtr2QJ
五、创建软链接并启动 MySQL
# 创建软链接
[root@localhost bin]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@localhost bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
# 启动 MySQL
[root@localhost bin]# service mysql start
Starting MySQL. SUCCESS!
注意:启动可能会报以下错误
Starting MySQL.2020-08-04T09:34:33.708546Z mysqld_safe error: log-error set to ‘/var/log/mariadb/mariadb.log’, however file don’t exists. Create writable for user ‘mysql’.
ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).解决方式:
[root@localhost bin]# mkdir /var/log/mariadb [root@localhost bin]# touch /var/log/mariadb/mariadb.log [root@localhost bin]# chown -R mysql:mysql /var/log/mariadb/
然后重新启动 MySQL 就可以了
六、连接 MySQL 并重置密码
# 连接数据库(密码为第四步初始化数据库时生成的密码)
[root@localhost bin]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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>
注意:可能会报以下错误
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
解决方式:
[root@localhost bin]# ln /var/lib/mysql/mysql.sock /tmp/mysql.sock
然后重新连接 MySQL
# 设置新密码
mysql> set password for root@localhost = password('123456');
# 开放远程连接
mysql> use mysql;
mysql> update user set user.Host='%' where user.User='root';
mysql> flush privileges;
开放防火墙的3306端口:
[root@localhost bin]# firewall-cmd --permanent --add-port=3306/tcp
success
[root@localhost bin]# firewall-cmd --permanent --add-port=3306/udp
success
[root@localhost bin]# firewall-cmd --reload
success