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常用命令

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