安裝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
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