1. 官网下载
2. 将下载好的文件上传至服务器
/usr/local/tools
tar -xvf mysql-8.0.21-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.21-linux-glibc2.12-x86_64 mysql-8.0.21
3. 添加系统用户组和用户
groupadd mysql useradd -r -g mysql mysql
进入文件夹: cd mysql-8.0.21
此时当前目录为: /usr/local/tools/mysql-8.0.21
4. 新建数据目录个临时目录
mkdir data tmp chmod 777 ./data ./tmp
新建并修改配置文件:
cd support-files touch my-default.cnf vim my-default.cnf
将以下内容写入文件,其中lower_case_table_names=1 表示忽略大小写敏感
[mysqld]
basedir = /usr/local/tools/mysql-8.0.21
datadir = /usr/local/tools/mysql-8.0.21/data
port = 3306
socket = /usr/local/tools/mysql-8.0.21/tmp/mysql.sock
lower_case_table_names=1
[client]
default-character-set=utf8
socket= /usr/local/tools/mysql-8.0.21/tmp/mysql.sock
[mysql]
default-character-set=utf8
socket= /usr/local/tools/mysql-8.0.21/tmp/mysql.sock
5. 修改当前目录为mysql用户
chown -R mysql:mysql ./
初始化Mysql配置表: 切换至目录:/usr/local/tools/mysql-8.0.21
bin/mysqld --initialize --user=mysql --basedir=/usr/local/tools/mysql-8.0.21 --datadir=/usr/local/tools/mysql-8.0.21/data
执行结束会生成数据库root用户密码:
2020-07-20T10:02:45.666654Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.21) initializing of server in progress as process 40212
2020-07-20T10:02:47.687058Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: VRie4S*c%TNq
如果执行失败,需要删除data 和tmp文件下所有内容,检查配置后重新执行初始化
6. 设置开机启动服务
进入support-files目录
cp mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig --list mysql (检查是否启动成功)
Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'.
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
以上说明成功
7. 手动启动服务
vim /etc/profile (配置环境变量)
export PATH=$PATH:/usr/local/tools/mysql-8.0.21/bin:/usr/local/tools/mysql-8.0.21/lib
service mysql start (start,stop,restart命令)
source /etc/profile
8. 登录Mysql,重置密码
mysql -uroot -pVRie4S*c%TNq (密码是上面生成的密码,特殊符号可能报错,重新执行的初始化)
alter user 'root'@'localhost' identified by 'root';(重置为root)
flush privileges;
9. 放行远程链接
确保防火墙关闭,或者端口放行
mysql->use mysql;
mysql->update user set host = '%' where user ='root';
mysql->flush privileges;
mysql->select 'host' from user where user='root';
10. MySQL8.0登录提示caching_sha2_password问题解决方法
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host | User | plugin |
+-----------+------------------+-----------------------+
| % | root | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host | User | plugin |
+-----------+------------------+-----------------------+
| % | root | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)