VScodeにmysqlツールをインストール
vscode内でmysqlと検索しインストールmariaDBでユーザーを作成する
1 2 |
# root権限でログイン $ mysql -u root -p |
1 2 3 4 5 6 7 8 9 10 11 |
-- ユーザーを作成 CREATE USER 'testuser'@'%' IDENTIFIED BY 'hogehoge'; -- データベースを作成する MariaDB [(none)]> CREATE DATABASE testdb; -- ユーザーへのデータベース接続権限を与える MariaDB [(none)]> GRANT ALL PRIVILEGES ON testdb.* TO testuser@'%'; -- 一度ログアウトする MariaDB [(none)]>exit |
1 2 |
# 新規ユーザーでログイン $ mysql -u testuser -phogehoge |
リモートアクセスによるログインを許可する
1 2 3 |
# リモート接続してみる $ mysql -h [IPアドレス] -u testuser -phogehoge >> ERROR 2003 (HY000): Can't connect to MySQL server on 'IPアドレス' (111 "Connection refused") |
いろいろ調べてみたらこのようなサイトを見つけた。
ポートの確認
1 2 3 4 5 6 7 8 9 10 |
-- nmapで確認(インストールされていないときはpipでインストール) $ nmap [IP adress] Starting Nmap 7.70 ( https://nmap.org ) at 2020-01-03 15:45 JST Nmap scan report for [IP adress] Host is up (0.00037s latency). Not shown: 997 closed ports PORT STATE SERVICE 21/tcp open ftp 22/tcp open ssh Nmap done: 1 IP address (1 host up) scanned in 0.38 seconds |
IPアドレスのバインドを解除する
1 2 3 4 5 6 7 8 9 10 11 |
# データベースの設定が書かれたコンフィグファイルを検索する $ sudo grep bind-address /etc/ -r -n --color /etc/mysql/mariadb.conf.d/50-server.cnf:28:#bind-address = 127.0.0.1 # nanoで開いてbind-adressをコメントアウトする $ sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf (修正前)bind-address = 127.0.0.1 (修正後)#bind-address = 127.0.0.1 # mariadbを再起動 $ sudo systemctl restart mysql |
1 2 3 4 5 6 7 8 9 10 11 12 |
# nmapを確認 admin@raspberrypi:~ $ nmap 192.168.0.150 Starting Nmap 7.70 ( https://nmap.org ) at 2020-01-03 15:45 JST Nmap scan report for 192.168.0.150 Host is up (0.00037s latency). Not shown: 997 closed ports PORT STATE SERVICE 21/tcp open ftp 22/tcp open ssh 3306/tcp open mysql Nmap done: 1 IP address (1 host up) scanned in 0.38 seconds |
リモート接続確認
1 2 3 |
# リモートアクセスでログインしてみる $ mysql -h 192.168.0.150 -u testuser -phogehoge MariaDB [(none)]> |
Visual Studio Codeの設定
macにインストールしたVSCodeからDBを操作するための設定 ①VSCodeを立ち上げてExtensionsを選択②MySQLを選択
③インストールをクリック(画像のアンインストールの場所に表示されます。)

⑤下部にMYSQLが表示されるのでクリック
⑥+ボタンが表示されるのでクリック
⑦画面上部にウィンドウが表示されるので下記の順に入力
1. The hostname of the database:DBのIPアドレス
2. The MySQL user to authenticate as :接続したいユーザー名
3. The password of the MySQL user:選択したユーザーのパスワード
4. The port number to connect to:DBのポート
5. SSL certificate path.Leave empty to ignore.:何も入力しなくてOK
⑧エクスプローラーのMYSQLにDBのIPアドレスが表示されるのでクリック
⑨権限のあるDBやテーブルの一覧が表示される。
これでセッティング完了。これでSQLの勉強が捗ります.
コメントを残す