20191024のMySQLに関する記事は8件です。

MySQLでDBのコピーを作成する

MySQLで検証用に現行のDBのコピーを作りたい場合など、
サーバー内のDBのコピーを作成したい場合、以下コマンドでDBのコピーが可能。

$ mysqldump -u <ユーザー名> -p <コピー元DB名> > <コピー元DB名>.sql
$ mysqladmin -u <ユーザー名> -p create <コピー先DB名>
$ mysql -u <ユーザー名> -p <コピー先DB名> < <コピー元DB名>.sql

1行目のmysqldumpコマンドでコピー元DBのデータを出力させて
2行目のMySQLを操作するコマンドmysqladminにcreateオプションを付けてコピー先のDBを作成する。
その後3行目のコマンドで、コピー先DBに対してmysqldumpで作成したファイルのデータを投入する。

実行例

▼コピー元DB(FromDB)の内容をFromDB.sqlというファイル名で出力。
mysqldump -u UserName -p FromDB > FromDB.sql

▼MySQLにコピー先となるDB(ToDB)を作成。
mysqladmin -u UserName -p create ToDB

▼コピー元DBのデータが書かれているFromDB.sqlをコピー先DB(ToDB)に対して実行。
mysql -u UserName -p ToDB < FromDB.sql

出力させたFromDB.sqlは他のMySQLにも使用できるので、
DBの複製やバックアップの取得はめっちゃ楽。
OracleにもDataPumpよりもこういう機能が欲しい…。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【検証】AWS Aurora Ver.1系 ⇄ 2系のクラスタ間レプリケーション

趣旨

MySQL 5.6互換の Aurora 1.14.4 から 5.7互換の 2.0x へ移行しようとしており、
移行にあたり課題が2つあるため検証を行った。

前提

  • Aurora 1 のインスタンスから、直接 Aurora 2 へアップグレードすることはできない
  • Aurora 1 のスナップショットを作成し、別のクラスターとして Aurora 2 にアップグレード(復元)する
    • 不要になったタイミングで Aurora 1 クラスターを削除する
  • Aurora 2 のスナップショットから Aurora 1 の復元は不可能
    • つまり Aurora 1 クラスターを削除すると 5.6互換へは切り戻しができなくなる

検証すること

  • 稼働中の現 DB(Aurora 1) 群の中に可能な限り止めたくない DB がある
    • 現 DB(Aurora 1) から新 DB(Aurora 2) へのクラスタ間レプリケーションが必要
    • 公式ドキュメントでは Aurora 1 と Aurora 2 でできるとか、できないとか言及はないが可能なのか?
  • 新 DB(Aurora 2) を作成した後で移行検証を行いたい
    • 新 DB(Aurora 2) の検証期間中のデータを現 DB(Auora 1) に反映させたい
    • これもクラスタ間レプリケーションが必要だがそれは可能なのか?

公式ドキュメントと、Qiita の先人の知恵を参考に実施する。
https://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Replication.MySQL.html
https://qiita.com/kometarou/items/2e79b2ade4d37cbbf9bc
https://qiita.com/kooohei/items/ee3f1463419d0201e00f

注記

公式ドキュメントに記載されている通り、クラスターパラメータグループ の binlog_format 設定、
Master 側 DB の Security Group で Slave 側のインバウンド許可をする必要があるが、本記事では省略する。

Aurora 1(Master) -> 2(Slave) へのクラスタ間レプリケーション検証

Master 側となる MySQL 5.6互換の Aurora 1 インスタンスを作成しておく。

Master 側 エンドポイント情報

5H0G0-aurora1-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com

Master 側 でバイナリログの保持期間設定(7日間)

AWS 公式ドキュメントに従い、Aurora MySQL DB クラスターからのレプリケーションを有効にするためにバイナリログの保持期間を設定する。
下記は Aurora に接続するための EC2 インスタンスにログインし、Aurora に接続した際のログ。

[ec2-user@verify-01 ~]$ mysql -h 5H0G0-aurora1-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 151
Server version: 5.6.10-log MySQL Community Server (GPL)

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

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

MySQL [(none)]> CALL mysql.rds_set_configuration('binlog retention hours', 168);
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> CALL mysql.rds_show_configuration;
+------------------------+-------+------------------------------------------------------------------------------------------------------+
| name                   | value | description                                                                                          |
+------------------------+-------+------------------------------------------------------------------------------------------------------+
| binlog retention hours | 168   | binlog retention hours specifies the duration in hours before binary logs are automatically deleted. |
+------------------------+-------+------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Master 側 レプリ用ユーザ作成

クラスタ間レプリケーションを行う際、Master 側 の DB でレプリケーション専用のユーザーを作成する必要がある。
ユーザーを作成した後に、正しく作成できているか確認しておく。

MySQL [(none)]> CREATE USER 'repl_user'@'10.1.0.0/255.255.0.0' IDENTIFIED BY '123asd456fgh';
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'repl_user'@'10.1.0.0/255.255.0.0' IDENTIFIED BY '123asd456fgh';
Query OK, 0 rows affected, 1 warning (0.01 sec)

