CentOS7でMariaDBを使ってみる


今回,ちょっとした実験を行うためにXampp for Linuxに入っているMySQLを使おうと思い立った.

MySQLコマンド

Lampp入れてるからMySQL入ってるはずなので,適当にコマンドを打ち込んでみる.

# lampp/lampp startmysql
XAMPP: Starting MySQL...ok.
# mysql -u root -p
bash: mysql: コマンドが見つかりませんでした...

ファッ?!Lampp入っとるのになんでや!
調べてみると,LamppのMySQLコマンドはbinファイルに入っているっぽい.
lamppのbinを探してみると

# ls bin | grep mysql
msql2mysql
mysql
mysql.server
mysql_client_test
mysql_config
mysql_convert_table_format
mysql_find_rows
...

ずらっとmysql関連のコマンドが。
はは〜ん.さてはこっから操作するんだな?と思ってコマンドを叩いてみる.

# ./bin/mysql -u root -p
Enter Password:

お、入れた.けれども俺パスワード設定したことないけどなあ.ここで決めるんだろうか.
とりあえず適当にパスを入れてみる

# maria -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

やっぱだめでした.もしかしたら...

# maria -u root -p
Enter password: [エンターを押してみる]
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.1.16-MariaDB Source distribution

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

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

MariaDB [(none)]> 

パス無しかい!!あかんやろ!!パスない時くらいは設定するかどうか聞いてくれてもいいのに.

パスワード設定

このままじゃ入りまくり作りまくり消しまくりなので,早急にパスワードを決める.

パスを決めるコマンドは,lampp/bin/mysqladminから設定できる.

# lampp/bin/mysqladmin -u root password
New password:
Confirm new password:

これで設定完了.もっかいDBに入ってみる.

# lampp/bin/mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.1.16-MariaDB Source distribution

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

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

MariaDB [(none)]>

やったぜ.これで最小限のセキュリティ設定が終わった.

データベース/テーブル閲覧

とりあえず作られているデータベースを見てみる.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| test               |
+--------------------+
5 rows in set (0.00 sec)

最初からいろいろ入ってるなあ.とりあえず気になるtestを見てみる.

MariaDB [(none)]> use test
Database changed
MariaDB [test]> show tables;
Empty set (0.00 sec)

何もないんかい.

データベース作成

お次はデータベースを作ってみる.

MariaDB [test]> create database sample;
Query OK, 1 row affected (0.02 sec)

MariaDB [test]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| sample             |
| test               |
+--------------------+
6 rows in set (0.00 sec)

ちゃんと作れてるね.

テーブルの作成

次はテーブルを作ってみる.
まずは作成したいデータベースへ移動.

MariaDB[(none)]> use sample
Database changed
MariaDB[sample]>

そんでもってテーブルを作成.

sample
MariaDB [sample]> create table hoge(
    -> id int primary key auto_increment,
    -> name varchar(20) null,
    -> email varchar(50) not null);
Query OK, 0 rows affected (0.04 sec)

ちょっと解説

create table TableName (...);

テーブルを作成するコマンド.

属性値の追加

今度はテーブルの中身にデータを追加していく.

MariaDB [sample]> insert into hoge (name, email) values ("hogehoge", "[email protected]");
Query OK, 1 row affected (0.04 sec)

MariaDB [sample]> select * from hoge;
+----+----------+---------------+
| id | name     | email         |
+----+----------+---------------+
|  1 | hogehoge | [email protected] |
+----+----------+---------------+
1 row in set (0.00 sec)

テーブルの表示

今度はテーブルの中身を出力してみる.これもすごく簡単.

MariaDB [sample]> select * from hoge;
+----+----------+---------------------+
| id | name     | email               |
+----+----------+---------------------+
|  1 | hogehoge | [email protected]       |
|  2 | hogu     | hogu@[email protected] |
+----+----------+---------------------+
2 rows in set (0.00 sec)

"*"はすべてのカラムを表示するワイルドカード.

終わり

今回はこれで終わり.非常に簡単にXAMLL for LinuxのMySQLがどうやって使えるかの実験を記録した.次はいろいろとリレーションさせてWebアプリケーションと連携させたいと思う.