MySQL复制在同一台机器上

这怎么解释MySQL在同一台机器上的复制。



你有很大的命中,数据库驱动的站点和性能,冗余,安全性现在被考虑在内。 DBA现在由很多手指输入,寻求一些很好的解决方案,使幸福的脸孔。所以这里有一些单词,如集群,复制,备份,故障切换等。那么这些是什么?我们目前正在基于Web的数据库驱动的站点讨论复制,以获得大型命中。

什么复制是为什么?


复制允许DBA将master的数据库克隆到具有相同数据库的另一个数据库服务器。这包括主和从属身份。从站使自己成为主数据库服务器及其数据库的精确副本。主人和Minion之间可能有一对一,一对多的关系。从机连续读取master处的二进制日志(binlog存储在master数据库服务器上编写的查询),并将其输入到其从属数据库服务器。

复制的不是:


考虑备份,性能,安全性和冗余性的解决方案。还有其他的技术。

我们目前看到在同一台机器上复制MySQL服务器,即主机和从机在同一机器上运行。我们还将讨论一些复制问题。

先决条件:


MySQL服务器4.1.12或以上的源格式。从源代码格式从http://mysql.com下载它们。
您可以轻松地从http://mysql.com下载mysql客户端或将mysql放在环境变量中。其他明智的mysql将不得不从相应的主/从目录调用。
一些Linux发行版。 (我使用Fedora Core 2)。
删除mysql服务器的依赖关系

MySQL主安装和配置:


下载mysql-4.1.12源在/ misc文件夹
tar xzvf mysql-4.1.12.tar.gz
cd /misc/mysql-4.1.12
./configure --prefix=/usr/local/mysql-master
make
make install
cd /usr/local/mysql-master/bin
./mysql_install_db
(它将创建一个var文件夹)
cd ../var
cp /misc/mysql-4.1.12/support-files/my-medium.cnf my.cnf
cd ..
groupadd mysql
useradd -g mysql mysql
chown -R root .
chown -R mysql var
chgrp -R mysql .

[mysqld]
port = 3306
socket = /usr/local/mysql-master/mysql.sock

#skip-networking // we have skip this in our case as we are doing both master and slave on same machine.

# Replication Master Server (default)
# binary logging is required for replication
log-bin

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1

根据您的要求配置其他设置或保持相同,它将工作! :)
现在启动mysql服务器:
cd /usr/local/mysql-master/bin
./mysqld_safe --defaults-file=/usr/local/mysql-master/var/my.cnf &;

MySQLMinion:


现在在不同的目录中提取mysql-4.1.12.tar.gz,
cd /opt/mysql-4.1.12
./configure --prefix=/usr/local/mysql-slave
make
make install
cd /usr/local/mysql-slave
cd bin
./mysql_install_db
(它将创建一个var文件夹)
cd ../var
cp /opt/mysql-4.1.12/support-files/my-medium.cnf my.cnf
cd ..
groupadd mysql
useradd -g mysql mysql
chown -R root .
chown -R mysql var
chgrp -R mysql .

在var文件夹中编辑my.cnf
[mysqld]
port = 3307
socket = /usr/local/mysql-slave/var/mysql.sock

#skip-networking

server-id = 2

# The replication master for this slave - required
master-host = localhost
master-user = slavedb
master-password = q1w2e3r4t5
master-port = 3306
现在启动mysql服务器:
cd /usr/local/mysql-slave/bin
./mysqld_safe --defaults-file=/usr/local/mysql-slave/var/my.cnf &

配置复制:


通过以下方式连接到mysql master:
mysql --sock=/usr/local/mysql-master/mysql.sock

在主人创建帐户为Minion:


mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO slavedb@"192.168.1.27/255.255.255.0" identified by 'q1w2e3r4t5'; Query OK, 0 rows affected (0.28 sec)

通过以下方式连接到mysql slave:
mysql --sock=/usr/local/mysql-slave/mysql.sock