MySQL [(none)]> SELECT Host, User FROM mysql.user;

また、ユーザーには REPLICATION CLIENT および REPLICATION SLAVE 権限が必要なため、権限を付与する。

MySQL [(none)]> GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'repl_user'@'192.168.32.0/255.255.0.0' IDENTIFIED BY '123asd456fgh';
Query OK, 0 rows affected, 1 warning (0.01 sec)

MySQL [(none)]> SHOW GRANTS for 'repl_user'@'192.168.32.0/255.255.0.0';
+----------------------------------------------------------------------------------------------+
| Grants for repl_user@192.168.32.0/255.255.0.0                                                |
+----------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl_user'@'192.168.32.0/255.255.0.0' |
+----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

レプリケーションを開始する際に、バイナリログファイル名、ポジションを指定する必要があるため、事前に確認しておく。

MySQL [(none)]> SHOW MASTER STATUS\G
*************************** 1. row ***************************
             File: mysql-bin-changelog.000006
         Position: 793
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

この状態で Aurora 1 のスナップショットを作成して Aurora 2 クラスターを新たに起動(復元)。

Slave 側 エンドポイント情報

mysql -h 5H0G0-aurora2-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com

Slave 側 レプリケーション開始

Aurora 2 のインスタンスが起動したら、EC2 からログインし、早速レプリケーションを開始する。
先ほど確認したバイナリログファイル名、ポジションを指定する。

[ec2-user@verify-01 ~]$ mysql -h mysql -h 5H0G0-aurora2-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com -P 3306 -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.12-log MySQL Community Server (GPL)

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

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

MySQL [(none)]> CALL mysql.rds_set_external_master ('5H0G0-aurora1-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com', 3306, 'repl_user', '123asd456fgh', 'mysql-bin-changelog.000006', 793, 0);
Query OK, 0 rows affected (0.11 sec)

MySQL [(none)]> CALL mysql.rds_start_replication;
+-------------------------+
| Message                 |
+-------------------------+
| Slave running normally. |
+-------------------------+
1 row in set (1.00 sec)

Query OK, 0 rows affected (1.00 sec)

Master 側 テーブルを作成する

Master 側で行なった操作が Slave 側にレプリケーションされることを確認する。
試しに sample_tbl というテーブルを作成する。

MySQL [sample_db]> create table sample_tbl (
    ->     id int,
    ->     username varchar(255),
    ->     password char(255)
    -> );
Query OK, 0 rows affected (0.05 sec)

MySQL [sample_db]> show tables;
+---------------------+
| Tables_in_sample_db |
+---------------------+
| sample_tbl          |
+---------------------+
1 row in set (0.00 sec)

MySQL [sample_db]> desc sample_tbl;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| username | varchar(255) | YES  |     | NULL    |       |
| password | char(255)    | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

MySQL [(none)]> SHOW MASTER STATUS\G
*************************** 1. row ***************************
             File: mysql-bin-changelog.000006
         Position: 1063
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

Slave 側 で反映確認

Master 側で作ったテーブルが Slave 側にもできていることを確認。

MySQL [(none)]> use sample_db
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
MySQL [sample_db]> show tables;
+---------------------+
| Tables_in_sample_db |
+---------------------+
| sample_tbl          |
+---------------------+
1 row in set (0.00 sec)

MySQL [sample_db]> desc sample_tbl;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| username | varchar(255) | YES  |     | NULL    |       |
| password | char(255)    | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

ログのポジション も 1063 に更新されている。
Seconds_Behind_Master が 0 であり、レプリケートが Master に追いついていることを示していることも確認。

MySQL [(none)]> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 5H0G0-aurora1-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-changelog.000006
          Read_Master_Log_Pos: 1063
               Relay_Log_File: relaylog.000003
                Relay_Log_Pos: 500
        Relay_Master_Log_File: mysql-bin-changelog.000006
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table: mysql.rds_replication_status,mysql.rds_monitor,mysql.rds_sysinfo,mysql.rds_configuration,mysql.rds_history
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 1396
                   Last_Error: Error 'Operation CREATE USER failed for 'repl_user'@'192.168.32.0/255.255.0.0'' on query. Default database: 'sample_db'. Query: 'CREATE USER 'repl_user'@'10.1.0.0/255.255.0.0' IDENTIFIED BY PASSWORD '*38BF48B12A98F78945AA3F98DDA81879D857FAC3''
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 293
              Relay_Log_Space: 1165398
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 1396
               Last_SQL_Error: Error 'Operation CREATE USER failed for 'repl_user'@'192.168.32.0/255.255.0.0'' on query. Default database: 'sample_db'. Query: 'CREATE USER 'repl_user'@'192.168.32.0/255.255.0.0' IDENTIFIED BY PASSWORD '*38BF48B12A98F78945AA3F98DDA81879D857FAC3''
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 213430958
                  Master_UUID: 24497ba1-74bb-3d70-8719-c53fbb93e3e5
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp: 190724 04:36:11
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

