Lapple
发布于 2024-07-18 / 11 阅读
0

阿里云服务器安装配置MySQL

操作系统:CentOS 7.x、CentOS 8.x、Alibaba Cloud Linux 2、Alibaba Cloud Linux 3。

安装数据库

1.运行以下命令,更新YUM源。

sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm

2.运行以下命令,安装MySQL。

sudo yum -y install mysql-community-server --enablerepo=mysql80-community --nogpgcheck

3.运行以下命令,查看MySQL版本号。

mysql -V

配置MySQL

启动并设置开机自启动MySQL服务。

sudo systemctl start mysqldsudo systemctl enable mysqld

获取并记录root用户的初始密码。

sudo grep 'temporary password' /var/log/mysqld.log

运行结果:

 其中E>4jhoorf(j5为初始密码(复制的时候注意前面有个空格,每个人的初始密码不一样),后续在对MySQL进行安全性配置时,需要使用该初始密码。

对MySQL进行安全性配置。

sudo mysql_secure_installation

  1)重置MySQL数据库root用户的密码。 
   在输入密码时,系统为了最大限度的保证数据安全,命令行将不做任何回显。您只需要输入正确的密码信息,然后按Enter键即可。 
   Enter password for user root: #输入已获取的root用户初始密码 The existing password for the user account root has expired. Please set a new password. New password: #输入新的MySQL密码(这里只能输入强密码(类似于Abc123.)) Re-enter new password: #重复输入新的MySQL密码The 'validate_password' component is installed on the server.The subsequent steps will run with the existing configurationof the component.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选择更新MySQL密码。您也可以输入N不再更新MySQL密码。 New password: #输入新的MySQL密码 Re-enter new password: #重复输入新的MySQL密码 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确认使用已设置的密码。 
    2)删除匿名用户。 
   By default, a MySQL installation has an anonymous user,allowing anyone to log into MySQL without having to havea 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 productionenvironment. Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y #输入Y删除MySQL默认的匿名用户。Success. 
   3)禁止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) :N #输入N不禁止root远程登录。 
    4)删除test库以及对test库的访问权限。 
   By default, MySQL comes with a database named 'test' thatanyone can access. This is also intended only for testing,and should be removed before moving into a productionenvironment.  Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y #输入Y删除test库以及对test库的访问权限。 - Dropping test database...Success.  - Removing privileges on test database...Success. 
    5)重新加载授权表。 
   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 #输入Y重新加载授权表。Success. All done! 

 到此,数据库的安装与配置已完成。这时可以使用连接数据库的工具进行连接了。

部分疑难问题

1、2058代码错误

报错内容:2058(Plugin caching_sha2_password could not be loaded:)

【可能原因】Mysql新版默认使用 caching_sha2_pasword 作为 身份验证插件。而旧版是使用 mysql_native_password,当连接MySQL时报错 plugin caching_sha2_password could not be loaded 时,可换回旧版插件。

【处理办法】使用命令行进入MySQL,然后更改下身份验证插件即可

1、进入mysql

 mysql -u 用户名 -p 例如:mysql -u root -p

2、然后执行下面语句

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

(其中123456是你预设置的root密码,我的密码是:123456 )

3、然后刷新配置

flush privileges;

2、1054代码错误

当修改密码出现以下错误

ERROR 1054 (42S22): Unknown column 'password' in 'field list'

错误原因:mysql数据库下已经没有password这个字段了,password字段改成了authentication_string。

解决:

update user set authentication_string="123456" where user="root";

当然,这些操作都是通过命令行进入MySQL进行操作的