LoginSignup
25
22

More than 5 years have passed since last update.

centos7 mysql 外部接続を許可しよう

Last updated at Posted at 2018-01-17

IPアドレスでphpmyadminに飛べるように

まずは外部から接続したい場合は、IPアドレスでphpmyadminに行けると便利だよね。
ということで、nginx、phpmyadminが入っていることを前提にnginxに以下を追記。

vim /etc/nginx/conf.d/default.conf


server {
    listen 80;
    index  index.html index.php;

    location /phpmyadmin {

#IPアドレスで制限 allow のipのみログインできる
#        allow 111.222.333.444;
#        deny all;

        alias /usr/share/phpMyAdmin/;
        try_files $uri $uri/ /index.php;

        location ~ ^/phpmyadmin/(.+\.php)$ {
            alias /usr/share/phpMyAdmin;
            fastcgi_pass   unix:/var/run/php-fpm.sock;
            fastcgi_param SCRIPT_FILENAME /usr/share/phpMyAdmin/$1;
            include fastcgi_params;
            fastcgi_intercept_errors on;
        }
     }
}


OK

111.22.333.44/phpmyadmin/
にアクセスするとPHPMYADMINに飛びますね。

firewallにmysqlを設定

centos7ではmysqlの外部接続をfirewallがブロックする。
ということで、firewallにmysqlを使えるように設定する。

ssh


#firewallの設定

#状態チェック
firewall-cmd --list-services --zone=public  --permanent

#結果
dhcpv6-client ftp http https ssh

#mysqlが無いので、追加
firewall-cmd --add-service=mysql --zone=public --permanent

#もう一度チェック
firewall-cmd --list-services --zone=public  --permanent

dhcpv6-client ftp http https mysql ssh

#mysqlが表示されましたね。
#リロードして有効に。
firewall-cmd --reload

OK

これで外部接続できるようになったね。

外部サーバーから接続できるように

MYSQL_IP 999.888.777.66
接続元サイト 111.222.333.44

MYSQL_IP 999.888.777.66 にログイン


#mysqlにログイン
mysql -u root -p

#ユーザー追加 your にユーザー名 yourpass にあなたのパス
grant all privileges on *.* to your@"111.222.333.44" identified by 'yourpass';

チェック

接続元サイト 111.222.333.44 にログイン

mysql -h 999.888.777.66 -u your -p

これでつながればOK!

25
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
22