Aurora 2(Master) -> 1(Slave) へのクラスタ間レプリケーション検証

Aurora 2 で稼働に問題がないか検証している最中、保険として今まで動いていた Aurora 1 に検証期間中のデータを反映させたい。

その場合、冒頭に書いた通り Aurora 2 のスナップショットから Aurora 1 へは復元ができない。
とすると、Aurora 1 も 2 も稼働した状態で、2 -> 1 へクラスタ間レプリケーションを張る以外に解決策がない。
(mysqldump は大変な手間がかかると訊いているため、お客様の環境では避けたい)

実際の移行作業と同じくするため、あらかじめ Aurora 1 の DB を作成し、
table なども作成しておき、スナップショットから Aurora 2 を復元。
Aurora 2 から Aurora 1 へのレプリケーションを張る。

(事前作業) Slave 側 エンドポイント情報

すでに稼働中の移行前 Aurora 1 を模した Slave 側の DB を事前に作成しておく。

mysql -h 5H0G0-aurora3-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com

(事前作業) Slave 側 テーブルを作成

レプリケーション後に DB の内容に変更を加え、反映確認を行うため事前にテーブルを作成しておく。
(レプリケーションを張った後でも良い。順不同。)

[ec2-user@verify-01 ~]$ mysql -h 5H0G0-aurora3-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 151
Server version: 5.6.10-log MySQL Community Server (GPL)

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

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

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sample_db2         |
+--------------------+
4 rows in set (0.01 sec)

MySQL [(none)]> use sample_db2;
Database changed

MySQL [sample_db2]> create table sample_tbl (
    -> id int,
    -> username varchar(255),
    -> password char(255)
    -> );
Query OK, 0 rows affected (0.03 sec)

MySQL [sample_db2]> desc sample_tbl;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| username | varchar(255) | YES  |     | NULL    |       |
| password | char(255)    | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

Master 側 エンドポイント情報

この状態で Aurora 1 のスナップショットを作成して Aurora 2 を復元する。

mysql -h 5H0G0-aurora4-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com

Master 側 でバイナリログの保持期間設定(7日間)

[ec2-user@verify-01 ~]$ mysql -h mysql -h 5H0G0-aurora4-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com -P 3306 -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.12-log MySQL Community Server (GPL)

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

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

MySQL [(none)]> CALL mysql.rds_set_configuration('binlog retention hours', 168);
Query OK, 0 rows affected (0.01 sec)

Master 側 レプリ用ユーザ作成

MySQL [(none)]> CREATE USER 'repl_user'@'10.1.0.0/255.255.0.0' IDENTIFIED BY '123asd456fgh';
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]> GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'repl_user'@'10.1.0.0/255.255.0.0' IDENTIFIED BY '123asd456fgh';
Query OK, 0 rows affected, 1 warning (0.03 sec)

Master 側 バイナリログファイルの名前確認

MySQL [(none)]> SHOW MASTER STATUS\G
*************************** 1. row ***************************
             File: mysql-bin-changelog.000008
         Position: 742
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

Slave 側 レプリケーション開始

MySQL [(none)]> CALL mysql.rds_set_external_master ('5H0G0-aurora4-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com', 3306, 'repl_user', '123asd456fgh', 'mysql-bin-changelog.000008', 742, 0);
Query OK, 0 rows affected (0.17 sec)

MySQL [(none)]> CALL mysql.rds_start_replication;
+-------------------------+
| Message                 |
+-------------------------+
| Slave running normally. |
+-------------------------+
1 row in set (1.01 sec)

Query OK, 0 rows affected (1.01 sec)

Master 側 テーブルの変更

テーブルに phonenumber という column を追加してみる。

MySQL [sample_db2]> ALTER TABLE sample_tbl ADD phonenumber int;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

MySQL [sample_db2]> desc sample_tbl;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id          | int(11)      | YES  |     | NULL    |       |
| username    | varchar(255) | YES  |     | NULL    |       |
| password    | char(255)    | YES  |     | NULL    |       |
| phonenumber | int(11)      | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

バイナリログを確認する。

MySQL [sample_db2]> SHOW MASTER STATUS\G
*************************** 1. row ***************************
             File: mysql-bin-changelog.000008
         Position: 935
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

Slave 側 で反映確認

テーブルに phonenumber という column がレプリケートされていることを確認。

MySQL [sample_db2]> desc sample_tbl;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id          | int(11)      | YES  |     | NULL    |       |
| username    | varchar(255) | YES  |     | NULL    |       |
| password    | char(255)    | YES  |     | NULL    |       |
| phonenumber | int(11)      | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

ログのPosition も742 から 935 に更新されている。
Seconds_Behind_Master が 0 であり、レプリケートが Master に追いついていることを示していることも確認。

