Help us understand the problem. What is going on with this article?

virtualbox(centos)でMYSQLインストールメモ(mysqld_multiで複数インスタンス起動まで)

More than 5 years have passed since last update.

環境 centos6.3(ゲストOS)macosx10.8(ホストOS)

centosのデフォルトのリポジトリでmysqlをインストールすると5.1系が入ってしまう。それだとmysqld_mutiの機能がないので最新版の5.5系をインストールする必要がある

  • ここから先は全てrootユーザーで実行しています
  • 開発環境構築の場合で記載しています。本番環境などはもっと詳しいブログをみつけてください。

参考にしたサイト

install編

yumでサクッと入れる(remiリポジトリで)

yum --enablerepo=remi install mysql-server

my.cnfのバックアップ

my.cnfとはMYSQLの設定ファイルです。
ココでゴニョゴニョカクことによってmysqlで色々なことができます。

なんかやらかした場合に戻せるように

cp /etc/my.cnf /etc/my.cnf.bk

my.cnfの編集

デフォルトの設定を編集します

/etc/my.cnf
vim /etc/my.cnf

#修正前
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

#修正後
[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock

#追加 ----ここから----
#mysqld項目のすぐ下に記載してください
#デフォルトMYSQL
[mysqld1]
server-id = 1
port      = 3306
datadir   = /var/lib/mysql/multi1
socket    = /var/lib/mysql/multi1/mysql.sock
pid-file  = /var/lib/mysql/multi1/mysqld.pid

#MYSQLその2
[mysqld2]
server-id = 2
port      = 3307
datadir   = /var/lib/mysql/multi2
socket    = /var/lib/mysql/multi2/mysql.sock
pid-file  = /var/lib/mysql/multi2/mysqld.pid
#追加 ----ここまで----

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

datadir,socketpid-file,port,server-idが重複しなければ複数記載でOK
編集後のmy.cnf

[mysqld_multi]
mysqld     = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin

[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 (fedora >= 15).
# 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
user=mysql

# Semisynchronous Replication
# http://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html
# uncomment next line on MASTER
;plugin-load=rpl_semi_sync_master=semisync_master.so
# uncomment next line on SLAVE
;plugin-load=rpl_semi_sync_slave=semisync_slave.so

# Others options for Semisynchronous Replication
;rpl_semi_sync_master_enabled=1
;rpl_semi_sync_master_timeout=10
;rpl_semi_sync_slave_enabled=1

# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html
;performance_schema

;default-character-set = utf8

#デフォルトMYSQL
[mysqld1]
server-id = 1
port      = 3306
datadir   = /var/lib/mysql/multi1
socket    = /var/lib/mysql/multi1/mysql.sock
pid-file  = /var/lib/mysql/multi1/mysqld.pid

#MYSQLその2
[mysqld2]
server-id = 2
port      = 3307
datadir   = /var/lib/mysql/multi2
socket    = /var/lib/mysql/multi2/mysql.sock
pid-file  = /var/lib/mysql/multi2/mysqld.pid

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

パーミッションをmysqlユーザーに変更する

chown -R mysql:mysql /var/lib/mysql/

mysql_install_dbでdataの初期化を行う

dataの初期化処理をする

mysql_install_db --datadir=/var/lib/mysql/multi1 --user=mysql
mysql_install_db --datadir=/var/lib/mysql/multi2 --user=mysql

#両方ともこんな感じのメッセージがでればOK

#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 localhost.localdomain 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!

mysqld_multi起動スクリプトを作成する

MYSQLは起動した瞬間に立ち上がっててほしいはずなので起動スクリプトを書く

/etc/init.d/mysqld_multi
vim /etc/init.d/mysqld_multi

#!/bin/sh

# Mysql daemon start/stop script.
#

# Usually this is put in /etc/init.d (at least on machines SYSV R4
# based systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/S01mysql.
# When this is done the mysql server will be started when the machine is started
# and shut down when the systems goes down.

# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 90 90
# description: A very fast and reliable SQL database engine.

# The following variables are only set for letting mysql.server find things
# if you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf or other configuration files

MYSQLDIR=/usr
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:$MYSQLDIR/bin
MYSQLID=1,2,3,4,5
export PATH

# See how we were called.
case "$1" in
start)
echo -n "Starting mysqld: "
$MYSQLDIR/bin/mysqld_multi start $MYSQLID
echo
;;
stop)
echo -n "Shutting down mysqld: "
$MYSQLDIR/bin/mysqld_multi stop $MYSQLID
echo
;;
status)
$MYSQLDIR/bin/mysqld_multi report $MYSQLID
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac

exit 0

起動スクリプトの登録

chkconfig --add mysqld_multi
chkconfig mysqld_multi on

一旦virtualboxを落として自動起動しているか確認する

起動して下記のコマンドを実行してis runningと表示されていればOK

/etc/init.d/mysqld_multi status

#Reporting MySQL servers
#MySQL server from group: mysqld1 is running
#MySQL server from group: mysqld2 is running

接続確認

mysqld_multiを設定するとmysqlの起動時にポートの指定とhostの指定をしないとmysqlに入れなくなるので注意してください

mysql -uroot -P3306 -h127.0.0.1

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.30 MySQL Community Server (GPL) by Remi

Copyright (c) 2000, 2013, 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> 

rootユーザーのパスワードを設定する

2つのインスタンスがあるので2つとも設定する
※以下はport3306のみの内容を記載します

mysqladmin -uroot -P3306 -h127.0.0.1 password 'パスワード'

接続エラーになるのを確認する

mysql -uroot -P3306 -h127.0.0.1
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

#再度設定したパスワードでmysqlに入る
mysql -uroot -pパスワード -P3306 -h127.0.0.1

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.30 MySQL Community Server (GPL) by Remi

Copyright (c) 2000, 2013, 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> 

パスワードがないrootユーザーを削除する

MySQLには最初パスワードの存在しないユーザが用意されていますので、これを削除します。

mysql> DELETE FROM mysql.user WHERE Password = '';
Query OK, 3 rows affected (0.00 sec)

普段使用するユーザーを作成する

GRANT ALL PRIVILEGES ON *.* TO 'ユーザー名'@'localhost' IDENTIFIED BY 'パスワード' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

作成したユーザーで入れるか確認する

mysql -uユーザー -pパスワード -P3306 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.30 MySQL Community Server (GPL) by Remi

Copyright (c) 2000, 2013, 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> 

以上

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした