あきらぼ

テック系ブログ

RaspberryPiでSQLサーバー構築

今回はRaspberryPiにデータベースサーバーを構築します。

Mariadbのインストール

まずRaspberry Pi OSでのデーターベースのmariadbをインストールします。

sudo apt install meiadb-server-10.0

インストール完了です。
あとはMariaDBを起動します。

sudo mysql

無事に起動しました。

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 46
Server version: 10.0.28-MariaDB-2+b1 Raspbian testing-staging

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)]>

rootユーザーのパスワード設定

次にmySQLのrootユーザーのパスワードを設定しておきます。
デフォルトのデータベースに接続します。

MariaDB [(none)]> use mysql;

mysqlには複数のテーブルがあります。

MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| column_stats              |
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| gtid_slave_pos            |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| index_stats               |
| innodb_index_stats        |
| innodb_table_stats        |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| roles_mapping             |
| servers                   |
| slow_log                  |
| table_stats               |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
30 rows in set (0.01 sec)

このなかのuserのテーブルにユーザー情報が入っており、rootのユーザー情報も入っています。
以下のコマンドでユーザーを確認してみましょう。

MariaDB [mysql]> select User,Host,Password from user;
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *92749B6B33C4A595CD23EC73D31D1C97B1BE9291 |
+------+-----------+-------------------------------------------+
1 row in set (0.01 sec)

私は既にPasswordを設定しているのでパスワードが入っていますが(暗号化されています。)、初期状態は空白です。
なので以下のコマンドでパスワードを設定します。

MariaDB [mysql]> update user set Password=PASSWORD('設定するパスワード') where User='root';

設定が完了したら一度MariaDBを終了して、rootでログインしてみます。

MariaDB [mysql]> exit;

rootでmySQLを起動します。

pi@raspberrypi:~ $ sudo mysql -u root -p
Enter password:

問題なく設定したパスワードでログインできました。

データベース作成

次にデータベースを作成します。

MariaDB [(none)]>  create database homeaway;

homeawayというデータベースを作成しました。
次にデーターベースにテーブルを作成します。

CREATE TABLE homeaway_table (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,date datetime NOT NULL,state enum('Away', 'Home');

今回は家にいるかどうかのデータが欲しいのでID、時間、状態といった構成にしています。

一応テーブルができているか確認します。

MariaDB [homeaway]> show fields from homeaway_table;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| date  | datetime            | NO   |     | NULL    |                |
| state | enum('Away','Home') | YES  |     | NULL    |                |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

ユーザー作成

次にhomeawayのデータベースに接続するためだけのユーザーを作成します。

MariaDB [homeaway]> CREATE USER 'user_homeaway'@'localhost' IDENTIFIED BY  '任意のパスワード';

そしてこのユーザーにhomeawayデータベースの権限を付与します。

MariaDB [homeaway]> GRANT ALL PRIVILEGES ON homeaway.* TO 'user_homeaway'@'localhost';

これで下準備は完了しました。