1. 主从mysql server的工作原理:(如图及其过程分析) 过程: Mysql的复制(replication)是一个异步的复制,从一个Mysql instace(称之为Master)复制到另一个Mysql instance(称之Slave)。实现整个复制操作主要由三个进程完成的,其中两个进程在Slave(Sql进程和IO进程),另外一个进程在 Master(IO进程)上。 要实施复制,首先必须打开Master端的binary log(bin-log)功能,否则无法实现。因为整个复制过程实际上就是Slave从Master端获取该日志然后再在自己身上完全顺序的执行日志中所记录的各种操作。 复制的基本过程如下: (1)Slave上面的IO进程连接上Master,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容; (2)Master接收到来自Slave的IO进程的请求后,通过负责复制的IO进程根据请求信息读取制定日志指定位置之后的日志信息,返回给Slave 的IO进程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息已经到Master端的bin-log文件的名称以及bin-log的位置; (3)Slave的IO进程接收到信息后,将接收到的日志内容依次添加到Slave端的relay-log文件的最末端,并将读取到的Master端的 bin-log的文件名和位置记录到master-info文件中,以便在下一次读取的时候能够清楚的高速Master“我需要从某个bin-log的哪个位置开始往后的日志内容,请发给我”; (4)Slave的Sql进程检测到relay-log中新增加了内容后,会马上解析relay-log的内容成为在Master端真实执行时候的那些可执行的内容,并在自身执行。 好了,了解了其原理后就让我们来安装mysql及配置主从mysql 服务器吧 为了使用方便,和使mysql的功能更优一点我们使用二进制包安装,下载地址(需要注册,免费):http://www.mysql.com/downloads/mysql/ 2. 二进制安装mysql(过程不做详细解释): #解压包及做链接 tar xvf mysql-5.1.50-linux-i686-glibc23.tar.gz /usr/local cd /usr/local ln -sv mysql-5.1.50-linux-i686-glibc23.tar.gz mysql cd mysql #增加用户及该权限(-r :加一系统用户) groupadd mysql useradd -g mysql -s /sbin/nologin -M -r mysql mkdir /mysql/data chown -R mysql.mysql /mysql/data cd /usr/local/mysql chown mysql:mysql . -R # 初始化mysql配置 scripts/mysql_install_db --user=mysql --datadir=/mysql/data chown root . -R chown mysql data/ -R cp support-files/my-large.cnf /etc/my.cnf vim /etc/my.cnf datadir = /mysql/data #加入这一行 # 启动mysql bin/mysqld_safe --user=mysql & netstat -nutlp | grep 3306 # 使其可以使用mysql命令 vim /etc/profile #add PATH=$PATH:/usr/local/mysql/bin . /etc/profile #重读配置文件 # 加载库函数 vim /etc/ld.so.conf.d/mysql.conf #add /usr/local/mysql/lib ldconfig -v ln -sv /usr/local/mysql/include /usr/include/mysql ls /usr/include/mysql/ #把mysql加入开机启动 cp support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on service mysqld restart 3. Mysql装好了我们就来实现master 和 slave mysql server架构 主:192.168.0.192 station192.example.com 从:192.168.0.193 station193.example.com Master端的配置: vim /etc/my.cnf #确保有一下两行,并开启 log_bin = mysql-bin server_id = 24 授权可以来读取日志文件的用户: mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENTON *.* -> TO tom@'192.168.0.%' IDENTIFIED BY 'password'; 查看一下主mysql的状态(结果能不一样,已使用情况而定) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 108 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 从(slave)端的设置: vim /etc/my.cnf #确保有一下几行 log_bin = mysql-bin server_id = 2 relay_log = mysql-relay-bin log_slave_updates = 1 read_only = 1 定义怎样连接master mysql mysql> CHANGE MASTER TO MASTER_HOST='station192.example.com', -> MASTER_USER='tom', -> MASTER_PASSWORD='password', -> MASTER_LOG_FILE='mysql-bin.000001', -> MASTER_LOG_POS=0; 4.查看状态 #查看从服务器的状态: mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Master_Host: station192.example.com Master_User: tom Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 5 Relay_Log_File: mysql-relay-bin.000001 Relay_Log_Pos: 5 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No ………………. mysql> START SLAVE; 注意:这个命令的不能有错误产生 #再次查看状态 mysql> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: station192.example.com Master_User: tom Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 175 Relay_Log_File: mysql-relay-bin.000001 Relay_Log_Pos: 175 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes ……………………… 5.查看进程 查看Master(IO进程): mysql> SHOW PROCESSLIST\G *************************** 1. row *************************** Id: 24 User: tom Host: station193.example.com:54831 db: NULL Command: Binlog Dump Time: 610237 State: Has sent all binlog to slave; waiting for binlog to be updated Info: NULL 查看Slave(Sql进程和IO进程): mysql> SHOW PROCESSLIST\G *************************** 1. row *************************** Id: 12 User: system user Host: db: NULL Command: Connect Time: 611116 State: Waiting for master to send event Info: NULL *************************** 2. row *************************** Id: 13 User: system user Host: db: NULL Command: Connect Time: 33 State: Has read all relay log; waiting for the slave I/O thread to update it Info: NULL 注意1.row 是I/O进程 , 2.row是sql进程,已经空闲33秒 forward from http://www.uml.org.cn/sjjm/201009073.asp |