准备工作:
主从机上作操作----关闭selinux和iptables或者开放3306端口(iptables -A INPUT -p tcp --dport 3306 -j ACCEPT iptables -A OUTPUT -p tcp --sport 3306 -j ACCEPT),时间同步ntpdate time.windows.com
mysql_1信息:主
[root@mysql_1 ~ 10:30 &6]#cat /etc/redhat-release ;uname -r ;ifconfig eth0 |grep 'inet addr' |awk -F ':' '{print $2}' |echo ip:`awk -F ' ' '{print $1}'` CentOS release 6.5 (Final) 2.6.32-431.el6.i686 ip:172.31.12.4
mysql_2信息:从
[root@mysql_2 ~ 11:02 &18]#cat /etc/redhat-release ;uname -r ;ifconfig eth1 |grep 'inet addr' |awk -F ':' '{print $2}' |echo ip:`awk -F ' ' '{print $1}'` CentOS release 6.5 (Final) 2.6.32-431.el6.i686 ip:172.31.12.233
下载mysql并解压:这里只演示从机
cd /usr/local/src/
[root@mysql_2 ~ 11:19 &20]#wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz [root@mysql_2 src 11:24 &23]#tar -zxvf mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
移动解压目录到/usr/loca/下:创建mysql目录
[root@mysql_2 src 11:26 &26]#mv mysql-5.1.40-linux-i686-icc-glibc23 /usr/local/mysql
创建mysql用户:禁止mysql用户登录bash登录
[root@mysql_2 src 13:13 &27]#useradd -s /sbin/nologin mysql
创建mysql安装目录,并修改为mysql属主属组:
[root@mysql_2 src 13:14 &28]#mkdir -p /data/mysql [root@mysql_2 src 13:16 &29]#chown -R mysql:mysql /data/mysql/
进入到/usr/local/mysql目录下初始化mysql:
[root@mysql_2 src 13:16 &30]#cd /usr/local/mysql/ [root@mysql_2 mysql 13:19 &31]#./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
拷贝mysql主配置文件到/etc/目录下并重命名为my.cnf:
[root@mysql_2 mysql 13:21 &32]#cp support-files/my-large.cnf /etc/my.cnf
拷贝mysql的启动脚本到/etc/init.d/目录下并重命名为mysqld并讲权限修改为755:
[root@mysql_2 mysql 13:27 &33]#cp support-files/mysql.server /etc/init.d/mysqld [root@mysql_2 mysql 13:29 &35]#chmod 755 /etc/init.d/mysqld
编辑启动脚本:
[root@mysql_2 mysql 13:30 &36]#vim /etc/init.d/mysqld 指定mysql的安装目录:datadir=/data/mysql
把启动脚本加入到服务项中,并设置为开机启动:
[root@mysql_2 mysql 13:33 &37]#chkconfig --add mysqld [root@mysql_2 mysql 13:35 &38]#chkconfig mysqld on
修改从配置文件服务id号为2:
[root@mysql_2 mysql 13:38 &40]#vim /etc/my.cnf server-id = 1 改为 server-id = 2
设置mysql 命令路径:vim /etc/profile,在末尾加上并刷新服务:
[root@mysql_2 mysql 13:39 &41]#echo PATH=$PATH:/usr/local/mysql/bin >> /etc/profile [root@mysql_2 mysql 13:41 &43]#source /etc/profile
启动mysql服务:
[root@mysql_2 mysql 13:42 &45]#service mysqld start
以上步骤,分别在两台服务器上配置好!
分别给主从mysql设置密码:
[root@mysql_2 mysql 13:44 &46]#mysqladmin -uroot password '123456'
登陆mysql主、从服务器,分别建立数据库 db1:
[root@mysql_2 mysql 13:47 &47]#mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.40-log MySQL Community Server (GPL) Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database db1; 创建db1库 Query OK, 1 row affected (0.00 sec)
创建完成,退出数据库:
mysql> \q Bye
备份mysql主服务器上的mysql库,并还原到db1库中:
[root@mysql_1 mysql 14:48 &17]#mysqldump -uroot -p123456 mysql > mysql.sql_$(date +%F) [root@mysql_1 mysql 14:48 &18]#mysqldump -uroot -p123456 db1 < mysql.sql_2014-07-24
备份主服务器的db1并拷贝到从服务器上
[root@mysql_1 mysql 14:53 &20]#mysqldump -uroot -p123456 db1 > db1.sql_$(date +%F) [root@mysql_1 mysql 14:55 &22]#scp db1.sql_2014-07-24 172.31.12.233:/usr/local/mysql/ [email protected]'s password: db1.sql_2014-07-24 100% 466KB 465.9KB/s 00:00
登陆主服务器,给从服务器授权,创建从服务器用户登陆,并锁定数据库表及查看状态:
[root@mysql_1 mysql 21:13 &24]#mysql -uroot -p123456 #登陆 mysql> grant replication slave on *.* to 'silence'@'172.31.12.233' identified by 'silence.com'; #为从机172.31.12.233创建用户silence,并授权登陆 mysql> flush privileges; #刷新授权 mysql> flush tables with read lock; #锁定表 mysql> show master status; #查看状态,记住下面的数字 +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000005 | 475202 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
在从服务器上恢复db1:
[root@mysql_2 mysql 14:50 &49]#mysql -uroot -p123456 db1 < db1.sql_2014-07-24
登陆从服务器,并停止slave服务;
[root@mysql_2 mysql 21:59 &50]#mysql -uroot -p123456 mysql> slave stop;
配置从连主信息:
mysql> change master to master_host='172.31.12.4', master_port=3306, master_user='silence', master_password='silence.com', master_log_file='mysql-bin.000005',master_log_pos=475202; #master_host='主服务器IP地址' #master_port=主服务器mysql端口 #master_user='主授权的从登陆用户' #master_password='主授权从服务器用户登录的密码' #master_log_file='主服务器状态file对应文件' #master_log_pos='主服务器状态Position对应的数字'
启动从服务器slave服务:
mysql> slave start;
到主服务器解锁之前锁定的表:
mysql> unlock tables;
查看从服务器状态:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.31.12.4
Master_User: silence
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 475202
Relay_Log_File: mysql_2-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes #确认这两条信息为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: 475202
Relay_Log_Space: 408
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
ERROR:
No query specified
主从测试:登陆主服务器上在数据库 db1下创建表db2 :
mysql> use db1; mysql> create table db2 (id int(5),name char(10)); mysql> show tables;
mysql> show tables;
+---------------------------+
| Tables_in_db1 |
+---------------------------+
| columns_priv |
| db |
| db2
查看从服务器db1库内是否有db2表:
mysql> use db1; mysql> show tables;
mysql> show tables;
+---------------------------+
| Tables_in_db1 |
+---------------------------+
| columns_priv |
| db |
| db2
在操作主从复制需要特别注意的一点是:如果有对mysql进行重新配置,需要重启mysql 服务的话,一定要先停止从;一旦主先被停掉,有可能主从架构就会遭到破坏。同时注意,主从启动顺序,必须先启动主,然后启动从!