mysql@CentOS7

CentOS7では同梱されているのが
MySQLではなくMariadbになってます。
なぜそうなったのかはアーカイブを探って
いただくとして、これに泣いている人がいます。

CentOS6(たぶんfinal)から7にしようとしたら
MySQLのデータ移行がうまくいかなくて、MySQLに
したいけど、yumでmysqlをインストールするも
Mariadbが勝手に入ってきちゃうと嘆いています。

そういえば拙性も当初は同じ経験をしましたが、
なんとかMySQLをインストールできました。
出来たことは書いていましたが、どぉやったかまで
書いていなかったので、ちょっとここに書いておきます。

まずは共存できないのでMariadbをぶち消します。
以下のコマンドで出てきたものを全部です。

# rpm -qa | grep mariadb

例えばmariadb-libsが表示されたら

# yum remove mariadb-libs

のごとくです。
次に6から移設したという/var/lib/mysqlの
データも全部ぶち消します。

# rm -rf /var/lib/mysql/

ここから初めてMySQLをインストールします。
まずはリポジトリを登録します。

# yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

注意すべきことはインストールするのは
Community Editionであることです。
例えばmysal-serverは

# yum -y install mysql-community-server

となります。
依存関係にあるものもインストールされますが、
もしかしたらphp-mysqlあたりがインストールされないかも。
phpとmysqlの組み合わせでphpが拡張できないzoー!
なんて怒られたらこれだと思ってください。

で、システム起動時に自動起動するように登録し
とりあえず手動で立ち上げてみます。

# systemctl enable mysqld.service
# systemctl start mysqld.service

これまではごく普通なのですが、ここからがちょいと
面倒くさいかも。

まずこのバージョンでは初期パスワードが自動で決められていて、
そのパスワードでMySQLに入りこんでから任意のPWに変更します。
その初期PWはココ↓で調べます。

# cat /var/log/mysqld.log

これで表示されたなかのこの部分です。

A temporary password is generated for root@localhost: *********

この*********が初期パスワードです。
では任意のパスワードに変更します。

パスワードは
8文字以上
・英大文字
・英小文字
・数字
・記号
の4種類を含む。。。

つまり Zx259u-W のごとくです。
これにマッチしないとはねられちゃいます。
以下のコマンドで対話モードに入ります。

# 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: 任意のパスワードを入力

Re-enter new password: 任意のパスワードを再度入力

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: 0
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: 50
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, 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) : y 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!

ウ~ン、こう来たか!?って感じですね。

次にutf-8の指定とパスワード期限をなしに設定しておきます。
ゼロ以外の数値では期限付きになります。

# vi /etc/my.cnf

[mysqld]
character-set-server = utf8
default_password_lifetime = 0

一度再起動してみます。

# systemctl restart mysqld.service

これ以降にデータを移行しますが、ネットワーク経由の
rsyncで、現用機からデータベースである/var/lib/mysql/***を
新しいサーバーに必要な分だけ送るのが簡単です。

# rsync -azv -e ssh /var/lib/mysql/oba-q 192.168.**.**:/var/lib/mysql/

# systemctl restart mysqld.service

面倒だからと/var/lib/mysql/*のように全部をいっぺんに
送らないでください。
mysqlがエラーを吐いて起動しなくなります。

もしroot以外のユーザーがデータベースを作成していた場合
そのユーザーも新しいサーバー側に作成しておきます。

# mysql -u root -p

先ほど設定したパスワードで入りユーザー(例としてoba-q)を
作成します。
mysql> GRANT ALL PRIVILEGES ON *.* TO oba-q@localhost IDENTIFIED BY ‘********’ WITH GRANT OPTION;
mysql>flush privileges;
mysql>quit;

********の部分がユーザーoba-qのパスワードとなりますが、
ポリシーはrootパスワードと一緒です。
現在はquitの後に;がなくてもOKですが癖でつけちゃいます。(笑

public_htmlなど公開している領域の移行していて、
Web上で表示される設定が完了しているのなら、
アドレスをlocalhostを使用してアクセスしてみてください。
うまく行ったらLAMP環境のCMSなどはローカル内で表示されます。

ナンか足りないzoー!などと怒られてしまったら

# yum install php-mysql

などと、必要な部分を補います。
phpを反映させるには

# systemctl restart httpd.service

のようにhttpdを再起動してください。

上記作業でこのBLOG(ベースがWordpress)はサーバー
予備機のローカル上でちゃんと表示されています。

HOME

おすすめ