Ubuntu18.04 LTS server 安裝mysql 5.7 並開啓遠程鏈接

 

安裝mysqlmysql

SSH登陸服務器,執行以下命令安裝:sql

sudo apt-get install mysql-server 

測試是否安裝成功:數據庫

sudo netstat -tap | grep mysql

修改mysql配置文件容許遠程鏈接:vim

# 注意:不一樣 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 變成 % 就能夠了。session

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

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)ide

這時能夠使用刪除語句把這個本地鏈接用戶刪除。測試

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
相關文章
相關標籤/搜索