ぺーぺーSEのブログ

備忘録・メモ用サイト。

MySQL5.6のインストール

CentOSrpmでインストールした。
基本的にroot作業。


事前準備

MariaDBがインストールされていないか確認

CentOS7やRHEL7にはデフォルトでMariaDBがインストールされているためアンインストールする。

# MariaDBがインストールされていないか確認
> yum list installed | grep maria
mariadb-libs.x86_64              1:5.5.35-3.el7                        @anaconda
# あれば削除
> yum -y remove mariadb-libs


MySQLがインストールされていないか確認

これからインストールするMySQLと異なるバージョンのMySQLがインストールされていないか確認する。

# MySQLがインストールされていないか確認
> rpm -qa | egrep -i 'mysql|unixodbc'

# mysql-libs以外のRPMパッケージを削除する
> rpm -e --allmatches mysql mysql-devel

# MySQLのパッケージが削除された事を確認する
> rpm -qa | egrep -i 'mysql|unixodbc'


mysqlユーザやmysqlグループが作成されているかどうかを確認

# mysqlユーザがいないか確認
> id mysql
# mysqlグループがいないか確認
> grep mysql /etc/group
# いれば削除
> userdel -r mysql
> groupdel mysql



MySQLのインストール

以下のRPMを準備する。

  • Clientパッケージ(client
    • mysqlやmysqladminなど各種MySQLクライアントプログラムが格納されたパッケージ。
  • Serverパッケージ(server
    • mysqldなどのサーバ側で必要となるプログラムのパッケージ。
  • 開発用パッケージ(devel
    • MySQLをリンクするプログラムを作成するためのパッケージ。ヘッダーファイルとスタティックリンクするためのライブラリが含まれる。
  • 共有ライブラリパッケージ(shared
  • スタティックではなく、libmysqlclientとダイナミックにリンクするプログラムを作成する際に必要なパッケージ。
  • 互換共有ライブラリパッケージ(shared-compat
    • 古いバージョンのMySQLと互換のあるlibmysqlclientを利用する際に必要となるパッケージ。
  • 組み込みサーバパッケージ(embedded
    • 組み込みデータベース用のMySQLプログラム、libmysqldが格納されたパッケージ。
  • テストスイートパッケージ(test
    • MySQLのテストスイートが格納されたパッケージ。

MySQLサーバ用途であればclient、server、shared、及びdevel を、MySQLクライアント用途であればclient、shared、及び、devel が必要。
インストールの際、mysql-libsがmysql-serverと競合してインストールできないことがあるため、shared-compatパッケージを上書きインストールする。
以下はMySQL5.6をインストールする際の手順の例。

> wget http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-shared-compat-5.6.21-1.el7.x86_64.rpm
> wget http://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-5.6.21-1.el7.x86_64.rpm-bundle.tar
> tar xvf MySQL-5.6.21-1.el7.x86_64.rpm-bundle.tar

> rpm -ivh MySQL-shared-compat-5.6.21-1.el7.x86_64.rpm
> rpm -ivh MySQL-shared-5.6.21-1.el7.x86_64.rpm
> rpm -ivh MySQL-devel-5.6.21-1.el7.x86_64.rpm
> rpm -ivh MySQL-client-5.6.21-1.el7.x86_64.rpm
> rpm -ivh MySQL-server-5.6.21-1.el7.x86_64.rpm

インストールが完了したら「/etc/init.d/mysql」に起動スクリプトが登録されているので、サーバ起動時に自動でMySQLがあがる。
ランレベルを確認する。

> chkconfig --list mysql
mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off

ランレベルの意味は以下の通りで、自動起動は3がonであればいい。

ランレベル 意味 詳細
0 システムの停止 シャットダウンを意味する。
1 シングルユーザーモード、ネットワーク無し 所謂セーフモード。rootでしかログインできない。
2 マルチユーザーモードNFSマウントなし)
3 マルチユーザーモード(コンソール)、ネットワーク有り X Windows以外のデーモンのほとんどを起動するモード。
4 未使用
5 マルチユーザーモードX Windows)、ネットワーク有り デスクトップマシンとして利用する場合やX Windowsを利用する場合。
6 システム再起動 reboot時にはランレベルが6。

MySQLユーザおよびグループが作成されていることを確認する。

# mysqlグループの確認
> cat /etc/group | grep mysql
mysql:x:502:

見方は「[グループ名]:[シャドウパスワード]:[グループID]:[サブグループとして登録しているユーザ]」。

# mysqlグループの確認
> cat /etc/passwd | grep mysql
mysql:x:502:502::/usr/local/mysql:/bin/bash

見方は「[ユーザ名]:[シャドウパスワード]:[ユーザID]:[グループID]:[コメント]:[ホームディレクトリ]:[シェル]」。
シェルが「/noexist」のことがあるので「/bin/bash」に変更しておく。


OS系の各種パラメータをチューニング

mysqlユーザが使用できるシステムリソースの上限を確認

> su - mysql
> ulimit -a
[mysql@localhost ~]$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3623
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

ファイルディスクリプタ上限(open files)および起動プロセス上限(max user processes)をマシンスペックに応じて変更する。
rootに戻って「/etc/security/limits.conf」に下記を加筆する。

mysql      soft    nproc   xxxxx
mysql      hard    nproc   xxxxx
mysql      soft    nofile  yyyyy
mysql      hard    nofile  yyyyy

xxxxx、yyyyyの箇所をいい感じに。
設定が反映されていることを確認。

> su - mysql
> ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3623
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) xxxxx
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) yyyyy
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

また、Linuxカーネル2.6から採用されたswappiness(メモリスワップの度合いを調整するパラメータ)を変更する。

> cat /proc/sys/vm/swappiness
60
> echo 0 > /proc/sys/vm/swappiness


カーネルパラメータを変更

rootユーザで下記のように確認する。

> sysctl -a
# 超出るので省略

ソケットバッファをマシンスペックに応じて変更およびswappinessを設定しておく。
/etc/sysctl.conf」を下記のように編集。

net.core.rmem_default = xxxxxx
net.core.rmem_max = xxxxxx
net.core.wmem_default = xxxxxx
net.core.wmem_max = xxxxxx
vm.swappiness = 0

xxxxxの箇所をいい感じに。
設定が反映されていることを確認。

> sysctl -a | grep net.core.
net.core.wmem_max = xxxxxx
net.core.rmem_max = xxxxxx
net.core.wmem_default = xxxxxx
net.core.rmem_default = xxxxxx
> sysctl -a | vm.swappiness
vm.swappiness = 0



MySQLの各種パラメータをチューニング

MySQLの設定ファイルは「/usr/my.cnf」にある。

> cat /usr/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

my.cnfの書き方は長くなりそうなので別で。


MySQLの起動および初期設定

mysqlの起動(mysqlユーザで)。

> service mysql start

起動確認。

> ps -ef | grep -i mysql
mysql xxxx xxxx xx xx:xx xxx/x xx:xx:xx /usr/sbin/mysqld --basedir=/usr
                                                         --datadir=/var/lib/mysql
                                                         --plugin-dir=/usr/lib/mysql/plugin
                                                         --log-error=/var/lib/mysql/localhost.localdomain.err
                                                         --pid-file=/var/lib/mysql/localhost.localdomain.pid



初期パスワードの確認(rootで)。

> cat ~/.mysql_secret

mysqlへログイン(mysqlユーザで)。

> mysql -u root -p[上で確認したパスワード]

初期パスワードからの変更。

>> set password='';
>> exit


mysqlの初期設定

mysql_secure_installation」を使って下記のように質問に答えて初期設定を行います。
mysqlユーザの書き込み権限があるディレクトリ且つmysqlユーザで実行する。

> mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

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? [Y/n] 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? [Y/n] 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? [Y/n] Y
 - 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? [Y/n] Y
 ... Success!




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ユーザで)。

> mysqladmin -u root -p[password] shutdown