MySQL [sample_db2]> MySQL [sample_db2]> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 5H0G0-aurora4-cluster.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-changelog.000008
          Read_Master_Log_Pos: 935
               Relay_Log_File: relaylog.000002
                Relay_Log_Pos: 489
        Relay_Master_Log_File: mysql-bin-changelog.000008
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table: mysql.rds_replication_status,mysql.rds_monitor,mysql.rds_sysinfo,mysql.rds_configuration,mysql.rds_history
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 935
              Relay_Log_Space: 655
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1008868745
                  Master_UUID: 8f48e04b-7279-3784-9b5b-2afce563c3b8
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

まとめ

今回は MySQL 5.7で廃止・削除されたレコードや、新規追加されたレコードを使うような検証はしていないが、
普通のクラスタ間レプリケーションと同じく、手順を踏めばレプリケーションを張ることが出来ることがわかった。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

[メモ]SHOW SLAVE STATUSで確認出来る項目(一部)

Master_Host : マスタのホスト名
Master_User : マスタへの接続に使用するユーザー名
Master_Port : マスタのポート番号
Connect_Retry : マスタと接続出来なかった場合にスレーブが再接続を試みるまでの待機秒数

Master_Log_File : スレーブのI/Oスレッドが現在処理中のマスタのバイナリログ・ファイル名
Read_Master_Log_Pos : I/Oスレッドが読み込んだマスタのバイナリログの位置

Relay_Log_File : スレーブのSQLスレッドが現在処理中のスレーブのリレーログファイル名
Relay_Log_Pos : SQLスレッドが実行完了したスレーブのリレーログの位置

Relay_Master_Log_File : SQLスレッドが最後に実行したクエリが記録されていたマスタのバイナリログファイル名
Exec_Master_Log_Pos : SQLスレッドが最後に実行したクエリのマスタのバイナリログでの位置

Slave_IO_Running : スレーブのI/Oスレッドが稼働中か
Slave_SQL_Running : スレーブのSQLスレッドが稼働中か

Replicate_Do_DB : レプリケートするように指定されているDB名
Replicate_Ignore_DB : レプリケートしないように指定されているDB名

Last_Errno : 最後に実行したクエリのエラー番号。「0」なら成功
Last_Error : 最後に実行したクエリのエラーメッセージなど。空文字はエラー無し

Skip_Counter : 最後にSQL_SLAVE_SKIP_COUNTERを使用した時の値。使用がなければ「0」
Relay_Log_Space : 存在するリレーログファイルのサイズ。単位はバイト
Seconds_Behind_Master : I/Oスレッドに対してSQLスレッドの処理がどのくらい遅れているかを示す。単位は秒。マスタとスレーブとの間のネットワークが十分に早い状況下ならば、マスタに対してスレーブがどのくらい遅延しているかの指標になる。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Java 】サーブレット(tomcat)&MySQL&Java でコネクションプールを使用して接続してみた

参考サイト

※ めちゃくちゃ参考になった!!!
Java で MySQL に 接続する 方法
https://garafu.blogspot.com/2016/05/how-to-connect-mysql.html#con-svlt1

JDBS ドライバ導入

  1. 公式サイトからmysql-connector-java-8.0.18.zipダウンロード
  2. 解凍するしてmysql-connector-java-5.1.48-bin.jarを取り出す
  3. /WebContent/WEB-INF/lib/mysql-connector-java-5.1.48-bin.jarに入れる

context.xml を作成する

/WebContent/META-INF/context.xmlに作成する

context.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Context>
    <Resource name = "jdbc/book"
              auth = "Container"
              type = "javax.sql.DataSource"
              driverClassName = "com.mysql.jdbc.Driver"
              url      = "jdbc:mysql://localhost/book"
              username = "namari"
              password = "password">
    </Resource>
</Context>

サーブレット

/Sample/webapps/book/WEB-INF/src/chapter14/All.javaに作成する

All.java
package chapter14;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;

/**
 * Servlet implementation class All
 */
@WebServlet("/All")
public class All extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public All() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();

        try {
            // コネクション取得
            InitialContext ic = new InitialContext();
            DataSource ds = (DataSource) ic.lookup("java:/comp/env/jdbc/book");
            Connection con = ds.getConnection();

            // SQL文送信
            PreparedStatement st = con.prepareStatement("select * from product");
            // 実行&結果受け取り
            ResultSet rs = st.executeQuery();

            // データの表示
            while (rs.next()) {
                out.println(rs.getInt("id") + ":" + rs.getString("name") + ":" + rs.getInt("price"));
            }

            // データベース切断
            st.close();
            con.close();

        } catch (Exception e) {
            // 接続・SQL文エラー
            e.printStackTrace(out);

        } // try
    }

}
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

MySQLでパーティションの確認をする場合

MySQLでテーブルのパーティション設定を取得・確認するクエリ

SELECT TABLE_NAME, PARTITION_NAME, PARTITION_ORDINAL_POSITION, TABLE_ROWS
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE 
TABLE_SCHEMA = '{スキーマ名}' AND
TABLE_NAME =  '{テーブル名}';

パーティションの実行計画をとる場合はこちら

