9. 起動
service mysqld startのタイミングでmysqld --initialize
(mysql_install_dbの後継)が⾛る
# service mysqld start
Initializing MySQL database: 2015-10-19T04:47:16.696030Z 0 [Warn
ing] TIMESTAMP with implicit DEFAULT value is deprecated. Please
use --explicit_defaults_for_timestamp server option (see document
ation for more details).
8/103
10. 接続…︖
# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (us
ing password: NO)
# ls -l ~/.mysql_secret
ls: cannot access /root/.mysql_secret: No such file or directory
9/103
12. パスワードの在り処
# grep log-error /etc/my.cnf
log-error=/var/log/mysqld.log
# grep password /var/log/mysqld.log
2015-10-19T04:47:17.752156Z 1 [Warning] A temporary password is g
enerated for root@localhost: jM*C.2dAc6dh
2015-10-19T04:47:22.244470Z 2 [Note] Access denied for user 'UNKN
OWN_MYSQL_USER'@'localhost' (using password: NO)
2015-10-19T04:47:36.374844Z 3 [Note] Access denied for user 'root
'@'localhost' (using password: NO)
11/103
13. ログインはできたけれど
# mysql -p
Enter password:
mysql> status
ERROR 1820 (HY000): You must reset your password using ALTER USE
R statement before executing this statement.
12/103
16. validate_password= OFFを/etc/my.cnfに書くやり⽅
# vim /etc/my.cnf
[mysqld]
validate_password= OFF
# service mysqld restart
# mysql -p
Enter password:
mysql> SET PASSWORD= '';
Query OK, 0 rows affected (0.00 sec)
15/103
23. MySQL 5.7の全⽂検索
「全ツイート履歴をダウンロード」してきた。
mysql> CREATE TABLE tweet ( tweet_id BIGINT UNSIGNED PRIMARY KE
Y, timestamp DATETIME NOT NULL, text TEXT);
mysql> LOAD DATA INFILE '/tmp/tweets.csv' INTO TABLE tweet FIELD
S TERMINATED BY ',' ENCLOSED BY '"' IGNORE 1 LINES (tweet_id, @d
ummy, @dummy, @timestamp, @dummy, text, @dummy) SET timestamp= DA
TE_ADD(@timestamp, INTERVAL 9 HOUR);
ERROR 1290 (HY000): The MySQL server is running with the --secure
-file-priv option so it cannot execute this statement
22/103
25. sql_modeが強化
ツイート履歴はʼyyyy/mm/dd hh:nn:ss +TZʼな形式のタイ
ムスタンプを持っているので、MySQLがそのままは⾷えな
い。
ʻ+TZʼの部分を無理⽮理読み⾶ばして9時間⾜してやること
で5.6では何とかなったが
mysql> LOAD DATA INFILE '/var/lib/mysql-files/tweets.csv' INTO TABLE twe
et FIELDS TERMINATED BY ',' ENCLOSED BY '"' IGNORE 1 LINES (tweet_id, @
dummy, @dummy, @timestamp, @dummy, text, @dummy) SET timestamp= DATE_ADD
(@timestamp, INTERVAL 9 HOUR);
ERROR 1292 (22007): Truncated incorrect datetime value: '2015-10-19 05:4
6:24 +0000'
mysql> SELECT @@sql_modeG
*************************** 1. row ***************************
@@sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZE
RO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTIT
UTION
1 row in set (0.00 sec)
24/103
26. 申し訳ないが取り敢えず非strictにしよう
ホントはなんかスクリプト書けばそれで済むんだけど⾯
倒。。
mysql> SET sql_mode= '';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> LOAD DATA INFILE '/var/lib/mysql-files/tweets.csv' INTO TA
BLE tweet FIELDS TERMINATED BY ',' ENCLOSED BY '"' IGNORE 1 LINE
S (tweet_id, @dummy, @dummy, @timestamp, @dummy, text, @dummy) SE
T timestamp= DATE_ADD(@timestamp, INTERVAL 9 HOUR);
Query OK, 28051 rows affected, 56102 warnings (0.52 sec)
Records: 28051 Deleted: 0 Skipped: 0 Warnings: 56102
mysql> SET sql_mode= default;
Query OK, 0 rows affected (0.00 sec)
25/103
27. ngramパーサーは最初から有効になってる
mysql> ALTER TABLE tweet ADD FULLTEXT KEY idx_ngram(text) WITH PA
RSER ngram;
Query OK, 0 rows affected, 1 warning (4.22 sec)
Records: 0 Duplicates: 0 Warnings: 1
26/103
28. LIKEより遅くて草。。
mysql> SELECT COUNT(*) FROM tweet WHERE text LIKE '%mysql%';
+----------+
| COUNT(*) |
+----------+
| 6265 |
+----------+
1 row in set (0.05 sec)
mysql> SELECT COUNT(*) FROM tweet WHERE match(text) against ('mys
ql' IN BOOLEAN MODE);
+----------+
| COUNT(*) |
+----------+
| 6265 |
+----------+
1 row in set (0.18 sec)
27/103
29. 転置索引の中⾝をのぞいてみる
mysql> SET GLOBAL innodb_ft_aux_table= 'd1/tweet';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT word, count(*) AS c FROM information_schema.INNODB_FT_INDE
X_TABLE GROUP BY word ORDER BY c DESC LIMIT 10;
+--------+------+
| word | c |
+--------+------+
| my | 9409 |
| sq | 9353 |
| ql | 9346 |
| ys | 8866 |
| co | 8024 |
| って | 7957 |
| er | 6614 |
| 。。 | 5997 |
| tt | 5874 |
| ht | 5715 |
+--------+------+
10 rows in set (8.84 sec)
28/103
37. 取り敢えずCOUNT
mysql> SELECT COUNT(*) FROM tweet WHERE text LIKE '%mysql%';
+----------+
| COUNT(*) |
+----------+
| 6265 |
+----------+
1 row in set (0.04 sec)
mysql> SELECT COUNT(*) FROM tweet FORCE INDEX(idx_mecab) WHERE ma
tch(text) against ('mysql' IN BOOLEAN MODE);
+----------+
| COUNT(*) |
+----------+
| 5585 |
+----------+
1 row in set (0.00 sec)
36/103
38. 転置索引
mysql> SET GLOBAL innodb_ft_aux_table= 'd1/tweet';
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE tweet DROP KEY idx_ngram; -- このままだとngramとMeCabの転置
索引が混じるのでドロップ
Query OK, 0 rows affected (0.45 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT word, count(*) AS c FROM information_schema.INNODB_FT_INDEX_TABLE
GROUP BY word ORDER BY c DESC LIMIT 10;
+-----------+------+
| word | c |
+-----------+------+
| mysql | 7374 |
| :// | 5414 |
| http | 4629 |
| innodb | 1028 |
| という | 1016 |
| dbts | 883 |
| https | 883 |
| rkajiyama | 797 |
| mariadb | 795 |
| groonga | 681 |
+-----------+------+
10 rows in set (0.47 sec)
37/103
41. オフラインモードを試すためにSuperを持ってないユーザ
ーを作る
t';
Query OK, 0 rows affected, 1 warning (0.07 sec)
mysql> SHOW WARNINGSG
*************************** 1. row ***************************
Level: Warning
Code: 1287
oved in futu
re release. Create new user with CREATE USER statement.
1 row in set (0.00 sec)
mysql> SHOW GRANTS FOR yoku0825@localhost;
+-----------------------------------------------------------------+
| Grants for yoku0825@localhost |
+-----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'yoku0825'@'localhost' |
| GRANT ALL PRIVILEGES ON `mysqlslap`.* TO 'yoku0825'@'localhost' |
+-----------------------------------------------------------------+
2 rows in set (0.00 sec)
40/103
42. MySQL 5.7(あるいはsql_mode= NO_AUTO_CREATE_USER)に
怒られないユーザーの作成⽅法
mysql> CREATE USER yoku0826@localhost IDENTIFIED BY 'test';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL ON mysqlslap.* TO yoku0826@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GRANTS FOR yoku0826@localhost;
+-----------------------------------------------------------------+
| Grants for yoku0826@localhost |
+-----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'yoku0826'@'localhost' |
| GRANT ALL PRIVILEGES ON `mysqlslap`.* TO 'yoku0826'@'localhost' |
+-----------------------------------------------------------------+
2 rows in set (0.00 sec)
41/103
43. mysqlslapで負荷かけながらオフラインモードにしてみる
$ mysqlslap --no-drop --auto-generate-sql --auto-generate-sql-exe
cute-number=100000 -uyoku0826 -ptest
mysqlslap: [Warning] Using a password on the command line interfa
ce can be insecure.
mysql> SET GLOBAL offline_mode= ON;
Query OK, 0 rows affected (0.00 sec)
mysqlslap: Error when storing result: 2013 Lost connection to MyS
QL server during query
mysqlslap: Cannot run query INSERT INTO t1 VALUES (95275444,'bNIr
BDBl81tjzdvuOpQRCXgX37xGtzLKEXBIcE3k7xK7aFtqxC99jqYnpTviK83bf6lGD
gsKd4R3KLmHPnI8TqnIKj1gjw7N2sXFZNS2Svyg8cpZN7atxL39w4igsp') ERRO
R : MySQL server has gone away
42/103
44. オフラインモードでもう⼀度mysqlslapしてみる
$ mysqlslap --no-drop --auto-generate-sql --auto-generate-sql-exe
cute-number=100000 -uyoku0826 -ptest
mysqlslap: [Warning] Using a password on the command line interfa
ce can be insecure.
mysqlslap: Error when connecting to server: The server is current
ly in offline mode
43/103
47. InnoDBのバッファプールサイズオンライン変更(MySQL
側, 増やす)
mysql> SET GLOBAL innodb_buffer_pool_size= 32 * 1024 * 1024 * 1024;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW STATUS LIKE '%resize%';
+----------------------------------+----------------------------------+
| Variable_name | Value |
+----------------------------------+----------------------------------+
| Innodb_buffer_pool_resize_status | Resizing also other hash tables. |
+----------------------------------+----------------------------------+
1 row in set (0.00 sec)
mysql> SHOW STATUS LIKE '%resize%';
+----------------------------------+----------------------------------------------------+
| Variable_name | Value |
+----------------------------------+----------------------------------------------------+
| Innodb_buffer_pool_resize_status | Completed resizing buffer pool at 151019 10:00:03. |
+----------------------------------+----------------------------------------------------+
1 row in set (0.00 sec)
46/103
71. 範囲検索でORDER BYまでキーが使えないやつも
mysql> ALTER TABLE Country ADD KEY (population, gnp);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain SELECT Name FROM Country WHERE Population > 100000000 ORD
ER BY GNP DESC;
+----+-------------+---------+------------+-------+---------------+-----
-------+---------+------+------+----------+-----------------------------
----------+
| id | select_type | table | partitions | type | possible_keys | ke
y | key_len | ref | rows | filtered | Extr
a |
+----+-------------+---------+------------+-------+---------------+-----
-------+---------+------+------+----------+-----------------------------
----------+
| 1 | SIMPLE | Country | NULL | range | Population | Popu
lation | 4 | NULL | 10 | 100.00 | Using index condition; Usin
g filesort |
+----+-------------+---------+------------+-------+---------------+-----
-------+---------+------+------+----------+-----------------------------
----------+
1 row in set, 1 warning (0.00 sec)
70/103
72. generated columnならこの通り
mysql> ALTER TABLE Country ADD is_over_100mil tinyint AS (IF(Population > 100000
00, 1, 0)), ADD KEY (is_over_100mil, GNP);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain SELECT Name FROM Country WHERE is_over_100mil = 1 ORDER BY GNP DE
SC;
+----+-------------+---------+------------+------+----------------+-------------
---+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | ke
y | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+----------------+-------------
---+---------+-------+------+----------+-------------+
| 1 | SIMPLE | Country | NULL | ref | is_over_100mil | is_over_100m
il | 2 | const | 78 | 100.00 | Using where |
+----+-------------+---------+------------+------+----------------+-------------
---+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
71/103
73. 昇順と降順が混じったソートも
mysql> ALTER TABLE Country ADD KEY (is_over_100mil, GNP, GNPOld);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain SELECT Name FROM Country USE INDEX(is_over_100mil_2) WHERE is_ove
r_100mil = 1 ORDER BY GNP ASC, GNPOld DESC LIMIT 1;
+----+-------------+---------+------------+------+------------------+-----------
-------+---------+-------+------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | ke
y | key_len | ref | rows | filtered | Extr
a |
+----+-------------+---------+------------+------+------------------+-----------
-------+---------+-------+------+----------+-----------------------------+
| 1 | SIMPLE | Country | NULL | ref | is_over_100mil_2 | is_over_10
0mil_2 | 2 | const | 78 | 100.00 | Using where; Using filesort |
+----+-------------+---------+------------+------+------------------+-----------
-------+---------+-------+------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)
72/103
74. generated columnならこの通り
mysql> ALTER TABLE Country ADD invert_gnpold float(10, 2) AS (0 - GNPOld), ADD K
EY (is_over_100mil, GNP, invert_gnpold);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain SELECT Name FROM Country WHERE is_over_100mil = 1 ORDER BY GNP AS
C, invert_gnpold ASC LIMIT 1;
+----+-------------+---------+------------+------+------------------------------
--------------------+------------------+---------+-------+------+----------+----
---------+
| id | select_type | table | partitions | type | possible_key
s | key | key_len | ref | row
s | filtered | Extra |
+----+-------------+---------+------------+------+------------------------------
--------------------+------------------+---------+-------+------+----------+----
---------+
| 1 | SIMPLE | Country | NULL | ref | is_over_100mil,is_over_100mil
_2,is_over_100mil_3 | is_over_100mil_3 | 2 | const | 78 | 100.00 | Usi
ng where |
+----+-------------+---------+------------+------+------------------------------
--------------------+------------------+---------+-------+------+----------+----
---------+
1 row in set, 1 warning (0.00 sec)
73/103
75. MySQLにないCHECK制約もgenerated columnならこ
の通り
mysql> ALTER TABLE Country ADD v_check tinyint AS (IF(GNPOld <
1, NULL, 1)) NOT NULL;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> INSERT INTO Country SET Name= 'Dummy', GNPOld= 0.01;
ERROR 1048 (23000): Column 'v_check' cannot be null
mysql> INSERT INTO Country SET Name= 'Dummy', GNPOld= 1000.01;
Query OK, 1 row affected (0.00 sec)
74/103
84. GTIDは振られない(が、GTIDが有効なマスターに接続し
ても⽂句は⾔わない)
# at 2748010
#151021 11:06:35 server id 1 end_log_pos 2748075 CRC32 0x367e304
1 Anonymous_GTID last_committed=6992 sequence_number=69
93
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2748075
#151021 11:06:35 server id 1 end_log_pos 2748152 CRC32 0x916da7a
5 Query thread_id=16 exec_time=0 error_code=0
SET TIMESTAMP=1445393195/*!*/;
BEGIN
/*!*/;
..
# at 2748372
#151021 11:06:35 server id 1 end_log_pos 2748403 CRC32 0x5fa8c12
b Xid = 51208
COMMIT/*!*/;
83/103
86. GTIDを振るようになる(が、GTIDが有効でないスレーブ
がいても⽂句は⾔わない)
# at 997588
#151021 11:09:16 server id 1 end_log_pos 997653 CRC32 0x985a4456
GTID last_committed=2538 sequence_number=2539
SET @@SESSION.GTID_NEXT= '00013253-1111-1111-1111-111111111111:2539'/*!
*/;
# at 997653
#151021 11:09:16 server id 1 end_log_pos 997730 CRC32 0xed08a25b
Query thread_id=15 exec_time=0 error_code=0
SET TIMESTAMP=1445393356/*!*/;
BEGIN
/*!*/;
..
# at 997950
#151021 11:09:16 server id 1 end_log_pos 997981 CRC32 0xc3bfdb4c
Xid = 68724
COMMIT/*!*/;
85/103
88. gtid_mode= ONの前に
# ./m -e "SET GLOBAL gtid_mode= ON"
ERROR 3111 (HY000) at line 1: SET @@GLOBAL.GTID_MODE = ON is no
t allowed because ENFORCE_GTID_CONSISTENCY is not ON.
# ./use_all "SELECT @@enforce_gtid_consistency"
# master
@@enforce_gtid_consistency
OFF
# server: 1:
@@enforce_gtid_consistency
OFF
# server: 2:
@@enforce_gtid_consistency
OFF
87/103
101. 参考URL
MySQL :: MySQL 5.7 Reference Manual :: 1.4 What Is
New in MySQL 5.7
MySQL :: MySQL 5.7 Release Notes
Complete list of new features in MySQL 5.7
中の⼈が作った「新機能完全リスト」-
MySQL 5.7の新機能完全リスト | Yakst
↑をがんばって⽇本語に訳したヤーツ-
⽇々の覚書: 5.7
yoku0825ʼs Presentations on SlideShare
100/103