LoginSignup
0
0

More than 1 year has passed since last update.

MySQLのレプリケーション

Posted at

■mysql生データ
/usr/local/mysql/data

■slow log query
sudo ls /var/lib/mysql
合計 28696
-rw-rw---- 1 mysql mysql 5242880 9月 18 11:02 2014 ib_logfile0
-rw-rw---- 1 mysql mysql 18874368 9月 18 11:02 2014 ibdata1 ←innodbのデータファイル
drwxr-xr-x 5 mysql mysql 4096 9月 18 11:02 2014 .
-rw-rw---- 1 mysql mysql 183 9月 18 11:02 2014 mysql-slow.log ←スローログクエリ
srwxrwxrwx 1 mysql mysql 0 9月 18 11:02 2014 mysql.sock
-rw-rw---- 1 mysql mysql 5242880 9月 18 11:02 2014 ib_logfile1
drwx------ 2 mysql mysql 4096 9月 18 11:02 2014 mysql
drwx------ 2 mysql mysql 4096 9月 18 11:02 2014 performance_schema
drwx------ 2 mysql mysql 4096 9月 18 11:02 2014 test
drwxr-xr-x 18 root root 4096 9月 18 10:47 2014 ..

mysqldumpslow -s t mysql-slow.log

■レプリケーション設定手順
・構成内容
書き込み(マスタ)、読み込み(スレーブ)構成

■レプリケーションユーザの作成
・マスタ側設定
192.168.23.0/24内のネットワークで許可する場合
ユーザ名:repl
パスワード:slavepass
mysql > GRANT REPLICATION SLAVE ON . TO 'repl'@'192.168.23.0/255.255.255.0' IDENTIFIED BY 'slavepass’;

■マスタ設定
vim /etc/my.cnf

[mysqld]
log-bin=mysql-bin
server-id=1001

※設定後mysqldの再起動
※server-idはint整数値ならOKだがid1,2はデフォルト値とバッティングするので使用しない

■スレーブ設定
[mysqld]
server-id=1002
※mysql設定後は再起動

■マスタデータベースでのmysqldumpによるデータのダンプ
操作の開始前にすべてのDBデータ更新cronなどを停止する

mysql > FLUSH TABLES WITH READ LOCK;
mysql > SHOW MASTER STATUS;

テーブルをロックした状態で、File(バイナリログ名)とPosition(ファイル内の現在位置)の値をメモしておく

mysql > SHOW MASTER STATUS;
+---------------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------------+----------+--------------+------------------+
| example-replication.000002 | 98 | | |
+---------------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

※バイナリロギングを行っていなかった場合はEmptyが返るので、Fileは空文字、Positionは4となる

mysqldump -u ユーザ名 -p --all-databases --lock-all-tables > mysqldump.db
※必ずテーブルロック中にやる

mysql > UNLOCK TABLES;

■スレーブの設定
mysql -u ユーザ名 -p < mysqldump.db
ダンプデータからDBを復元する

マスタサーバー情報を設定する
mysql > CHANGE MASTER TO
MASTER_HOST='example',
MASTER_USER='repl',
MASTER_PASSWORD='slavepass',
MASTER_LOG_FILE='example-replication.000002',
MASTER_LOG_POS=98;

■レプリケーションの開始
mysql > START SLAVE;

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0