Ubuntu18.04 云服务器 配置远程登陆Mysql数据库

环境信息:
OS:Ubuntu18.04
MySQL: 5.7.30-0ubuntu0.18.04.1

1.安装Mysql

Ubuntu中,默认情况下,只有最新版本的MySQL包含在APT软件包存储库中,要安装它,只需更新服务器上的包索引并安装默认包apt-get

#命令1
sudo apt-get update
#命令2
sudo apt-get install mysql-server

在这里插入图片描述

2.配置MySQL

2.1 初始化配置

sudo mysql_secure_installation

配置项较多,如下所示:

#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)

#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)

#3
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...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4
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 (我的选项)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#6
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 (我的选项)

实际shell:

root@ecs-sn3-medium-2-linux-20191104202244:/home/zsw/program# sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.
#1111111111
VALIDATE PASSWORD PLUGIN 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 plugin?

Press y|Y for Yes, any other key for No: N  #一定要选否,这里是要求你配置密码强度的


#2222222222
Please set the password for root here.
New password:#输入密码
Re-enter new password:#重复输入密码
#这个问题如果你第一个选了N,应该不会出现
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

#3333333333
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) : N #不删除匿名账户

 ... skipping.

#4444444444
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 #不允许远程登陆root
Success.

#5555555555
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) : N#不删除测试数据库

 ... skipping.
 
#6666666666
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!

2.2检查mysql服务状态:

systemctl status mysql.service

显示如下结果说明mysql服务是正常的:

在这里插入图片描述

3.配置远程访问

在Ubuntu下MySQL缺省是只允许本地访问的,使用workbench连接工具是连不上的;
如果你要其他机器也能够访问的话,那么需要改变/etc/mysql/my.cnf配置文件;

3.1首先用根用户进入

开始时没有密码,直接回车就行。

sudo mysql -u root -p

在这里插入图片描述

修改root密码:

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

在这里插入图片描述

其中root@localhosroot是用户localhost是本地访问,配置成%就是所有主机都可连接,可以配置成具体ip地址;

如果设置密码过程中出现了下图的错误,说明开启了密码强度限制,点击这里查看如何解决
在这里插入图片描述

3.2新建数据库和用户

用root用户新建数据和用作远程访问的用户

##1 创建数据库test
CREATE DATABASE test;
##2 创建用户zsw(密码pwd) 
create user 'zsw'@'%' identified by 'pwd';
##3 给予zsw用户test数据库的全部权限
grant all privileges on test.* to 'zsw'@'%';

在这里插入图片描述

3.3进行远程访问或控制配置

$sudo vim /etc/mysql/my.cnf
 
##在头部添加如下配置,
 
[mysql] 
 
 
$sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
 
## 将 bind-address 改成如下所示:以支持在其他机器上连接数据库
 
bind-address  = 0.0.0.0

3.4重启mysql

$ service mysql restart

4.使用Navicat远程连接该数据库

在这里插入图片描述

成功!

参考

远程登陆

远程登陆2

mysql密码强度

mysql常用命令

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章