Ubuntu中安装配置MySQL8并实现远程连接(以Ubuntu22.04为例)

1.安装MySql

更新列表

sudo apt-get update

安装MySQL

sudo apt-get install mysql-server

2.Mysql安全配置

sudo mysql_secure_installation

会输出以下内容

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No:

意为询问是否使用密码验证组件。输入 “Y” 并按下回车键。

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:

设置密码验证策略的级别。他提供了 3 个级别:

LOW: 密码长度至少 8 位
MEDIUM: 密码长度至少 8 位, 由数字、混合大小写的字母和特殊字符组成
STRONG: 密码长度至少 8 位, 由数字、混合大小写的字母、特殊字符和字典文件组成

请选择适合你的密码级别。我选择 0。

Please set the password for root here.

New password:

Re-enter new password:

在这里,输入两次密码

Estimated strength of the password: 25
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : 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 for
testing, 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
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

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
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!

在这里,上面所有问题都输入 Y 。然后完成整个配置过程。

3.启动MySQL

sudo service mysql start

重启和关闭服务的命令:

#重启:
sudo  service mysql restart 

#关闭: 
sudo service mysql stop

4.查看MySQL状态

systemctl status mysql

在这里插入图片描述

5.登录MySQL

 mysql -u root -p

输入密码进入mysql
在这里插入图片描述

6.修改MySQL密码

ALTER USER 'root'@'%' identified with mysql_native_password BY '你的密码';

修改之后要刷新权限

flush privileges;

7.开启远程连接

把访问权限从root改成%,代表所有用户均可访问

update user set host='%' where user='root';

记得刷新权限

flush privileges;

可以查看MySQL用户和权限表:

select host,user,plugin from mysql.user;

%代表对应的用户有远程访问权限
在这里插入图片描述

8.服务器开放3306端口(如有)

在这里插入图片描述

9.测试连接

此时,使用navicat进行远程连接,发现仍然连接失败

10.修改配置文件解决无法远程连接问题

sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

把文件中对应如下位置的两行ip地址改为0.0.0.0,或者直接注释掉
在这里插入图片描述

11.重启mysql

sudo  service mysql restart

12.再次Navicat测试连接

成功
在这里插入图片描述

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值