在基于RPM的系统(如CentOS、Red Hat Enterprise Linux或Fedora)上安装MySQL 8,您可以遵循以下步骤:
- 下载MySQL的官方Yum Repository:
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
- 添加MySQL Yum Repository到您的系统:
sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
- 安装MySQL服务器:
sudo yum install mysql-community-server
- 启动MySQL服务:
sudo systemctl start mysqld systemctl enable mysqld
- 查找临时生成的root密码:
sudo grep 'temporary password' /var/log/mysqld.log
2024-08-02T10:12:51.861872Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: s7#nMPui*i-M
- 安全配置MySQL(包括设置root密码等):输入密码,y-->y-->空-->y
sudo mysql_secure_installation
- 启动MySQL服务,并使其在系统启动时自动启动:
sudo systemctl enable mysqldsudo systemctl start mysqld
请确保您的系统是最新的,并且已经安装了wget和yum。如果需要其他版本的MySQL或不同的操作系统,请从MySQL官方网站下载相应的Yum Repository RPM包。
设置允许远程访问
[root@localhost soft]# mysql -uroot -pEnter 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> update user set host='%' where user='root';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
通过数据库中user 表,可以发现,默认只有一行,host 部分,值为localhost.
mysql> select host, user, authentication_string, plugin from user where user='root';
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
localhost | root | *C135C66BA4322B0E07770F6B6F3AED44106C7F6E | mysql_native_password |
如果这个时候,远程使用ROOT登陆,会报如下的错误:
[root@host01 ~]# mysql -uroot -p -h192.168.133.201
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'host01.example.com' (using password: YES)
这个时候,我们可以直接修改USER表
mysql> update user set host='%' where user='root';
不要忘了,把数据库中修改 刷新 到内存中,或者,重启数据库
mysql> flush privileges;
,之后,就可以通过远程登陆了。