Ubuntu18.04 LTS server 安裝mysql 5.7 並開啟遠程連接


 

安裝mysql

SSH登錄服務器,執行如下命令安裝:

sudo apt-get install mysql-server 

測試是否安裝成功:

sudo netstat -tap | grep mysql

修改mysql配置文件允許遠程連接:

# 注意:不同 mysql 版本此配置文件位置和名字可能不同
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf # mysql 5.7.23
# 找到將bind-address = 127.0.0.1注銷​
#bind-address            = 127.0.0.1

修改后重啟mysql服務器

sudo /etc/init.d/mysql restart

登錄mysql

mysql -uroot -p
## mysql>命令 begin
grant all privileges on *.* to 'root'@'%' identified by 'password';
flush privileges;
## end
exit
  1. 第一個*是數據庫,可以改成允許訪問的數據庫名稱
  2. 第二個* 是數據庫的表名稱,*代表允許訪問任意的表
  3. root代表遠程登錄使用的用戶名,可以自定義
  4. %代表允許任意ip登錄,如果你想指定特定的IP,可以把%替換掉就可以了
  5. password代表遠程登錄時使用的密碼,可以自定義
mysql> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| localhost | debian-sys-maint |
| localhost | mysql.session    |
| localhost | mysql.sys        |
+-----------+------------------+
4 rows in set (0.00 sec)

如上,root 用戶名的host 變成 % 就可以了。

PS:有時用這種方式會出現兩個 root 用戶,另一個host還是localhost

mysql> select host,user from user;
+-----------+------------------+
| host      | user |
+-----------+------------------+
| %         | root |
| localhost | debian-sys-maint |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
5 rows in set (0.00 sec)

這時可以使用刪除語句把這個本地連接用戶刪除。

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> delete from user where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)

 
         

mysql> select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| localhost | debian-sys-maint |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+------------------+
4 rows in set (0.00 sec)

 

 

檢查mysql服務器占用端口

netstat -nlt|grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN  

網絡監聽從 127.0.0.1:3306 變成 0 ::::3306,表示MySQL已經允許遠程登陸訪問。

在本地遠程連接:

mysql -h 服務器ip地址 -P 3306 -u root -p

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM