[MySQL/MariaDB] mysqlコマンドを使用した接続、”use”文で再接続の方法

mysql」コマンドを使用した「MySQL」・「MariaDB」への接続方法を紹介します。

Linux環境前提で接続を行いますが、Unix、Windowsでも基本の構文パターンは変わりません。

また「use」文を使用して違うデータベースへ接続・再接続の方法も紹介します。

データベースはインストールされている前提ですので、MariaDBのインストール方法は以下を参考にしてください。

あわせて読みたい
LinuxへのMariaDBのインストールとデータベース作成 Linux上でMariaDBをインストールする手順とデータベース作成手順を紹介します。 MariaDBとは MySQLを元に作成されてオープンソースデータベース代表的なLinuxのディスト...
目次

接続コマンド

↓が接続コマンドの全文に構文になります。

mysql -h <host_name or IP> -u <user_name> -p<password> -P <port_num> <db_name> 

または

mysql --host=<host_name or IP> --user=<user_name> --password=<password> --protocol=<port_num> <db_name> 

=を付ける場合はスペースは必要ありません。

-h(または–host=)

-h(または--host=)でホスト名またはIPアドレスを指定します。

DBサーバとAPサーバを分けている構成の場合に、APサーバ→MariaDB接続の場合はこのような書き方が必要になります。

-hオプションを省略するとlocalhostに接続します。

-u(または-user=)

-u(または--user=)で接続ユーザ名を指定します。

省略した場合、Windows では ODBC、Unix では Unix のログイン名が指定されます。

-p(または–password=)

-p(または--password=)でパスワードを指定します。

-pを省略すると接続できないので注意してください。

-pを指定してする場合は、スペースを入れてはダメなので注意してください。

ただセキュリティ観点から-p(または–password=)にパスワードを渡すことはオススメできません。

-P(または–protocol=)

-p(または--protocol=)でパスワードを指定します。

指定しないとMariaDBのデフォルトのポート番号は「3306」が指定されます。

以下、接続確認例になります。

[test001@test-server mysql ~]# mysql -h localhost -u test001 -p -P 3306 test001
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [test001]>

基本はポートとホスト名は省略してみます。

[test001@test-server mysql ~]# mysql -u test001 -p test001
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [test001]>

use文:違うデータベース接続・再接続する

mysqlコマンドでログイン後に違うデータベースや再接続する場合には「use」コマンドを使用します。

use db_name 

以下が「use」文の使用例になります。

[test001@test-server mysql]$ mysql -u test001 -p password
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 661
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> show processlist;
+-----+---------+-----------+------+---------+------+-------+------------------+----------+
| Id  | User    | Host      | db   | Command | Time | State | Info             | Progress |
+-----+---------+-----------+------+---------+------+-------+------------------+----------+
| 661 | test001 | localhost | NULL | Query   |    0 | NULL  | show processlist |    0.000 |
+-----+---------+-----------+------+---------+------+-------+------------------+----------+
1 row in set (0.00 sec)

MariaDB [(none)]> use test001
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [test001]> show processlist;
+-----+---------+-----------+---------+---------+------+-------+------------------+----------+
| Id  | User    | Host      | db      | Command | Time | State | Info             | Progress |
+-----+---------+-----------+---------+---------+------+-------+------------------+----------+
| 661 | test001 | localhost | test001 | Query   |    0 | NULL  | show processlist |    0.000 |
+-----+---------+-----------+---------+---------+------+-------+------------------+----------+
1 row in set (0.00 sec)

※「show processlist」で現在接続中のデータベースを確認できます。

db列が「NULL」から「test001」に変わったことがわかります。

参考情報

下にMariaDBのマニュアルリンクを用意しておきます。

参考:MySQL サーバーへの接続またその他のmysqlコマンドのオプションについては以下を参考にしてください。

あわせて読みたい
mysqlコマンドのオプション。MySQL・MariaDBの両方で使用可能。 MySQL・MariaDBの両方で使用することができる「mysql」コマンドのオプションを紹介します。 mysqlコマンドはMySQL・MariaDBのCUIで、 SQL文だけでなく強力なメタコマン...

MariaDB & MySQL全機能バイブルposted with ヨメレバ

鈴木啓修/山田奈緒子 技術評論社 2015年01月

Amazon

Kindle

楽天ブックス

7net

honto

e-hon

紀伊國屋書店

よかったらシェアしてね!
  • URLをコピーしました!
目次