WordPress というものを使いたいので頑張る。まずは MySQL サーバーをインストールする。
(1) MySQL インストール
普通に apt-get する。途中で (Linux の root じゃなく) MySQL 自体の root のパスワードを設定するかどうかと聞かれるので、素直に設定しておく (余談だが MySQL の root パスワードは Linux の root パスワードと一緒にしないほうが良いんだろうとは思う) 。これが完了すると、ローカルに MySQL サーバーが走るようになる。
<<VPS 上での作業>> $ sudo apt-get install mysql-server
(2) UTF-8 化
次に文字コードを統一する。ローカルで動いている (ハズの) MySQL サーバーへユーザー root でアクセス (この root は MySQL 自体の root の意味なので、それをコマンドを動かすには普通に一般ユーザー権限で良い) 。 status を実行すると、デフォルトが latin1 (ISO-8859-1) になっているのがわかる。
<<VPS>> $ mysql -u root -p Enter password: ************ Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 6 Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2016, 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> status -------------- mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper Connection id: 6 Current database: Current user: root@localhost SSL: Not in use Current pager: /usr/bin/less Using outfile: '' Using delimiter: ; Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: latin1 Conn. characterset: latin1 UNIX socket: /var/run/mysqld/mysqld.sock Uptime: 12 min 44 sec Threads: 1 Questions: 13 Slow queries: 0 Opens: 106 Flush tables: 1 Open tables: 99 Queries per second avg: 0.017 -------------- mysql>
これらを UTF-8 にしたいわけだ。修正対象となる設定ファイルは /etc/mysql の下にある 3 つ :
- /etc/mysql/mysql.conf.d/mysqld.cnf : 「 character-set-server=utf8 」を追加
- /etc/mysql/conf.d/mysql.cnf : 「 default-character-set=utf8 」を追加
- /etc/mysql/conf.d/mysqldump.cnf : 「 default-character-set=utf8 」を追加
変更内容は以下 (diff -u 形式) :
--- /etc/mysql/mysql.conf.d/mysqld.cnf.ORG 2017-01-17 20:21:07.679658966 +0900 +++ /etc/mysql/mysql.conf.d/mysqld.cnf 2017-01-17 20:56:39.543630291 +0900 @@ -25,6 +25,7 @@ nice = 0 [mysqld] +character-set-server=utf8 # # * Basic Settings #
--- /etc/mysql/conf.d/mysql.cnf.ORG 2017-01-17 20:29:49.040879407 +0900 +++ /etc/mysql/conf.d/mysql.cnf 2017-01-17 20:30:06.811852815 +0900 @@ -1 +1,2 @@ [mysql] +default-character-set=utf8
--- /etc/mysql/conf.d/mysqldump.cnf.ORG 2017-01-17 20:28:55.034960399 +0900 +++ /etc/mysql/conf.d/mysqldump.cnf 2017-01-17 20:56:56.531606297 +0900 @@ -1,4 +1,5 @@ [mysqldump] +default-character-set=utf8 quick quote-names max_allowed_packet = 16M
これらを編集したら MySQL サーバーをリスタートさせ、デフォルトの文字コードが UTF-8 で統一できたのを確認しておく。
$ sudo /etc/init.d/mysql restart [ ok ] Restarting mysql (via systemctl): mysql.service. $ mysql -u root -p Enter password: ************ Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 3 Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2016, 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> status -------------- mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper Connection id: 3 Current database: Current user: root@localhost SSL: Not in use Current pager: /usr/bin/less Using outfile: '' Using delimiter: ; Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/run/mysqld/mysqld.sock Uptime: 1 min 19 sec Threads: 1 Questions: 5 Slow queries: 0 Opens: 105 Flush tables: 1 Open tables: 98 Queries per second avg: 0.063 -------------- mysql> show variables like "chara%"; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.01 sec) mysql>
(3) 初期化
改めて mysql_secure_installation で MySQL を初期化。途中の質問は読んで決めればいいけれども、どれも安全面に関する質問のようなので、ほとんど yes で良いんじゃないかなと思う (実際そうした) 。
註 : mysql_secure_installation を起動するとすぐに「 default-character-set=utf8 」に関してエラーが出るが (下記ログ中の太字部分) 、これは /etc/mysql/conf.d/mysql.cnf に追加した記述が影響しているようだ。但し、今のところこのエラーが出るのは mysql_secure_installation だけのようだし (単独で mysql コマンドを利用しても OK 、 mysqld がエラーを出していたりもしない) 、とりあえずこのまま使って様子を見ることにする。
user_name@vpshost:~$ sudo mysql_secure_installation mysql_secure_installation: [ERROR] unknown variable 'default-character-set=utf8' Securing the MySQL server deployment. Enter password for user root: VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : y New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : yes - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yes Success. All done!