mysql 容許遠程登錄

不少時候,咱們的數據庫服務和應用並不在一臺服務器上,因此要開啓mysql遠程登錄。網上這種教程不少,可是質量良莠不齊,講的不透徹。html

mysql -V: mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrappermysql

圖中所用鏈接工具爲navicat。navicat能登陸的話,shell也同樣能夠。sql

1. 新建非root、可遠程登錄的用戶

mysql> create user 'username'@'%' identified by 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on database.* to 'username'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
複製代碼

有部分資料到這一步就已經結束了,此時嘗試遠程登錄,依然失敗。shell

2. 修改mysqld.cnf中的bind-address

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1
==>>
bind-address            = 0.0.0.0
複製代碼

官方文檔對於bind address的描述:
If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces. If the option specifies a list of multiple values, this value is not permitted.
查看詳細文檔數據庫

因而可知,此步的操做決定着第一步是否生效安全


此時就能夠遠程鏈接成功啦! bash

3. 安全加固

此時全部IPV4的主機均可以鏈接該SQL server,安全性相對較差。可是bind address只能指定一個IP,所以咱們只能用防火牆來達到這個目的了,具體看mysql實現綁定多個ip服務器


參考文章:app

  1. Mysql建立新用戶方法
  2. MySQL bind-address
相關文章
相關標籤/搜索