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-8是UNICODE的一种变长字符编码又称万国码,由Ken Thompson于1992年创建。现在已经标准化为RFC 3629。UTF-8用1到6个字节编码UNICODE字符。用在网页上可以同一页面显示中文简体繁体及其它语言(如日文,韩文)
这里我们将mysql的字符集修改为utf8
修改my.cnf配置文件在【mysqld】下加
character-set-server=utf8