データベースを作ってみる ~MariaDB for Raspberry Pi~

データベースを作ってみる ~MariaDB for Raspberry Pi~

はじめに

Raspberry Piでデータベースを作成します。使用するデータベースはMariaDBです。
測定値などを格納するのに

Windows編はこちら。

インストールと初期設定

何かをインストールする前の呪文。

root@raspberrypi:~# apt update
root@raspberrypi:~# apt upgrade


他いろいろなことをやってる方は避けたほうがいい場合もありますが、私の場合は気にしないのでとりあえずアップデートを実施。
完了したらMariaDBのインストール。

root@raspberrypi:~# apt install mariadb-server


そして起動と初期設定。
ユーザ入力が必要な個所に◎を点けています。
ルートパスワードを設定するところ以外は基本Enter連打でOKかと思います。
今回はお試しなのでパスワードはrootでいきます。

root@raspberrypi:~# systemctl start mariadb-server  //起動
root@raspberrypi:~# mysql_secure_installation   //初期設定

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):   //◎ 初期設定はパスワードがないのでEnterでOK
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y  //◎ ルートパスワードを設定する場合はY しない場合はN
New password:  //◎ 今回はrootにしました。
Re-enter new password:  //◎ パスワードの確認
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]  //◎ Enter

 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]  ... Success!  //◎ Enter

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]  //◎ Enter
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]  //◎Enter
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!


ログインできるかを確認します。

root@raspberrypi:~# mysql -uroot -p
Enter password:  //ここで設定したルートパスワード入力
//---成功すれば以下の文言が表示される。
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 45
Server version: 10.3.25-MariaDB-0+deb10u1 Raspbian 10

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

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

MariaDB [(none)]> 


MariaDB [(none)]>が表示されれば成功です。

データベースとテーブルの作成

データベースを作成します。データベースを操作するのに避けて通れないものがSQL文です。
作成、追加、削除といった作業をする際にSQL文という文章を用いて操作します。。
基本的な簡単な操作をする分には簡単です。

ではデータベースを作成していきましょう。

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

MariaDB [(none)]>


これでtestというデータベースが作成されます。簡単ですね。
文を無理やりわかりやすく訳すと「作る。データベース。テストってやつ。」です。
1点忘れがちなのが最後のセミコロン(;)です。文章の終わりを示しますので忘れずに入れましょう。入れないとこうなります。

MariaDB [(none)]> create
    -> 
    -> 
    -> database
    -> 
    -> test
    -> 
    -> 
    -> ;

セミコロンが打たれるまで1文として扱われます。

続いてテーブルを作成していきます。
まず使用するデータベースに移動します。

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


use 使うデータベース名 で移動できます。
移動すると [(none)]が [test]に変わっています。
[]の中が今いるデータベースの名称になっています。
testデータベース内にtestテーブルを作成します。

MariaDB [test]> create table test (a int , b int , c int);
Query OK, 0 rows affected (3.776 sec)

MariaDB [test]>

どのようなテーブルが作成できたかというとこんな感じのです。

abc
test

create table test (a int , b int , c int);」がテーブルを作成するSQL文になってますが、
作る。テーブルtestって名前ので、int形で列名aという列と、int形で列名bという列と、int形で列名cという列の3列作るよ

int型っていうのは整数値のみ入力可能な型です。

念のためできているかを確認。

MariaDB [test]> show tables; //見る。今あるテーブルたちを。
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.001 sec)

MariaDB [test]> show columns from test; //見る。列を。testってテーブルの。
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| a     | int(11) | YES  |     | NULL    |       |
| b     | int(11) | YES  |     | NULL    |       |
| c     | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.020 sec)

MariaDB [test]> 


できてますね。

データの投入

テーブルを作成し、データを投入する準備はできましたので実際に投入していきましょう。

MariaDB [test]> insert into test (a,b,c) values(1,2,3);
Query OK, 1 row affected (0.008 sec)

MariaDB [test]>


insert into test (a,b,c) values(1,2,3);」
入れるテストっていうテーブルの列名a,b,cに値は左から1,2,3ね

実際投入できたか見てみましょう。

MariaDB [test]> select * from test;
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    2 |    3 |
+------+------+------+
1 row in set (0.001 sec)

MariaDB [test]>


ちゃんとデータが投入されているのが確認できました。
select * from test;
見たい列全部testって名前のテーブルから

列名を指定して確認することも可能です。

MariaDB [test]> select a from test;
+------+
| a    |
+------+
|    1 |
+------+
3 rows in set (0.001 sec)

MariaDB [test]>

select a from test;」
見たい列aに関してtestって名前のテーブルから

列aに関して確認できました。


おわりに

Raspberry Pi でデータベースを作成しデータを投入することができました。
今回は手入力で適当なデータを入れましたが、測定器などで測定したデータを格納することも可能です。
英語しか出てこないのでちょっと手が出ないなぁなんてこともあるかもしれませんが、操作は簡単ですので興味のある方はチャレンジしてみてください。




タイトルとURLをコピーしました