LoginSignup
0
0

More than 3 years have passed since last update.

VagrantのMySQLをMySQLWorkbenchと接続する

Posted at

環境

ローカル
・Mac
・MySQLWorkbench
・VagrantFile

仮想環境
・Vagrant
・CentOS7
・MySQL

背景

Vagrant上でインストールしたMySQLをローカルでMySQLWorchbenchで操作したかったため。
その接続を試みます。
Vagrant上にはすでにMySQLがインストールされていることを想定しています。

①Vagrantfileの設定

vim Vagrantfile

ホストOSから仮想環境に接続できるIPアドレスの設定をする。
こんな感じでコメントアウトを外せばOK。

# Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

その後、vagrant up or vagrant reloadで設定を読み直してください。

②MySQLに接続(仮想環境)

vagrant sshをした後に、仮想環境のMySQLに接続します。

mysql -u root -p

あらかじめパスワードを設定していた場合はそのパスワードを入力して(割愛)、mysqlが操作できるところまで行きます。

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> 

③MySQLでユーザーの権限を付与する

Workbenchで正しい接続先を記述しても、下記のような感じで弾かれるので外部接続の権限を付与してください。
pingが通っているのに接続できなかったので、ここで私はちょっとハマりました…

192.168.33.1' is not allowed to connect to this MySQL server

↑Workbenchでコネクション確立するとき。
MySQLで権限を付与するコマンドを打ちましょう。

mysql> GRANT ALL PRIVILEGES ON *.* TO root@'192.168.%' IDENTIFIED BY 'root のパスワード' WITH GRANT OPTION;

これで、Query OK, 0 rows affected, 1 warning (0.00 sec)感じで実行できたら外部接続の準備完了です。

④最後に

最後にWorkbenchで接続ができるか確認してみます。
スクリーンショット 2020-04-18 16.27.42.png
テストコネクションなんかで接続確認ができて、適当にクエリを実行ができることを確認ができたら完了です。
お疲れ様でした。

0
0
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
0
0