mysql主主搭建配置

mysql主主搭建配置:

這裏我們的主主架構爲192.168.1.12  192.168.1.13

192.168.1.12服務器上執行以下:

vim   /etc/my.cnf

[mysqld]

 server-id = 11

 log-bin = mysql-bin

 auto-increment-increment = 2

 auto-increment-offset = 1

 relay-log=mysql-relay           

 relay-log-index=mysql-relay.index

mysql>grant replication client,replication slave on *.* to [email protected] identified by '135246';

 mysql>flush privileges;

 

 

192.168.1.13服務器上執行以下:

vim   /etc/my.cnf

[mysqld]

 server-id = 12

 log-bin = mysql-bin

 auto-increment-increment = 2

 auto-increment-offset = 2

 relay-log=mysql-relay           

 relay-log-index=mysql-relay.index

mysql>grant replication client,replication slave on *.* to [email protected] identified by '135246';

 mysql>flush privileges;

 

如果此時兩臺服務器均爲新建立,且無其它寫入操作,各服務器只需記錄當前自己二進制日誌文件及事件位置,以之作爲另外的服務器複製起始位置即可

192.168.1.12

mysql> show master status;

 +------------------+----------+--------------+------------------+

 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

 +------------------+----------+--------------+------------------+

 | mysql-bin.000004 |      360 |              |                  |

 +------------------+----------+--------------

192.168.1.13

mysql> show master status;

 +------------------+----------+--------------+------------------+

 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

 +------------------+----------+--------------+------------------+

 | mysql-bin.000005 |      107 |              |                  |

 +------------------+----------+--------------+------------------+

各服務器接下來指定對另一臺服務器爲自己的主服務器即可:

192.168.1.12

mysql>change master to \

 master_host='192.168.1.13',

 master_user='mysql0',

 master_password='135246',

 master_log_file='mysql-bin.000005',

 master_log_pos=107;

 

 

192.168.1.13

mysql>change master to \

 master_host='192.168.1.12',

 master_user='mysql0',

 master_password='135246',

 master_log_file='mysql-bin.000004',

 master_log_pos=360;

 

然後mysql> start slave;

到此主主機構已經完成!

mysql>show tables

mysql> create table user( id int auto_increment, name varchar(20), primary key (id));

Query OK, 0 rows affected (0.03 sec)

mysql> insert into user(name) values ('zhangsan'), ('lisi');

Query OK, 2 rows affected (0.03 sec)

Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into user(name) values ('zhang'), ('li');

Query OK, 2 rows affected (0.03 sec)

Records: 2  Duplicates: 0  Warnings: 0

mysql> select id from user;

+----+

| id |

+----+

|  1 |

|  3 |

|  5 |

|  7 |

+----+

mysql> show variables like '%auto_incr%';

+--------------------------+-------+

| Variable_name            | Value |

+--------------------------+-------+

| auto_increment_increment | 2     |

| auto_increment_offset    | 1     |

+--------------------------+-------+

2 rows in set (0.00 sec)

修改AUTO_INCREMENT_OFFSET 值,改變自增器起始值

mysql > set auto_increment_offset=60;

Query OK, 0 rows affected (0.00 sec)

 

是否啓用了日誌

mysql>show variables like 'log_%';

怎樣知道當前的日誌

mysql> show master status;

顯示二進制日誌數目

mysql> show master logs;

看二進制日誌文件用mysqlbinlog

shell>mysqlbinlog mail-bin.000001

或者shell>mysqlbinlog mail-bin.000001 | tail

在配置文件中指定log的輸出位置.

Linux 的配置文件爲 my.cnf ,一般在 /etc 下。

linux下:

Sql代碼

# [mysqld] 中輸入

#log

log-error=/usr/local/mysql/log/error.log

log=/usr/local/mysql/log/mysql.log

long_query_time=2

log-slow-queries= /usr/local/mysql/log/slowquery.log

 

# [mysqld] 中輸入 #log

log-error=/usr/local/mysql/log/error.log

log=/usr/local/mysql/log/mysql.log

long_query_time=2

log-slow-queries= /usr/local/mysql/log/slowquery.log

 

注:

在配置中會遇到的問題:

[root@www ~]# service mysqld restart

MySQL server PID file could not be found!                  [FAILED]

這裏由於數據是空的我採用的解決方案是:

cd /usr/local/mysql

[root@www mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/data

Installing MySQL system tables...

120718 15:17:51 [Warning] You need to use --log-bin to make --binlog-format work.

OK

Filling help tables...

120718 15:17:52 [Warning] You need to use --log-bin to make --binlog-format work.

OK

當然由於此mysql以前做過mysql的從服務器所以出現warning

這裏的解決方案是:

vim /etc/my.cnf

把原來註釋掉的二進制日誌打開即可

 

UTF-8UNICODE的一種變長字符編碼又稱萬國碼,由Ken Thompson1992年創建。現在已經標準化爲RFC 3629UTF-816字節編碼UNICODE字符。用在網頁上可以同一頁面顯示中文簡體繁體及其它語言(如日文,韓文)

這裏我們將mysql的字符集修改爲utf8

修改my.cnf配置文件在【mysqld】下加

character-set-server=utf8

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章