https://qiita.com/a-nishimura/items/d27c8ee31ffec73b12e6

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

BrewインストールしたMysqlが起動しない場合のクリーンインストール方法

Mysqlが起動しないので色々試してみたが、brewコマンドではどうしようもなかった件

バックアップしてなかったら意味ないんだからね!

Mysqlが起動しない

mysqlのサービス状態をチェック

Terminal
$ brew services cleanup mysql@5.7
All user-space services OK, nothing cleaned...

$  brew services list
Name      Status  User       Plist
memcached started %user_name% /Users/%user_name%/Library/LaunchAgents/homebrew.mxcl.memcached.plist
mysql@5.7 started %user_name% /Users/%user_name%/Library/LaunchAgents/homebrew.mxcl.mysql@5.7.plist
redis     stopped            

startedが黄色文字になっており、mysqlコマンドを受け付けない

仕方がないのでアンインストールを試みる

Terminal
$ brew services stop mysql@5.7
Stopping `mysql@5.7`... (might take a while)
==> Successfully stopped `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)

$  brew services list
Name      Status  User       Plist
memcached started %user_name% /Users/%user_name%/Library/LaunchAgents/homebrew.mxcl.memcached.plist
mysql@5.7 stopped            
redis     stopped            

$ brew uninstall mysql@5.7
Uninstalling /usr/local/Cellar/mysql@5.7/5.7.28... (319 files, 231.9MB)
$  brew services list
Name      Status  User       Plist
memcached started %user_name% /Users/%user_name%/Library/LaunchAgents/homebrew.mxcl.memcached.plist
redis     stopped            

Brew service からも削除され、明らかにUninstallは成功している

再インストール(あくまでアンインストールからのインストール)を試みる

Terminal
$ brew install mysql@5.7
==> Downloading https://homebrew.bintray.com/bottles/mysql@5.7-5.7.28.mojave.bottle.tar.gz
Already downloaded: /Users/%user_name%/Library/Caches/Homebrew/downloads/10cf3f69c2915bcc2ceb12e669cc50e5d854b015b7870e358b64f5f5daf176da--mysql@5.7-5.7.28.mojave.bottle.tar.gz
==> Pouring mysql@5.7-5.7.28.mojave.bottle.tar.gz
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

mysql@5.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have mysql@5.7 first in your PATH run:
  echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"

For pkg-config to find mysql@5.7 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/mysql@5.7/lib/pkgconfig"


To have launchd start mysql@5.7 now and restart at login:
  brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/mysql@5.7/bin/mysql.server start
==> Summary
?  /usr/local/Cellar/mysql@5.7/5.7.28: 319 files, 231.9MB

Summaryまで出力されているので、インストールは成功したはず

Terminal
$  brew services list
Name      Status  User       Plist
memcached started %user_name% /Users/%user_name%/Library/LaunchAgents/homebrew.mxcl.memcached.plist
mysql@5.7 stopped            
redis     stopped            

$ brew link --force mysql@5.7
Linking /usr/local/Cellar/mysql@5.7/5.7.28... 87 symlinks created

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile

$ grep mysql ~/.bash_profile 
$ echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile

$ grep mysql ~/.bash_profile 
export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.28, for osx10.14 (x86_64) using  EditLine wrapper

$ brew list | grep mysql
mysql@5.7

$ brew services start mysql@5.7
==> Successfully started `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)

$ brew services list
Name      Status  User       Plist
memcached started %user_name% /Users/%user_name%/Library/LaunchAgents/homebrew.mxcl.memcached.plist
mysql@5.7 started %user_name% /Users/%user_name%/Library/LaunchAgents/homebrew.mxcl.mysql@5.7.plist
redis     stopped            
$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

brewインストール版のMySQLはパスワードなしのはずであるのに、パスワードを求められる不思議
そして、brew servicesも相変わらずの黄色文字

mysqlコマンドはログインできるのか

Terminal
$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

はい、失敗です

どうすればいいのん?

改めてインストールしたmysqlの確認

Terminal
$ brew info mysql@5.7
mysql@5.7: stable 5.7.28 (bottled) [keg-only]
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
/usr/local/Cellar/mysql@5.7/5.7.28 (319 files, 231.9MB) *
  Poured from bottle on 2019-10-24 at 10:36:10
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mysql@5.7.rb
==> Dependencies
Build: cmake ✘
Required: openssl@1.1 ✔
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

mysql@5.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have mysql@5.7 first in your PATH run:
  echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"

For pkg-config to find mysql@5.7 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/mysql@5.7/lib/pkgconfig"


To have launchd start mysql@5.7 now and restart at login:
  brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/mysql@5.7/bin/mysql.server start
==> Analytics
install: 26,693 (30 days), 74,153 (90 days), 264,588 (365 days)
install_on_request: 26,239 (30 days), 73,549 (90 days), 263,444 (365 days)
build_error: 0 (30 days)

何故・・・

正しいクリーンインストール(あくまでアンインストールからのインストール ?くどい)の正しい手順