mysql> slave start;
查询OK,0行受影响,1个警告(0.04秒)

测试:


在master处连接mysql:

mysql> show master status\G;
*************************** 1. row ***************************
File: adam-bin.000001
Position: 227
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.01 sec)

ERROR:
No query specified
在Minion连接mysql

mysql> show slave status\G;
*************************** 1. row ***************************
 Slave_IO_State: Connecting to master
Master_Host: localhost
Master_User: slavedb
Master_Port: 3306
Connect_Retry: 60

Master_Log_File: adam-bin.000001

Read_Master_Log_Pos: 4
Relay_Log_File: adam-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: adam-bin.000001

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: 4
Relay_Log_Space: 4
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: NULL
1 row in set (0.00 sec)

ERROR:
No query specified
masterlog的binlog文件的位置必须相同,在mysql slave中显示从属状态\ G;

如果一切顺利,您将在同一系统上运行一对一的主从关系的工作副本。


一些问题:


由于问题的数量,复制可能会失败。我分享了我的一些复制经验。

问题:复制失败,主人下来。


由于许多原因,主人可能会失败。检查数据库中的文件限制,SQL查询和磁盘使用。如果有任何原因显示,修复,重新启动mysqld并检查主状态:

mysql> show master status\G;
*************************** 1.行******************** *******

adam-bin.000003

职位:227

Binlog_Do_DB:
Binlog_Ignore_DB:
1排集(0.01秒)


现在检查slave:显示slave状态:
mysql> show slave status\G;
*************************** 1.行******************** *******
Slave_IO_State:连接到主
Master_Host:localhost
Master_User:slavedb
Master_Port:3306
Connect_Retry:60

Master_Log_File:adam-bin.000001

Read_Master_Log_Pos:4


Relay_Log_File:adam-relay-bin.000001
Relay_Log_Pos:4
Relay_Master_Log_File:adam-bin.000001

Slave_IO_Running:否

Slave_SQL_Running:是的


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:4
Relay_Log_Space:4
直到条件:无
Until_Log_File:
Until_Log_Pos:0
Master_SSL_Allowed:不
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:NULL
1排(0.00秒)

如上面的值所示,主机已经增加到第三个binlog,而从机仍然指向binlog1。所以通过在mysql slave连接来改变这个值
mysql>; stop slave;
mysql> change master to master_log_file='adam-bin.000003', master_log_pos=227;
mysql> start slave;

现在检查显示从站状态\ G;它的工作很好。

问题2:如果出现重复错误键,则在从站


在Minion
Mysql> show slave status\G;
*************************** 1.行******************** *******
Slave_IO_State:等待主人发送事件
Master_Host:10.0.0.152
Master_User:repl
Master_Port:3306
Connect_Retry:60
Master_Log_File:adam-bin.000048
Read_Master_Log_Pos:317714810
Relay_Log_File:db4-relay-bin.000001
Relay_Log_Pos:290512385
Relay_Master_Log_File:adam-bin.000048
Slave_IO_Running:是的

Slave_SQL_Running:否

Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:1062

Last_Error:错误'重复条目'dekq5g820avnfdmar5qi9dkhv3'为关键字1'查询。 默认数据库:'session_sql'。 查询:'INSERT INTO sessi ons5 VALUES('dekq5g820avnfdmar5qi9dkhv3',UNIX_TIMESTAMP(NOW())+ 18000,'redir ect | i:1;')'

Skip_Counter:0
Exec_Master_Log_Pos:290512419
Relay_Log_Space:317714776
直到条件:无
Until_Log_File:
Until_Log_Pos:0
Master_SSL_Allowed:不
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:NULL
1排(0.00秒)

这意味着,由于不可靠的主,Minion被读取,或者不可用的Minion,主人被更新,所以查询得到两个主键,一个条目是一个混乱。

所以修正它是:
在Minion:
Mysql> set global sql_slave_skip_counter=1;
Mysql> start slave;
Mysql> show slave status\G;
这将显示与主站同步的值。固定。

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