KACHIMAI DIGITAL MEDIA LAB

R&D サーバ・ネットワーク MySQL

AO

CentOS7.2にMySQLをインストールして、初期設定する方法まとめ

MySQLのインストール

MariaDBの削除

 CentOS7からデフォルトでMariaDBが入っているはずなので、まずMariaDBがインストールされているかを確認して、あれば削除(remove)する。既存のデータも削除(rm)する。

$yum list installed | grep maria
mariadb-libs.x86_64 1:5.5.50-1.el7_2 @updates

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

リポジトリの追加

 公式サイトでリポジトリのURLを確認します。
Download MySQL Yum Repository

 上記サイトの「Download」→「No thanks, just start my download」のリンクアドレスをコピーして、yum localinstall -y に続けます。

$ yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

 古いバージョンが入っているとエラーが出ます。

Error: mysql57-community-release conflicts with mysql-community-release-e7-5.noarch

 インストール済みのMySQLの確認して、古いバージョンなどを一括で削除し、再度リポジトリの追加を試みます。

$ yum list installed | grep mysql
mysql-community-release.noarch el7-5 @/mysql-community-release-el7-5.noarch

$ yum remove mysql*
$ yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

インストール

 ここまで問題なければmysql-community-serverをインストールします。

$ yum install -y mysql-community-server

 インストールされたか確認します。

$ mysqld --version
mysqld Ver 5.7.17 for linux on x86_64 (MySQL Community Server (GPL))

MySQLの設定

起動

 自動起動設定して、起動して、ステータスを確認します。ステータスはActive:active (running)になっていればOK。

$ systemctl enable mysqld
$ systemctl start mysqld
$ systemctl status mysqld

rootパスワード変更

 rootパスワードが初期設定されているので、それを確認します。

$ cat /var/log/mysqld.log | grep root
[Note] A temporary password is generated for root@localhost: T9xijl%opQ2s

 mysql_secure_installationコマンドで変更します。最初に初期設定パスワードの入力を求められます。
 次に新しいパスワードの入力を求められますが、'validate_password' pluginというのが入っていて、安易なパスワードだと受け付けてくれません。英数文字列に記号も入ったパスワードを入力します。
 以降は全部yでOKです。anonymousユーザの削除と、外部ホストからのrootログイン禁止、test用のデータベース削除、権限テーブルのリロードです。

$ mysql_secure_installation

Securing the MySQL server development.

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:
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.

Estimeted strength of the passwoed: 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 othe key for No) : y

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

Be 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

 - 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

All done!

my.cnfの設定

 使う文字コードをUTF-8にします。
 MySQLのバージョンが5.7.4~5.7.10だと、パスワード有効期限(360日)がデフォルトで設定されているので、無効にします。5.7.11以降は設定の必要なし。
 パスワードに記号を入れたくない場合はvalidate_password_policyを変更します。  ログの時間をシステムと同じタイムスタンプを使うようにします。

$ vi /etc/my.cnf

[mysqld]
・・・
character_set_server=utf8
default_password_lifetime=0
validate_password_policy=LOW
log_timestamps=system

ログ

 ログについては下記公式サイトを参照。
MySQL Server ログ

 デフォルトではエラーログしか出力されません。

再起動して設定を反映

$ systemctl restart mysqld

新しいパスワードでログイン

$ mysql -u root -p

パスワードを変更する場合は、ログインした状態で下記コマンド。

mysql> update mysql.user set password=password('hogehoge') where user ='root';
mysql> flush privileges;
mysql> exit;

以上です。

COMMENT

お名前

メールアドレス

URL

コメント

関連記事

最新記事リスト

月別記事リスト

リンク

人気の記事一覧

  • ハート数
  • 週間アクセス
  • 月間アクセス
Instagram