mysqld_multi で複数起動&レプリケーション mysql_safe | mysql 5.5

とりあえず、2つ動かして、master-slaveをテスト環境で作る。
適当に入れて適当に起動したら。

まだ作ってませんがTRANODEもよろしくお願いします。
user tranodeで動かします。

/etc/my.cnf

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

~/.my.cnf

[mysqld1]
datadir = /home/tranode/data/my/data1
socket = /home/tranode/data/my/mysql1.sock
pid-file = /home/tranode/data/my/mysqld1.pid
port = 50431
server-id=1
log-error=/home/tranode/log/my/mysqld1.log
sync_binlog=1
log-bin=mysql-bin
log-bin-index=mysql-bin
relay-log=relay-bin
relay-log-index=relay-bin

# slave
[mysqld2]
datadir = /home/tranode/data/my/data2
socket = /home/tranode/data/my/mysql2.sock
pid-file = /home/tranode/data/my/mysqld2.pid
port = 50432
server-id=2
master-host=127.0.0.1
master-port=50432
master-user=tranode
master-password=********
log-error=/home/tranode/log/my/mysqld2.log
read_only
log-slave-updates
log-bin=mysql-bin
log-bin-index=mysql-bin
relay-log=relay-bin
relay-log-index=relay-bin
$ mysqld_multi start --log=/home/tranode/log/my/multi.log

* root以外で実行する場合logを指定しないとエラーが出る。
WARNING: Log file disabled. Maybe directory or file isn’t writable?

111213 19:21:17 [Note] Plugin 'FEDERATED' is disabled.
/usr/libexec/mysqld: Table 'mysql.plugin' doesn't exist
111213 19:21:17 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
111213 19:21:17 InnoDB: The InnoDB memory heap is disabled

データベースを作れと

$ mysql_install_db --datadir=/home/tranode/data/my/data1 --user=tranode
$ mysql_install_db --datadir=/home/tranode/data/my/data2 --user=tranode
Installing MySQL system tables...
111213 22:07:39 [Warning] Ignoring user change to 'tranode' because the user was set to 'mysql' earlier on the command line

まぁまぁ行けたけど
* http://dev.mysql.com/doc/refman/5.5/en/replication-options-slave.html
Obsolete options. The following options are removed in MySQL 5.5. If you attempt to start mysqld with any of these options in MySQL 5.5, the server aborts with an unknown variable error. To set the replication parameters formerly associated with these options, you must use the CHANGE MASTER TO … statement (see Section 12.4.2.1, “CHANGE MASTER TO Syntax”).

ということでmaster-hostとかは使えないので、CHANGE MASTER TO で設定しろと。

この4行削除

master-host=127.0.0.1
master-port=50432
master-user=tranode
master-password=********

起動/停止OK

$ mysqld_multi start --log=/opt/home/tranode/log/my/multi.log
$ ps aux|grep mysql
tranode   5192  0.0  0.2  63872  1276 pts/2    S    14:01   0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/opt/home/tranode/data/my/data1 --socket=/opt/home/tranode/data/my/mysql1.sock --pid-file=/opt/home/tranode/data/my/mysqld1.pid --port=50431 --server-id=1 --log-error=/opt/home/tranode/log/my/mysqld1.log --sync_binlog=1 --log-bin=mysql-bin --log-bin-index=mysql-bin --relay-log=relay-bin --relay-log-index=relay-bin
tranode   5198  0.0  0.2  63872  1272 pts/2    S    14:01   0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/opt/home/tranode/data/my/data2 --socket=/opt/home/tranode/data/my/mysql2.sock --pid-file=/opt/home/tranode/data/my/mysqld2.pid --port=50432 --server-id=2 --log-error=/opt/home/tranode/log/my/mysqld2.log --read_only --log-slave-updates --log-bin=mysql-bin --log-bin-index=mysql-bin --relay-log=relay-bin --relay-log-index=relay-bin

$ mysqld_multi stop --log=/opt/home/tranode/log/my/multi.log
$ ps aux|grep mysql
Starting MySQL servers

111214 14:01:52 mysqld_safe Logging to '/opt/home/tranode/log/my/mysqld1.log'.
111214 14:01:52 mysqld_safe Logging to '/opt/home/tranode/log/my/mysqld2.log'.
111214 14:01:52 mysqld_safe Starting mysqld daemon with databases from /opt/home/tranode/data/my/data1
111214 14:01:52 mysqld_safe Starting mysqld daemon with databases from /opt/home/tranode/data/my/data2
mysqld_multi log file version 2.16; run: 水 12月 14 14:06:33 2011

Stopping MySQL servers

111214 14:06:34 mysqld_safe mysqld from pid file /opt/home/tranode/data/my/mysqld2.pid ended
111214 14:06:34 mysqld_safe mysqld from pid file /opt/home/tranode/data/my/mysqld1.pid ended

login master

$ mysql -u root -P 50431 -h 127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1

mysql> CREATE DATABASE IF NOT EXISTS tranode CHARACTER SET 'utf8';
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| tranode            |
+--------------------+
5 rows in set (0.00 sec)
mysql> CREATE USER 'tranode'@'127.0.0.1' IDENTIFIED BY 'mypass';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL ON *.* TO 'tranode'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL ON *.* TO 'tranode'@'localhost';
Query OK, 0 rows affected (0.00 sec)

check login by tranode

$ mysql tranode -u tranode -P 50431 -h 127.0.0.1 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.19-log MySQL Community Server (GPL) by Remi

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 |
| test               |
| tranode            |
+--------------------+
3 rows in set (0.00 sec)
mysql> show create table users;
| users | CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `authid` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `authid` (`authid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SLAVE を見てみる。データベースも何も無いはず。

$ mysql -u root -P 50432 -h 127.0.0.1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

レプリケーション設定。

http://dev.mysql.com/doc/refman/5.5/en/sql-syntax-replication.html

mysql> show slave status;
Empty set (0.00 sec)
mysql> CHANGE MASTER TO 
MASTER_HOST = '127.0.0.1',
MASTER_USER = 'tranode',
MASTER_PASSWORD = 'mypass',
MASTER_PORT = 50431
;
mysql> start slave;
mysql> show slave status;
(略)
| Waiting for master to send event | 127.0.0.1   | tranode     |       50431 |            60 | 

mysql> show dabases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| tranode            |
+--------------------+
5 rows in set (0.00 sec)

mysql> use tranode
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_tranode |
+-------------------+
| users             |
+-------------------+
1 row in set (0.00 sec)

master側から確認

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |     1088 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql> show slave hosts;
+-----------+------+------+-----------+
| Server_id | Host | Port | Master_id |
+-----------+------+------+-----------+
|         2 |      | 3306 |         1 |
+-----------+------+------+-----------+
1 row in set (0.00 sec)

? 3306になってる。おかしいけど。netstatで見ても3306は無いし、50431,50432で動いてるけど・・・?
ちょっと別サーバーからもテスト。

とりあえず完了。

This entry was posted on 火曜日, 12月 13th, 2011 at 10:43 PM and is filed under mysql. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Unityによるゲームを公開しております iOS/Android
https://itunes.apple.com/jp/app/lost-knight-3d-action/id900917032
https://play.google.com/store/apps/details?id=com.groundroad.runknight

各種開発支援・機能開発等小さいお仕事などもお請けしております。
unity開発支援, PHPシステム開発, javascript/html5 フロント開発, titanium mobileアプリ開発
お気軽にお問い合わせください
大崎・五反田近郊での対面でのお打ち合わせはいつでも可能です。