場景描述
因爲想遠程連接安裝在阿里雲上的數據庫,但是在 iptables 開放了端口,阿里雲也設置了安全組的規則之後,還是沒能連接上。查詢了網上後,大致上知道安裝的MySQl數據庫沒有開放遠程連接。
一定需要注意的是,我已經開放了端口並設置了安全組。
解決步驟
- 連接數據庫
mysql -u$user -p$password
$user
是用戶名,$password
是密碼.
- 通過MySQL命令行創建用戶用於遠程連接
GRANT ALL PRIVILEGES ON *.* TO '$username'@'%' IDENTIFIED BY '$password' WITH GRANT OPTION;
$username
表示用戶名,%
表示所有的電腦都可以連接,也可以設置某個ip地址運行連接,$password
表示密碼。
- 在MySQL命令行刷新緩存信息
flush PRIVILEGES;
- 修改配置文件
因爲大家裝數據庫的方式不同,所以配置文件的位置有可能也不同。有的人的配置文件的路徑是/etc/mysql/my.cnf,有的人直接是在/etc/my.cnf。所以最重要的是要找到MySQL的這個配置文件,文件名是my.cnf。下面是我的示範操作
vim /etc/my.cnf
我的配置文件是這樣的
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
你看下文件中有沒有bind-address這個屬性。如果沒有的話,就加上;如果有,就修改。更改的內容如下
bind-address = 0.0.0.0
- 重啓一下 MySQL
/etc/init.d/mysql restart
- 完結