Terminal
$ brew services stop mysql@5.7
Stopping `mysql@5.7`... (might take a while)
==> Successfully stopped `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  qt@5.5
  imagemagick

Warning: Some installed formulae are not readable:
  qt@5.5: unknown version :mountain_lion

$ brew list mysql@5.7
/usr/local/Cellar/mysql@5.7/5.7.28/.bottle/etc/my.cnf
/usr/local/Cellar/mysql@5.7/5.7.28/bin/innochecksum
/usr/local/Cellar/mysql@5.7/5.7.28/bin/lz4_decompress
/usr/local/Cellar/mysql@5.7/5.7.28/bin/my_print_defaults
/usr/local/Cellar/mysql@5.7/5.7.28/bin/myisam_ftdump
/usr/local/Cellar/mysql@5.7/5.7.28/bin/myisamchk
/usr/local/Cellar/mysql@5.7/5.7.28/bin/myisamlog
/usr/local/Cellar/mysql@5.7/5.7.28/bin/myisampack
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql.server
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_client_test
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_client_test_embedded
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_config
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_config_editor
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_embedded
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_install_db
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_plugin
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_secure_installation
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_ssl_rsa_setup
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_tzinfo_to_sql
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysql_upgrade
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqladmin
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqlbinlog
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqlcheck
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqld
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqld_multi
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqld_safe
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqldump
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqldumpslow
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqlimport
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqlpump
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqlshow
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqlslap
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqltest
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqltest_embedded
/usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqlxtest
/usr/local/Cellar/mysql@5.7/5.7.28/bin/perror
/usr/local/Cellar/mysql@5.7/5.7.28/bin/replace
/usr/local/Cellar/mysql@5.7/5.7.28/bin/resolve_stack_dump
/usr/local/Cellar/mysql@5.7/5.7.28/bin/resolveip
/usr/local/Cellar/mysql@5.7/5.7.28/bin/zlib_decompress
/usr/local/Cellar/mysql@5.7/5.7.28/homebrew.mxcl.mysql@5.7.plist
/usr/local/Cellar/mysql@5.7/5.7.28/include/mysql/ (110 files)
/usr/local/Cellar/mysql@5.7/5.7.28/lib/libmysqlclient.20.dylib
/usr/local/Cellar/mysql@5.7/5.7.28/lib/pkgconfig/mysqlclient.pc
/usr/local/Cellar/mysql@5.7/5.7.28/lib/plugin/ (51 files)
/usr/local/Cellar/mysql@5.7/5.7.28/lib/ (4 other files)
/usr/local/Cellar/mysql@5.7/5.7.28/README-test
/usr/local/Cellar/mysql@5.7/5.7.28/share/aclocal/mysql.m4
/usr/local/Cellar/mysql@5.7/5.7.28/share/doc/ (3 files)
/usr/local/Cellar/mysql@5.7/5.7.28/share/man/ (36 files)
/usr/local/Cellar/mysql@5.7/5.7.28/share/mysql/ (61 files)
/usr/local/Cellar/mysql@5.7/5.7.28/support-files/ (4 files)

$ brew uninstall mysql@5.7
Uninstalling /usr/local/Cellar/mysql@5.7/5.7.28... (319 files, 231.9MB)

$ ls -la /usr/local/Cellar/mysql@5.7/
ls: /usr/local/Cellar/mysql@5.7/: No such file or directory

$ ls -la /usr/local/Cellar/
total 0
drwxrwxr-x  53 %user_name%  admin      1696 10 24 10:43 .
drwxr-xr-x  15 root        wheel       480  5  7 13:01 ..
-rw-r--r--   1 %user_name%  admin         0 10 23 19:48 .keepme
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 autoconf
drwxr-xr-x   3 %user_name%  600333409    96  5  9 12:54 fontconfig
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 freetype
drwxr-xr-x   3 %user_name%  600333409    96  5  9 12:54 gd
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 gdbm
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 gettext
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 git
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 glib
drwxr-xr-x   3 %user_name%  600333409    96  5  9 12:54 graphviz
drwxr-xr-x   3 %user_name%  600333409    96  5  9 12:54 gts
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 ilmbase
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:37 imagemagick
drwxr-xr-x   3 %user_name%  600333409    96  5  8 10:32 imagemagick@6
drwxr-xr-x   3 %user_name%  600333409    96  5  9 12:54 jasper
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 jpeg
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 libde265
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:34 libevent
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 libffi
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 libheif
drwxr-xr-x   3 %user_name%  600333409    96  5 16 20:44 libiconv
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 libomp
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 libpng
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 libtiff
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 libtool
drwxr-xr-x   3 %user_name%  600333409    96  5 16 20:44 libxml2
drwxr-xr-x   3 %user_name%  600333409    96  5 16 20:44 libxslt
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 little-cms2
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 memcached
drwxr-xr-x   3 %user_name%  600333409    96  5  9 12:54 netpbm
drwxr-xr-x   3 %user_name%  admin        96  5  7 15:37 nodebrew
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 openexr
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:37 openjpeg
drwxr-xr-x   4 %user_name%  600333409   128 10 23 20:26 openssl
drwxr-xr-x   3 %user_name%  600333409    96 10  4 19:00 openssl@1.1
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 pcre
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 pcre2
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 pkg-config
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 python
drwxr-xr-x   3 %user_name%  600333409    96  8  6 20:01 qt
drwxr-xr-x   3 %user_name%  600333409    96  6  6 12:58 qt@5.5
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 rbenv
drwxr-xr-x   3 %user_name%  600333409    96 10  4 19:01 readline
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:35 redis
drwxr-xr-x   3 %user_name%  admin        96 10  4 19:01 ruby-build
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 shared-mime-info
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 sqlite
drwxr-xr-x   3 %user_name%  600333409    96  5  8 13:44 v8@3.15
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:37 webp
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 x265
drwxr-xr-x   3 %user_name%  600333409    96  5  7 15:36 xz

$ mysql --help | grep my.cnf
-bash: /usr/local/bin/mysql: No such file or directory

$ which mysql

$ ls -la /usr/local/var/mysql/
Patche-Kai.local.err  ib_logfile1         mysql/              test_mysql/             test/               
ib_logfile0         ibdata1             performance_schema/ test_mysql_performance/ 

$ rm -rf /usr/local/var/mysql*

$ rm -rf /usr/local/etc/my.cnf 

$ brew install mysql@5.7
==> Downloading https://homebrew.bintray.com/bottles/mysql@5.7-5.7.28.mojave.bottle.tar.gz
Already downloaded: /Users/%user_name%/Library/Caches/Homebrew/downloads/10cf3f69c2915bcc2ceb12e669cc50e5d854b015b7870e358b64f5f5daf176da--mysql@5.7-5.7.28.mojave.bottle.tar.gz
==> Pouring mysql@5.7-5.7.28.mojave.bottle.tar.gz
==> /usr/local/Cellar/mysql@5.7/5.7.28/bin/mysqld --initialize-insecure --user=%user_name% --basedir=/usr/local/Cellar/mysql@5.7/5.7.28 --datadir=/usr/loca
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

mysql@5.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have mysql@5.7 first in your PATH run:
  echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"

For pkg-config to find mysql@5.7 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/mysql@5.7/lib/pkgconfig"


To have launchd start mysql@5.7 now and restart at login:
  brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/mysql@5.7/bin/mysql.server start
==> Summary
?  /usr/local/Cellar/mysql@5.7/5.7.28: 319 files, 231.9MB

$ ls -ltr /usr/local/var/mysql/
total 221264
-rw-r-----    1 %user_name%  admin  50331648 10 24 10:45 ib_logfile1
-rw-r-----    1 %user_name%  admin        56 10 24 10:45 auto.cnf
-rw-------    1 %user_name%  admin      1676 10 24 10:45 ca-key.pem
-rw-r--r--    1 %user_name%  admin      1112 10 24 10:45 ca.pem
-rw-------    1 %user_name%  admin      1680 10 24 10:45 server-key.pem
-rw-r--r--    1 %user_name%  admin      1112 10 24 10:45 server-cert.pem
-rw-------    1 %user_name%  admin      1676 10 24 10:45 client-key.pem
-rw-r--r--    1 %user_name%  admin      1112 10 24 10:45 client-cert.pem
-rw-------    1 %user_name%  admin      1676 10 24 10:45 private_key.pem
-rw-r--r--    1 %user_name%  admin       452 10 24 10:45 public_key.pem
drwxr-x---   90 %user_name%  admin      2880 10 24 10:45 performance_schema
drwxr-x---   77 %user_name%  admin      2464 10 24 10:45 mysql
drwxr-x---  108 %user_name%  admin      3456 10 24 10:45 sys
-rw-r-----    1 %user_name%  admin       425 10 24 10:45 ib_buffer_pool
-rw-r-----    1 %user_name%  admin  50331648 10 24 10:45 ib_logfile0
-rw-r-----    1 %user_name%  admin  12582912 10 24 10:45 ibdata1


$ mysql.server start
-bash: mysql.server: command not found

# そりゃ、シムリンクせずにmysqlコマンド打ってもね(だってばよ)
$ brew link --force mysql@5.7
Linking /usr/local/Cellar/mysql@5.7/5.7.28... 87 symlinks created

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile

$ mysql.server start
Starting MySQL
. SUCCESS! 

はい成功しました

Terminal
$ view /usr/local/var/mysql/Patche-Kai.local.err 

$ netstat -a
Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0  192.168.0.5.53927   a99-999-999-99.d.https ESTABLISHED
tcp4       0      0  192.168.0.5.53926   a99-999-999-99.d.https ESTABLISHED
tcp4       0      0  192.168.0.5.53925   a99-999-999-99.d.https ESTABLISHED
tcp4       0      0  localhost.mysql        *.*                    LISTEN     
tcp4       0      0  localhost.ipp          *.*                    LISTEN     
tcp6       0      0  localhost.ipp          *.*                    LISTEN     
tcp4       0      0  192.168.0.5.53924   lb-999-99-999-99.https ESTABLISHED
tcp4       0      0  192.168.0.5.53923   server-99-999-99.https ESTABLISHED
tcp4       0      0  192.168.0.5.53922   nrt99s99-in-f99..https ESTABLISHED
tcp4       0      0  192.168.0.5.53921   nrt99s99-in-f99..https ESTABLISHED
tcp4       0      0  192.168.0.5.53887   server-99-999-99.https ESTABLISHED
tcp4       0      0  192.168.0.5.53819   lb-999-99-999-99.https ESTABLISHED
tcp4       0      0  192.168.0.5.53699   999.999.999.99.https   ESTABLISHED
tcp4       0      0  192.168.0.5.53687   999.999.999.9.https    ESTABLISHED
tcp4       0      0  192.168.0.5.53470   999.999.999.999.https  ESTABLISHED
tcp4       0      0  192.168.0.5.53417   lb-999-99-999-99.https ESTABLISHED
^C

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28 Homebrew

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> quit
Bye

はい接続も成功です

Terminal
$ mysql.server stop
Shutting down MySQL
.. SUCCESS! 

$ brew services list
Name      Status  User       Plist
memcached started %user_name% /Users/%user_name%/Library/LaunchAgents/homebrew.mxcl.memcached.plist
mysql@5.7 stopped            
redis     stopped            

$ mysql.server start
Starting MySQL
. SUCCESS! 
$ brew services list
Name      Status  User       Plist
memcached started %user_name% /Users/%user_name%/Library/LaunchAgents/homebrew.mxcl.memcached.plist
mysql@5.7 stopped            
redis     stopped            

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28 Homebrew

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> create database test_exception_mysql;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'test_local'@'localhost' IDENTIFIED WITH mysql_native_password AS 'pattchepatchen1sh1teyany0';
ERROR 1827 (HY000): The password hash doesn't have the expected format. Check if the correct password algorithm is being used with the PASSWORD() function.
mysql> CREATE USER 'test_local'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pattchepatchen1sh1teyany0';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON test_exception_mysql.* TO 'test_local'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

$ mysql -utest_local -p test_exception_mysql < test_exception_mysql_performance.sql
Enter password: 
$ mysql -utest_local -p test_exception_mysql
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 Homebrew

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 

言い忘れていましたが、クラッシュする前にmysqldumpをしていた為、今回は大事に至りませんでした。
しかし、やはりバックアップはきちんと取っておきましょう。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【エラー】bundle実行時にmysql2がインストールされない

環境

  • Mac OS
  • Ruby 2.6.3
  • Rails 5.2.3
  • MySQL 8.0

エラー内容

Dockerを使ってRails+MySQLでの環境構築をした後に
bundle installを実行すると

An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'`
succeeds before bundling.

