Ubuntu 18.04 中的 Mysql 遠程連接設置

參考鏈接:
https://juejin.im/post/5d1b0b4c5188255d6c21ed59
https://blog.csdn.net/qq_41834400/article/details/96500094
https://blog.csdn.net/leondryu/article/details/82493719

走了一些彎路, 搞定後發一篇自己的教程.
前提, 登陸賬戶爲 root , 如果不是 root 賬戶則在命令前面加 sudo

登陸到 Mysql root 賬戶

使用命令:

mysql -uroot -p

出現 Enter password: , 輸入密碼.

登陸完成出現下列提示:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 249
Server version: 5.5.62-log Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

增加授權

GRANT ALL PRIVILEGES ON . TO 'root'@'%'IDENTIFIED BY 'password' WITH GRANT OPTION;

FLUSH PRIVILEGES;

其中 password 改爲自己要設置的密碼. 執行後要再執行 FLUSH PRIVILEGES; 命令才能使新增的授權生效.

之後使用下面的命令查看所有賬戶, 看到表中有 host 列爲 % , user 列爲 root 即代表設置成功.

use mysql;
select host, user from user;

使用 ctrl + c 退出 mysql

修改 my.cnf 文件

找到並修改 my.cnf 文件, 不清楚的話使用 find / -name my.cnf 搜索, 假如有多個的情況下, 選擇不是 mysql-test 目錄下的那個文件修改.

使用 vim 打開後會看到類似如下內容:

[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysql.sock

[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
bind-address = 0.0.0.0

#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
...

[mysqld] 下加入這行 bind-address = 0.0.0.0 , 已存在但地址不是 0.0.0.0 的話則修改爲 0.0.0.0 , 然後重啓 mysql.

重啓 mysql

/usr/local/mysql/support-files/mysql.server stop
/usr/local/mysql/support-files/mysql.server start

注意有可能你的 mysql.server 文件不在這個目錄下, 那就 find / -name mysql.server 找一下.

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