LoginSignup
74
82

More than 5 years have passed since last update.

EC2にMySQLインストールと設定確認

Posted at

RDS使えばいいじゃんという話もありますが、色々中身見たかったのでEC2(Amazon Linux AMI 2016.03.0)にMySQL5.6をインストールしたときのメモ。

MySQLのインストールと起動

以下で可能です。

# MySQL5.6のサーバーとクライアントをインストールする
$sudo yum install  mysql56 mysql56-server

# サービスの起動
$sudo service mysqld start
・・・

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  /usr/libexec/mysql56/mysqladmin -u root password 'new-password'
  /usr/libexec/mysql56/mysqladmin -u root -h ip-172-31-27-76 password 'new-password'

Alternatively you can run:

  /usr/libexec/mysql56/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

Note: new default config file not created.
Please make sure your config file is current

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

Starting mysqld:                                           [  OK  ]

Rootパスワードを設定してね、という旨の表示がされています。
後で設定します。
一度現在の状態を確認します。

# MySQLのrootユーザーでmysqlへログイン
$mysql -u root

# localhostからの接続ができるrootユーザーと匿名ユーザーがいる。パスワードは設定されていない
mysql> select Host, User, Password  from mysql.user;
+-----------------+------+----------+
| Host            | User | Password |
+-----------------+------+----------+
| localhost       | root |          |
| ip-172-31-27-76 | root |          |
| 127.0.0.1       | root |          |
| ::1             | root |          |
| localhost       |      |          |
| ip-172-31-27-76 |      |          |
+-----------------+------+----------+
6 rows in set (0.00 sec)

# mysqlからログアウト
mysql>\q

MySQLのrootユーザーがいますが、パスワードが設定されず、誰でもログインできる状態になっています。。。。
また、匿名ユーザーがいます。

初期設定を行う(Rootパスワードの設定など)

対話形式のコマンドより初期設定を行います。

# コマンドを実行
$sudo /usr/libexec/mysql56/mysql_secure_installation


# 現在のMySQLのrootパスワードを入力。パスワード未設定なのでenterだけタイプ
Enter current password for root (enter for none):

# rootパスワードを変えるか。変更するのでY。任意のパスワードを設定
Change the root password? [Y/n]

# 匿名ユーザーを削除するか?不要なのでYで削除
Remove anonymous users? [Y/n]

# rootユーザーのリモートホストからのログインを無効化するか?無効化したいのでY
Disallow root login remotely? [Y/n]

# 初期状態ではtestデータベースがあるが削除するか?削除したいのでY
Remove test database and access to it? [Y/n]

# 変更を即座に反映するために特権テーブルの読み込みを行うか?反映させたいのでY
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]


# 終わり
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...

mysqlへログインして確認します。

# パスワードなしではログイン不可
$mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

# パスワードを対話形式で入力
$mysql -u root -p
Enter password:

# 匿名ユーザーが削除され、rootユーザーもパスワードが設定された
mysql> select Host, User, Password  from mysql.user;
+-----------+------+-------------------------------------------+
| Host      | User | Password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *FABE5482D5AADF36D028AC443D117BE1180B9725 |
| 127.0.0.1 | root | *FABE5482D5AADF36D028AC443D117BE1180B9725 |
| ::1       | root | *FABE5482D5AADF36D028AC443D117BE1180B9725 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)

いい感じに設定できました。

デフォルト設定を確認してみる

次にデフォルトの設定を確認します。

# 起動コマンドを確認
$ps aux |grep mysql
root      2831  0.0  0.2  11604  2656 pts/0    S    04:45   0:00 /bin/sh /usr/libexec/mysql56/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql     3047  0.0 45.0 1065124 459496 pts/0  Sl   04:45   0:00 /usr/libexec/mysql56/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql56/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
ec2-user  3113  0.0  0.2 110472  2168 pts/0    S+   04:55   0:00 grep --color=auto mysql

# プロセス確認(mysql部分のみピックアップ)
$sudo pstree -p
        ├─mysqld_safe(2831)───mysqld(3047)─┬─{mysqld}(3048)
        │                                  ├─{mysqld}(3049)
        │                                  ├─{mysqld}(3050)
        │                                  ├─{mysqld}(3051)
        │                                  ├─{mysqld}(3052)
        │                                  ├─{mysqld}(3053)
        │                                  ├─{mysqld}(3054)
        │                                  ├─{mysqld}(3055)
        │                                  ├─{mysqld}(3056)
        │                                  ├─{mysqld}(3057)
        │                                  ├─{mysqld}(3059)
        │                                  ├─{mysqld}(3060)
        │                                  ├─{mysqld}(3061)
        │                                  ├─{mysqld}(3062)
        │                                  ├─{mysqld}(3063)
        │                                  ├─{mysqld}(3064)
        │                                  ├─{mysqld}(3065)
        │                                  ├─{mysqld}(3066)
        │                                  ├─{mysqld}(3067)
        │                                  ├─{mysqld}(3068)
        │                                  └─{mysqld}(3072)

# 設定ファイルを確認
$sudo cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

利用しているファイルなどは以下のようです。

タイトル パス 内容
起動スクリプト /usr/libexec/mysql56/mysqld_safe シェルスクリプト(rootで実行)
起動コマンド /usr/libexec/mysql56/mysqld (mysqlで実行)
ベースディレクトリ(basedir) /usr/
データディレクトリ(datadir) /var/lib/mysql
プラグインディレクトリ(plugin-dir) /usr/lib64/mysql56/plugin
エラーログ(log-error) /var/log/mysqld.log
PIDファイル(pid-file) /var/run/mysqld/mysqld.pid
UNIXドメインソケットファイル(socket) /var/lib/mysql/mysql.sock

変更したい場合には適宜、設定ファイルなどを変更すればいけるのでははないかと思います。

また、pstreeコマンドを見るとプロセスID3047を親プロセスとする複数のスレッドが起動しているのも確認できました。マルチスレッドで動作しているのですね。

サービスの停止

$sudo service mysqld stop
Stopping mysqld:                                           [  OK  ]
74
82
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
74
82