この記事は MySQLの限界に挑戦 Advent Calendar の5日目の記事です。
昨日の @sakaik さんの MySQLの限界に挑戦:長く寝る~SLEEP() - sakaikの日々雑感~(T)編 を読んで思い出しました。
MySQLの5.7とそれ以前には「起きていられる限界」の時間が存在しました(というか、26.10.0.1もそうだろうと思ってやり始めたら案外その限界を突破していたので、調べてみたらMySQL 8.0.46とそれ以降ではこの挙動は起こらなくなっています)
さあ、まずはMySQL 5.7をインストールし…ようと思ったんですが、MySQL 5.7はEL 7系まででしかサポートされていません。
しかも運悪く、このブログを書いている現在 dev.mysql.com が不安定で新しくダウンロードするのは難しそうです。
仕方ない、普段使い(?) のソースコードからビルドしたMySQL 5.7を使うことにします。
$ pwd
/usr/mysql/5.7.44
$ ll
total 284
-rw-r--r--. 1 yoku0825 yoku0825 260678 Oct 11 2023 LICENSE
-rw-r--r--. 1 yoku0825 yoku0825 566 Oct 11 2023 README
-rw-r--r--. 1 yoku0825 yoku0825 566 Oct 11 2023 README-test
drwxr-xr-x. 2 yoku0825 yoku0825 4096 Jan 23 2025 bin
drwxr-x---. 9 yoku0825 yoku0825 4096 Oct 20 10:00 data
drwxr-xr-x. 2 yoku0825 yoku0825 73 Jan 23 2025 docs
drwxr-xr-x. 3 yoku0825 yoku0825 4096 Jan 23 2025 include
drwxr-xr-x. 4 yoku0825 yoku0825 192 Jan 23 2025 lib
drwxr-xr-x. 4 yoku0825 yoku0825 30 Jan 23 2025 man
drwxr-xr-x. 10 yoku0825 yoku0825 4096 Jan 23 2025 mysql-test
drwxr-xr-x. 28 yoku0825 yoku0825 4096 Jan 23 2025 share
drwxr-xr-x. 2 yoku0825 yoku0825 90 Jan 23 2025 support-files
$ bin/mysqld --version
bin/mysqld Ver 5.7.44 for Linux on x86_64 (Source distribution)
タネも仕掛けもないように、datadirを消して再初期化しておきます。
$ rm -r data
$ bin/mysqld --no-defaults --initialize-insecure
2026-12-05T13:24:03.877854Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2026-12-05T13:24:04.173431Z 0 [Warning] InnoDB: New log files created, LSN=45790
2026-12-05T13:24:04.221974Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2026-12-05T13:24:04.280698Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 2438467b-f0af-11f1-889e-02001702f486.
2026-12-05T13:24:04.282194Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2026-12-05T13:24:04.380230Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2026-12-05T13:24:04.380243Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2026-12-05T13:24:04.380660Z 0 [Warning] CA certificate ca.pem is self signed.
2026-12-05T13:24:04.410189Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
$ bin/mysqld --no-defaults --daemonize --log-error=./error.log
$ ps auxwww | grep mysqld
yoku0825 963735 0.6 1.1 1909084 177368 ? Sl 13:24 0:00 bin/mysqld --no-defaults --daemonize --log-error=./error.log
yoku0825 963902 0.0 0.0 9292 1224 pts/4 S+ 13:25 0:00 grep --color=auto mysqld
無事に起動してきました。
$ bin/mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 Source distribution
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> SELECT @@version, NOW();
+-----------+---------------------+
| @@version | NOW() |
+-----------+---------------------+
| 5.7.44 | 2026-12-05 13:25:29 |
+-----------+---------------------+
1 row in set (0.00 sec)
さてこのタネも仕掛けもないMySQL。昨日の坂井さんの例に倣ってちょっと10年ばかり眠ってもらいましょう。
mysql> SELECT NOW(); SELECT SLEEP(60 * 60 * 24 * 365 * 10); SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2026-12-05 13:26:24 |
+---------------------+
1 row in set (0.00 sec)
眠りました。
私は坂井さんほど気が長くないので、10年待つことよりも現在を10年先に飛ばすことにしました。
$ sudo timedatectl set-time "2036-12-05 22:28:00"
$ date
Fri Dec 5 22:28:01 GMT 2036
無事に飛べたようです。MySQLの様子を見に行ってみましょう。
mysql> SELECT NOW(); SELECT SLEEP(60 * 60 * 24 * 365 * 10); SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2026-12-05 13:26:24 |
+---------------------+
1 row in set (0.00 sec)
+--------------------------------+
| SLEEP(60 * 60 * 24 * 365 * 10) |
+--------------------------------+
| 0 |
+--------------------------------+
1 row in set (1 min 59.62 sec)
+---------------------+
| NOW() |
+---------------------+
| 2036-12-05 22:28:00 |
+---------------------+
1 row in set (0.00 sec)
無事に眠りから覚めていました。
MySQLのSLEEP関数は「眠り始める時点」で「目を覚ます時間」を計算し、OSの時刻がその時間になった瞬間に目を覚まします。
しかし、 mysql コマンドラインクライアント側は「クエリを投げてから戻ってきた時点までの実時間」をどうにか取得しているようで、 1 row in set (1 min 59.62 sec) と返ってきています。
無事に目を覚ましてすぐで申し訳ないんですが、もう10年ほど眠ってもらいましょう。
mysql> SELECT NOW(); SELECT SLEEP(60 * 60 * 24 * 365 * 10); SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2036-12-05 22:30:11 |
+---------------------+
1 row in set (0.00 sec)
私はゆったりと10年後に飛びます。
$ sudo timedatectl set-time "2046-12-05 22:30:00"
$ date
Wed Dec 5 22:30:00 GMT 2046
MySQLも眠りから覚めたようですが、何か様子がおかしそうです。
mysql> SELECT NOW(); SELECT SLEEP(60 * 60 * 24 * 365 * 10); SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2036-12-05 22:30:11 |
+---------------------+
1 row in set (0.00 sec)
+--------------------------------+
| SLEEP(60 * 60 * 24 * 365 * 10) |
+--------------------------------+
| 0 |
+--------------------------------+
1 row in set (22.31 sec)
ERROR 2013 (HY000): Lost connection to MySQL server during query
psコマンドで確認すると、mysqldごと落ちているのがわかります。
$ ps auxwww | grep mysqld
yoku0825 966707 0.0 0.0 9292 1212 pts/4 S+ 22:30 0:00 grep --color=auto mysqld
エラーログを見てみましょう。
$ less data/error.log
..
2026-12-05T13:24:35.691192Z 0 [Note] Event Scheduler: Loaded 0 events
2026-12-05T13:24:35.691354Z 0 [Note] bin/mysqld: ready for connections.
Version: '5.7.44' socket: '/tmp/mysql.sock' port: 3306 Source distribution <--- ここまでが起動時のログ
2046-12-05T22:30:00.003575Z 2 [Warning] Current time has got past year 2038. Validating current time with 5 iterations before initiating the normal server shutdow
n process.
2046-12-05T22:30:00.003609Z 2 [Warning] Iteration 1: Current time obtained from system is greater than 2038
2046-12-05T22:30:00.003612Z 2 [Warning] Iteration 2: Current time obtained from system is greater than 2038
2046-12-05T22:30:00.003615Z 2 [Warning] Iteration 3: Current time obtained from system is greater than 2038
2046-12-05T22:30:00.003617Z 2 [Warning] Iteration 4: Current time obtained from system is greater than 2038
2046-12-05T22:30:00.003627Z 2 [Warning] Iteration 5: Current time obtained from system is greater than 2038
2046-12-05T22:30:00.003629Z 2 [ERROR] This MySQL server doesn't support dates later than 2038
2046-12-05T22:30:00.009162Z 0 [Note] Giving 0 client threads a chance to die gracefully
2046-12-05T22:30:00.009182Z 0 [Note] Shutting down slave threads
2046-12-05T22:30:00.009189Z 0 [Note] Forcefully disconnecting 0 remaining clients
2046-12-05T22:30:00.009194Z 0 [Note] Event Scheduler: Purging the queue. 0 events
2046-12-05T22:30:00.009241Z 0 [Note] Binlog end
..
2046-12-05T22:30:01.117737Z 0 [Note] bin/mysqld: Shutdown complete
Current time has got past year 2038 .. そうです。2038年問題です。
ソースコード的にはここ。
https://github.com/mysql/mysql-server/blob/mysql-5.7.44/sql/sql_parse.cc#L1285
起動しようと思っても、5.7はもう二度と起動しません。
$ bin/mysqld --no-defaults --daemonize --log-error=./error.log
2046-12-05T22:37:05.990160Z 0 [ERROR] This MySQL server doesn't support dates later than 2038
2046-12-05T22:37:05.990401Z 0 [ERROR] Aborting
2046-12-05T22:37:05.990456Z 0 [Note] Binlog end
Initialization of mysqld failed: 0
https://github.com/mysql/mysql-server/blob/mysql-5.7.44/sql/mysqld.cc#L2750
ちなみにこの動作、少なくとも8.0.25では再現していたようです。
2038年1月19日3時14分8秒 UTCを過ぎると、次のSQLをパースしたところでmysqldが落ちる
ざっとblameでたどってみたところ、どうやらMySQL 8.0.28とそれ以降では32bit timestampに対応していない場合のみが↑の動作になり、64bit timestampに対応しているなら普通に動作するようになったようです。
WL#14630 Make UNIX_TIMESTAMP, FROM_UNIXTIME work with 64 bits time wh… · mysql/mysql-server@bddc5bc
というわけで、MySQL 8.0.27とそれ以前は塩漬けするにしても 2038年1月19日3時14分8秒 UTC という活動限界があるのでした。
この記事が誰の役にも立たないことを祈ります。
明日は @sakaikさん の「数を数える限界に挑戦~AUTO_INCREMENT」です!

0 件のコメント :
コメントを投稿