といったような
mysql2がインストールできないというエラーが発生しました。

対処方法

$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl/include"
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"

上記二つのコマンドを実行し、
bundlerの設定をするとエラーが解決できて
無事mysql2がインストールできました!

感じたこと

エラー発生時なかなか解決できず困りましたが
とにかく色々と調べた結果上記の対処方法で解決できました!(汗)

Dockerを使って新しく環境構築するとこのエラーが発生するので
環境構築をし終えたら毎度このコマンドを実行してます!

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

勉強ログ_day2_データベース入門①

はじめに

非エンジニアの勉強ログ。
超初心者プレイのため、内容は自分の勉強メモです。

今日のゴール

  • MySQLとやらに触ってみる

環境

  • ホストOS:Windows10 Pro 1903
  • こちらで構築した環境を利用する
  • MySQL:

参考資料・引用元など

データベースについて

  • データを格納するために用意したサーバ
  • 今回はRDBを学ぶ。リレーショナルデータベース
    • 行:レコード
    • 列:フィールド/カラム
  • データベースの中身を扱う言語を「SQL(エスキューエル)」と言う
  • 取り扱うデータベースの種類によって若干言語の書き方がかわる。今回は「MySQL」
    • 行のうしろがセミコロン「;」で終わる。忘れないように注意
    • 大文字小文字が区別されない

MySQLサーバに接続する

  • 作成済みの開発環境でMySQLサーバが動いていた なぜだ わからん(sudo service mysqld status で確認した)
    • 確認したら、ドットインストールの途中でパッケージで色々入れていた
  • MySQLサーバへの接続終了:\q

MySQLをコマンドでいじる

データベース一覧の表示

mysql-shell
mysql > show databases;

データベースの作成

mysql-shell
mysql > create database testdb01;

データベースの削除 !!!危険な作業だから、間違えないよう注意!!! 

mysql-shell
mysql > drop database testdb01;

あとがき

  • Qiitaで下書きが出来ることに気づいたので、昨日フォーマット作って保存した
  • GUIでしかデータベース見たことなかった。CUIで見るとこんな感じなんだね
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む