LoginSignup
4
9

More than 3 years have passed since last update.

さくらのVPSにMySQLをインストール

Last updated at Posted at 2020-06-01

環境は以下の記事をご参照ください。centOs7です。
さくらのVPSにSSH接続+最低限のセキュリティ対策

ルートユーザーに切り替え

毎回sudoつけるの面倒なので

$ su -

MariaDBの削除

centOsにはデフォルトでMariaDBがインストールされています。不要なので削除。

$ yum remove mariadb-libs
$ rm -rf /var/lib/mysql

yumにMySQLのリポジトリを登録

npmなどと違い、yumにはデフォルトで登録されていないリポジトリが結構あります。
MySQLはデフォルトで登録されていない(or 古いバージョンしか登録されていない)ので、リポジトリを追加します。
ここではバージョン5.7系を追加しています。(たぶん57の部分を他の数字に変えれば他のバージョンを追加できるはず)

$ rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

インストール

$ yum install -y mysql-community-server mysql-devel
$ mysqld --version

起動設定

$ systemctl start mysqld.service # 起動
$ systemctl enable mysqld.service # 自動起動設定

初期設定

まずは初期パスワードを確認。(lCCqZ>=Q!9Yo)

$ cat /var/log/mysqld.log | grep password
[Note] A temporary password is generated for root@localhost: lCCqZ>=Q!9Yo

対話形式で初期設定

$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: # ログファイルから取得した初期パスワードを入力

The existing password for the user account root has expired. Please set a new password.

New password: # rootユーザの新規パスワードを入力(大文字小文字英数字+特殊文字で8文字以上で設定)

Re-enter new password: # 確認用にもう一度入力
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
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 # 本当にパスワードを変更して良いかの確認

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 # localhost以外からrootアカウントでログインできないようにする
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) : y # testデータベースを削除
 - 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) : y # リロードして設定を反映する
Success.

All done!

日本語対応

mysqlにログイン

$ mysql -u root -p

文字コードを確認。latin1のところをutf8に変更したい。

mysql> show variables like "chara%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
mysql> exit;
$ vi /etc/my.cnf
/etc/my.cnf
# ファイル末尾に以下を追記
character-set-server=utf8

[client]
default-character-set=utf8

再起動してlatin1→utf8に変更されていることを確認

$ systemctl restart mysqld.service
$ mysql -u root -p
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/ |
+--------------------------+----------------------------+

タイムゾーン設定

デフォルトのタイムゾーンを確認。SYSTEM→Asia/Tokyoに変更したい。

$ mysql -u root -p
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | JST    |
| time_zone        | SYSTEM |
+------------------+--------+
$ /usr/bin/mysql_tzinfo_to_sql /usr/share/zoneinfo > ~/timezone.sql 
# タイムゾーンのインポート。↑が失敗したら↓を試す
$ mysql_tzinfo_to_sql /usr/share/zoneinfo

$ mysql -u root -p -Dmysql < ~/timezone.sql
$ vi /etc/my.cnf
/etc/my.cnf
末尾に以下を追記
[mysqld]
default-time-zone = 'Asia/Tokyo'
$ systemctl restart mysqld.service
$ mysql -u root -p
mysql > show variables like '%time_zone%';
+------------------+------------+
| Variable_name    | Value      |
+------------------+------------+
| system_time_zone | JST        |
| time_zone        | Asia/Tokyo |
+------------------+------------+
2 rows in set (0.00 sec)

データベースと専用ユーザーを作成

$ mysql -u root -p
-- データベースを作成
CREATE DATABASE database_name DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- ユーザーを作成
CREATE USER 'your_name'@'localhost' IDENTIFIED BY 'your_password';
-- 作成したユーザーに作成したデータベースの操作権限を付与
GRANT ALL PRIVILEGES ON database_name.* TO 'your_name'@'localhost';
-- 設定を反映
FLUSH PRIVILEGES;
-- データベース一覧を表示
SHOW DATABASES;
-- ユーザー一覧を表示
SELECT host, user, password FROM mysql.user;

参考

4
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9