运行以下命令更新YUM源。
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
运行以下命令安装MySQL。
说明 如果您使用的操作系统内核版本为el8,可能会提示报错信息No match for argument。您需要先运行命令yum module disable mysql禁用默认的MySQL模块,再安装MySQL。
yum -y install mysql-community-server --nogpgcheck
运行以下命令查看MySQL版本号。
mysql -V
返回结果如下所示,表示MySQL安装成功。
mysql Ver 14.14 Distrib 5.7.31, for Linux (x86_64) using EditLine wrapper
运行以下命令启动MySQL。
systemctl start mysqld
依次运行以下命令设置开机启动MySQL。
systemctl enable mysqld systemctl daemon-reload
运行以下命令查看MySQL的初始密码。
grep "password" /var/log/mysqld.log
返回结果示例如下,本示例中初始密码为+47,uijcojcU
。
2020-08-28T03:01:49.848762Z 1 [Note] A temporary password is generated for root@localhost: +47,uijcojcU
运行以下命令配置MySQL的安全性。
mysql_secure_installation
安全性的配置包含以下五个方面:
重置root账号的密码。
说明 请您安全保管root账号的密码信息。
Enter password for user root: #输入上一步获取的root用户初始密码。The existing password for the user account root has expired. Please set a new password.New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/Re-enter new password: #重复输入新密码。The 'validate_password' plugin is installed on the server.The subsequent steps will run with the existing configurationof the plugin.Using existing password for root.Estimated strength of the password: 100Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #按Y,并再次输入上步已设置的密码。New password: #再次输入新密码。Re-enter new password: #重复输入新密码。Estimated strength of the password: 100Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #按Y使用新密码。
输入Y删除匿名用户账号。
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only fortesting, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y Success.
输入Y禁止root账号远程登录。
Normally, root should only be allowed to connect from'localhost'. This ensures that someone cannot guess atthe root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y Success.
输入Y删除test库以及对test库的访问权限。
By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing,and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y - Dropping test database... Success. - Removing privileges on test database... Success.
输入Y重新加载授权表。
Reloading the privilege tables will ensure that all changesmade so far will take effect immediately.Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入YSuccess.All done!
使用:mysql -uroot -p进行登录,进入到数据库后输入下面命令行, GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的登录密码' WITH GRANT OPTION; 然后刷新就可以访问了 如果没有反应可以使用flush privileges 命令进行刷新 刷新权限是指在MySQL中用来刷新权限的命令,主要用于重新加载用户权限。该命令格式如下: FLUSH PRIVILEGES; 安装完之后:使用navicat进行登录,如果访问不了报以下错误:1130,"Host 'xx' is not allowed to connect to this MySQL server"