logは/var/lib/mysqlのmysql-bin.xxxxにあるがバイナリなためツールを使用する
# mysqlbinlog mysql-bin.000002
#080124 18:05:01 server id 2 end_log_pos 127 Query thread_id=11 exec_time=0 error_code=0 SET TIMESTAMP=1201165501; INSERT INTO `foo` VALUES (1,'Okada'),(2,'Okumura'),(3,'Yamada');
のようなログが見えいる
mysqlbinlog: unknown variable 'default-character-set=utf8' のようなエラーが出たらmy.cnfの
[client] #default-character-set = utf8 <=コメントアウトする
ログファイルの種類
ログの種類 | オプション | デフォルトのファイル名 | 設定ファイルの出力ファイル名指定 | 内容 |
エラーログ | --log-error | ホスト名.err | log-error=ファイル名 | MySQLサーバー起動時、稼動時、停止時のエラーやメッセージ |
一般クエリーログ | --log | ホスト名.log | log=ファイル名 | 接続や実行クエリーについての情報 |
スロークエリーログ | --log-slow-queries | ホスト名-slow.log | log-slow-queries=ファイル名 | long_query_timeで指定の秒数を超えるクエリー |
バイナリログ | --log-bin | ホスト名-bin.数値 | log-bin=ファイル名 | 更新を伴うクエリーが記録され、バックアップやレプリケーションにしようされる |
InnoDBのログ | デフォルトで生成 | ib_logfile数値 | - | InnoDBのログファイル |
BackUpをとるには同じDBを作製しておく必要がある。
/etc/my.cnf
[mysqld] ## REPLICATION MASTER SETTING ## user = mysql server-id = 1 log-bin character-set-server = utf8
設定内容
SLAVE IP :10.1.4.142~
User :repl
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@10.1.4.142 IDENTIFIED BY '*****';
現在のバイナリログの状態を確認
(Master側でDBをLock) <=変更が多くないときは不要かも
mysql> FLUSH TABLES WITH READ LOCK;
logファイルと、logポジションを表示
mysql> SHOW MASTER STATUS; <=(1) +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000024 | 98 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.01 sec)
DBをBackUpしてスレーブ側にコピーしてからUnLock
mysql> UNLOCK TABLES;
/etc/my.cnf
[mysqld] ## REPLICATION SLAVE SETTING server-id = 2 character-set-server = utf8 read-only <=アプリケーションからの変更禁止
一度CHANGE MASTER TO は実行すればOK
mysql> CHANGE MASTER TO -> MASTER_HOST='10.99.99.202', -> MASTER_PORT=3306, -> MASTER_USER='repl', -> MASTER_PASSWORD='*******', -> MASTER_LOG_FILE='mysql-bin.000024', <=MASTER側で表示したSTATUSの結果を記入 -> MASTER_LOG_POS=98; <=MASTER側で表示したSTATUSの結果を記入 Query OK, 0 rows affected (0.04 sec)
スレーブの起動
mysql> START SLAVE; Query OK, 0 rows affected (0.01 sec)
スレーブの状態の表示
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.99.99.202 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000024 Read_Master_Log_Pos: 98 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 235 Relay_Master_Log_File: mysql-bin.000024 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 98 Relay_Log_Space: 235 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 1 row in set (0.00 sec) ERROR: No query specified
スレーブの停止
mysql> STOP SLAVE; Query OK, 0 rows affected (0.01 sec)
CHANGE MASTER TO を実行すると以下のファイルができるので、その情報でその後再起動は問題なく動作する。
# less /var/lib/mysql/master.info 14 mysql-bin.000024 98 10.99.99.202 repl je2ism 3306 60 0
# less /var/lib/mysql/relay-log.info ./localhost-relay-bin.000002 235 mysql-bin.000024 98
テーブル名の変更
ALTER TABLE 古いテーブル名 RENAME 新しいテーブル名;
mysql> ALTER TABLE ism_test RENAME ism_test2; Query OK, 0 rows affected (0.00 sec)
テーブルの複製
create table 新しいテーブル名 as select * from 元のテーブル名;
mysql> create table ism_test as select * from ism_test2; Query OK, 1 row affected (0.03 sec) Records: 1 Duplicates: 0 Warnings: 0
必要なレコードだけ残しコピーすることも可
create table 新しいテーブル名 as select * from 元のテーブル名 where (条件);
入れ子表現
入れ子は()で囲む
テーブルism_shouhinのすべてのIDフィールドの値と等しい、テーブルism_uriageのIDフィールドを抽出
mysql> select * from ism_uriage where ID in (select ID from ism_shouhin); +----+---------------------------+------+-----------+-----------+------+ | ID | date | uid | shouhinid | uriagesuu | etc | +----+---------------------------+------+-----------+-----------+------+ | 1 | 2008/09/27 (Sat) 23:41:05 | 3 | 14 | 2 | NULL | | 2 | 2008/09/27 (Sat) 23:41:05 | 3 | 16 | 4 | NULL | | 3 | 2008/09/27 (Sat) 23:41:05 | 3 | 18 | 6 | NULL | | 5 | 2008/09/27 (Sat) 23:45:02 | 3 | 11 | 1 | NULL | (略) | 33 | 2008/09/28 (Sun) 01:48:04 | 2 | 16 | 4 | NULL | | 34 | 2008/09/28 (Sun) 01:50:02 | 2 | 20 | 2 | NULL | | 35 | 2008/09/28 (Sun) 01:50:35 | 2 | 23 | 2 | NULL | +----+---------------------------+------+-----------+-----------+------+ 32 rows in set (0.01 sec)
フィールドの合計を別名で表示
mysql> select uid, uriagesuu, uid+uriagesuu as sum from ism_uriage where shouhinid=9; +------+-----------+------+ | uid | uriagesuu | sum | +------+-----------+------+ | 3 | 2 | 5 | | 2 | 2 | 4 | | 2 | 4 | 6 |
フィールドの文字の連結
mysql> select uid, uriagesuu, concat(uid, uriagesuu) as gousei from ism_uriage where shouhinid=9; +------+-----------+--------+ | uid | uriagesuu | gousei | +------+-----------+--------+ | 3 | 2 | 32 | | 2 | 2 | 22 | | 2 | 4 | 24 |
数値比較して結果を表示
mysql> select uid, uriagesuu, strcmp(uid, uriagesuu) as hikaku from ism_uriage where shouhinid=9; +------+-----------+--------+ | uid | uriagesuu | hikaku | +------+-----------+--------+ | 3 | 2 | 1 | | 2 | 2 | 0 | | 2 | 4 | -1 | | 2 | 4 | -1 | | 2 | 4 | -1 |
select in
mysql> select * from ism_shouhin where ID in (10,12,15); +------+---------------+------+--------+------+----------+------+ | ID | name | grp | kakaku | url | status | etc | +------+---------------+------+--------+------+----------+------+ | 10 | ビール 500ml | 201 | 360 | | | | | 12 | 発泡酒 500ml | 201 | 164 | | | | | 15 | ワイン (ロゼ) | 201 | 680 | | 販売終了 | | +------+---------------+------+--------+------+----------+------+ 3 rows in set (0.00 sec)
否定形 [not in]
mysql> select * from ism_shouhin where ID not in (10,12,15); +------+-------------------+------+--------+-----------------+------ | ID | name | grp | kakaku | url | status | etc | +------+-------------------+------+--------+-----+-----------+------ | 9 | ビール 350ml | 201 | 250 | | | | | 11 | 発泡酒 350ml | 201 | 134 | | | | | 13 | 焼酎 (芋) | 201 | 1048 | | | | | 14 | 焼酎 (麦) | 201 | 1148 | | | | | 16 | ワイン (白) | 201 | 650 | | | | +------+-------------------+------+--------+-----+-----------+-------
mysql> select ID,name as 名前,kakaku as 単価 from ism_shouhin where ID in(10,12,15); +------+---------------+------+ | ID | 名前 | 単価 | +------+---------------+------+ | 10 | ビール 500ml | 360 | | 12 | 発泡酒 500ml | 164 | | 15 | ワイン (ロゼ) | 680 | +------+---------------+------+ 3 rows in set (0.00 sec)
select ism_uriage.* , ism_shouhin.* from ism_uriage join ism_shouhin on ism_uriage.shouhinid=ism_shouhin.ID where ism_uriage.uid=2 order by ism_uriage.ID desc limit 3; +-----+---------------------------+------+-----------+-----------+------+------+ ----------------------------+------+--------+----------------------------------- --------------------------------------------+--------+------+ | ID | date | uid | shouhinid | uriagesuu | etc | ID | name | grp | kakaku | url | status | etc | +-----+---------------------------+------+-----------+-----------+------+------+
----------------------------+------+--------+----------------------------------- --------------------------------------------+--------+------+ | 160 | 2008/10/02 (Thu) 15:52:20 | 2 | 1 | 1 | NULL | 1 | 冷蔵庫 AB-12345(H) | 101 | 45000 | | | | | 159 | 2008/10/02 (Thu) 15:52:20 | 2 | 18 | 4 | NULL | 18 | カクテルバー(レモン) | 201 | 121 | | | | | 158 | 2008/10/02 (Thu) 15:52:20 | 2 | 21 | 2 | NULL | 21 | コンパクトカメラ(Leica C2) | 301 | 37590 | | | | +-----+---------------------------+------+-----------+-----------+------+------+ ----------------------------+------+--------+----------------------------------- --------------------------------------------+--------+------+ 3 rows in set (0.01 sec)
mysql> DROP TABLE IF EXISTS ism_test; Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create table if not exists ism_test( ID int8, date varchar(64), etc varchar(64)); Query OK, 0 rows affected (0.02 sec)
型の変更(data)
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | bigint(20) | YES | | NULL | | | date | varchar(64) | YES | | NULL | | | etc | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
mysql> alter table ism_test change date date int8; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | bigint(20) | YES | | NULL | | | date | bigint(20) | YES | | NULL | | | etc | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
名前の変更(data)
mysql> alter table ism_test change date data varchar(64); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | bigint(20) | YES | | NULL | | | data | varchar(64) | YES | | NULL | | | etc | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
書式形式
alter table <テーブル名> change <フィールド名> <新フィールド名> <型>
MySQLでは、定義さている値より多い値を入れようとすると、許容範囲まで入る。
dataが4Byte
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | int(11) | YES | | NULL | | | data | varchar(4) | YES | | NULL | | | etc | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
dataに5byte入力してみると4Byteまで入る
mysql> insert into ism_test values(1,'12345','岡田'); Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from ism_test; +------+------+------+ | ID | data | etc | +------+------+------+ | 1 | 1234 | 岡田 | +------+------+------+ 1 row in set (0.00 sec)
フィールドの型を変更すると矛盾すると値がなくなる (varchar=>int型に変更した場合)
mysql> select * from ism_test; +------+------+------+ | ID | data | etc | +------+------+------+ | 1 | 1234 | 岡田 | +------+------+------+ 1 row in set (0.00 sec)
mysql> alter table ism_test change etc etc int4; Query OK, 1 row affected, 1 warning (0.00 sec) Records: 1 Duplicates: 0 Warnings: 1
mysql> select * from ism_test; +------+------+------+ | ID | data | etc | +------+------+------+ | 1 | 1234 | 0 | +------+------+------+ 1 row in set (0.00 sec)
追加
mysql> show fields from ism_test; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | ID | int(11) | YES | | NULL | | | data | bigint(20) | YES | | NULL | | | etc | int(11) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
mysql> ALTER table ism_test add etc2 varchar(64); Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0
mysql> show fields from ism_test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | int(11) | YES | | NULL | | | data | bigint(20) | YES | | NULL | | | etc | int(11) | YES | | NULL | | | etc2 | varchar(64) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
削除
mysql> ALTER table ism_test drop etc2; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0