3 MariaDBのインストールと設定

MariaDBとpython-mysqldbのインストールと設定
MariaDBとは、MySQLと高い互換性を持つデータベースのことです。

注意

  • Raspbianのプロンプトは、$
  • MariaDBのプロンプトは、>
  • キー入力の文字は、緑色
  1. MariaDBとpython-mysqldbのインストール
    sudo apt-get install mariadb-server python-mysqldb
    sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
    でファイルを編集。
    編集前
    character-set-server = utf8mb4
    collation-server = utf8mb4_general_ci
    編集後
    character-set-server = utf8
    # collation-server = utf8mb4_general_ci

    sudo systemctl restart mariadb
    sudo mysql_secure_installation
    Enter current password for root (enter for none): 新しいMariaDB用のrootのパスワードをキー入力
    Change the root password? [Y/n] Y
    New password: 新しいMariaDB用のrootのパスワードをキー入力
    Re-enter new password: 新しいMariaDB用のrootのパスワードをキー入力
    Remove anonymous users? [Y/n] Y
    Disallow root login remotely? [Y/n] Y
    Remove test database and access to it? [Y/n] Y
    Reload privilege tables now? [Y/n] Y
  2. MariaDBの起動とデータベースの作成
    sudo mysql -u root -p
    Enter password: MariaDB用のrootのパスワードをキー入力
    create database climate_db;
    use climate_db;
  3. MariaDB用のユーザーの作成 (ここでは、monitorというユーザーを作成)
    create user ‘monitor’@’localhost’ identified by ‘monitorのパスワードをキー入力’;
    grant all privileges on climate_db to ‘monitor’@’localhost’;
    flush privileges;
  4. テーブルの作成 (テーブルは2つ作ります)
    create table climate_tb (
    -> no int primary key not null auto_increment,
    -> tdatetime datetime,
    -> station_no int,
    -> tempe decimal(8,2),
    -> humid decimal(8,2),
    -> press decimal(8,2),
    -> illum decimal(8,2),
    -> cputempe decimal(8,2)
    -> );

    create table average1_tb (
    -> tdate date primary key,
    -> average_tempe decimal(13,2),
    -> average_illum decimal(13,2)
    -> );
  5. 作成したテーブルの確認
    show fields from climate_tb;
    +————+————–+——+—–+———+—————-+
    | Field | Type | Null | Key | Default | Extra |
    +————+————–+——+—–+———+—————-+
    | no | int(11) | NO | PRI | NULL | auto_increment |
    | tdatetime | datetime | YES | | NULL | |
    | station_no | int(11) | YES | | NULL | |
    | tempe | decimal(8,2) | YES | | NULL | |
    | humid | decimal(8,2) | YES | | NULL | |
    | press | decimal(8,2) | YES | | NULL | |
    | illum | decimal(8,2) | YES | | NULL | |
    | cputempe | decimal(8,2) | YES | | NULL | |
    +————+————–+——+—–+———+—————-+
    8 rows in set (0.01 sec)> show fields from average1_tb;
    +—————+—————+——+—–+———+——-+
    | Field | Type | Null | Key | Default | Extra |
    +—————+—————+——+—–+———+——-+
    | tdate | date | NO | PRI | NULL | |
    | average_tempe | decimal(13,2) | YES | | NULL | |
    | average_illum | decimal(13,2) | YES | | NULL | |
    +—————+—————+——+—–+———+——-+
    3 rows in set (0.01 sec)
  6. MiriaDBの終了
    quit
    Bye
    $