このエントリーをはてなブックマークに追加


CentOSにMySQLをインストール

CentOS6にMySQLをインストールしたときの備忘録です。

関連資料

CentOS6にMySQLをインストール

yumコマンドを利用してMySQLパッケージをインストールしました。
以下のコマンドでインストールすることができます。

yum -y install mysql-server

default-character-setをutf8に変更する

インストール後、rootにてmysqlコマンドを利用してcharacter_setを確認してみると以下のように出力されました。

[root@centos6 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.61 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like "char%";
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

/etc/my.cnfに追記

character-set-server = utf8を追記する。

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-server = utf8

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

追記してmysqldを再起動した後の結果です。
character_set_database, character_set_serverがutf8に変更されました。

mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

character_set_clientなども変更したい場合は、

[mysql]
default-character-set=utf8

を/etc/my.cnfに追記してください。

mysql_secure_installationによる初期設定

yumコマンドによりmysqldをインストールした後、以下のコマンドでmysqldを起動するとメッセージが表示されます。

  • mysqldの起動
    service mysqld start
  • mysqld初回起動時のメッセージ
    [root@centos6 ~]# service mysqld start
    MySQL データベースを初期化中:  WARNING: The host 'centos6' could not be looked up with resolveip.
    This probably means that your libc libraries are not 100 % compatible
    with this binary MySQL version. The MySQL daemon, mysqld, should work
    normally with the exception that host name resolving will not work.
    This means that you should use IP addresses instead of hostnames
    when specifying MySQL privileges !
    Installing MySQL system tables...
    OK
    Filling help tables...
    OK
    
    To start mysqld at boot time you have to copy
    support-files/mysql.server to the right place for your system
    
    PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
    To do so, start the server, then issue the following commands:
    
    /usr/bin/mysqladmin -u root password 'new-password'
    /usr/bin/mysqladmin -u root -h centos6 password 'new-password'
    
    Alternatively you can run:
    /usr/bin/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.
    
    You can start the MySQL daemon with:
    cd /usr ; /usr/bin/mysqld_safe &
    
    You can test the MySQL daemon with mysql-test-run.pl
    cd /usr/mysql-test ; perl mysql-test-run.pl
    
    Please report any problems with the /usr/bin/mysqlbug script!

mysql_secure_installationを実行する

mysql_secure_installationにより、MySQLの設定(セキュリティ回りなど)を行うことができます。
mysql_secure_installationを調べると、シェルスクリプトのようです。

[root@centos6 ~]# file `which mysql_secure_installation`
/usr/bin/mysql_secure_installation: POSIX shell script text executable

mysql_secure_installationを実行してみる

mysql_secure_installationを実行したときの出力です。

[root@centos6 ~]# 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): 

MySQLのrootパスワードを入力します。
設定していないので、Enterキーを押します。

OK, successfully used password, moving on...

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

Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

デフォルトではYになっていますが、一応Yを入力しました。
MySQLのrootパスワードを設定します。

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!

匿名ユーザの削除をするか聞かれたのでYを入力しEnterで削除しました。

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!

外部からのrootによるログインを許可するかを聞いてきたのでYを押してEnterで不許可にしました。

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!

testデータベースの削除をするのか?と聞いてきたのでYを押してEnterでtestデータベースを削除しました。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

設定の反映を行うか?と聞いてきたのでYを押してEnterで設定を反映させました。

Cleaning up...



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

Thanks for using MySQL!

MySQLをサーバ立ち上げ時に自動起動にする

CentOSではcheckconfigコマンドによりon/offすることができます。
以下のコマンドにより、mysqldをサーバ立ち上げ時にon/offすることができます。
本コマンドはrootユーザで実行してください。

  • onにする
    chkconfig mysqld on
  • offにする
    chkconfig mysqld off
  • 設定を確認する
    chkconfig --list | grep mysql

MySQLにログインしてみる

以下のコマンドでMySQLにログインしてみます。

mysql -u root -p

Enter Passwordで入力するパスワードは、上記のmysql_secure_installationコマンドで設定したパスワードになります。

[sakura@centos6 ~]$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.1.61 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2015-03-20 (金) 22:08:00