注:主服务器:192.168.100.245 从服务器:192.168.100.246
一、检查系统是否安装MySQL服务
1.1 rpm -qa |grep -i mysql
二、安装MySQL服务器
2.1 rpm -ivh MySQL-server-5.5.16-1.rhel4.i386.rpm MySQL-client-5.5.16-1.rhel4.i386.rpm MySQL-devel-5.5.16-1.rhel4.i386.rpm
三、启动MySQL服务
3.1 service mysqld start
四、登录root账户
4.1 mysql -h localhost -u root -p 回车输入登录密码(刚安装的时候是没有root账号的,直接输入mysql回车即可登录)
4.2 查找mysql数据库下的user用户及主机
select user,host,password from mysql.user;
4.3 删除匿名用户
Delete from mysql.user where user=’’;
4.4 设置mysql的root账户密码
Set password for [email protected]=password(‘123456’);
4.5 查看数据库
show databases;
4.6 退出mysql
Exit;
五、编辑主服务器的mysql主配置文件/etc/my.cnf
要先在从服务器的主机上新建这个用户:
mysql>grant replication slave on *.* to ‘mydata‘@192.168.100.245 identified by ‘123456‘;
在[mysqld]下添加如下
server-id=1 #主机标示,整数
log-bin=/var/lib/mysql/mysql-bin.log #确保此文件可写
master-host=192.168.100.246 #主机地址
master-user=mydata #远程登录数据库用户名称
master-pass=123456 #远程登录数据库用户密码
master-port=3306 #远程数据库服务器端口号
master-connect-retry=60 #响应时间
binlog-do-db=test #需要备份数据,多个写多行
replicate-do-db=test #指定复制到哪个库
保存退出
六、重启服务器
Service mysqld restart
七、进入mysql锁表
Mysql>flush tables with read lock;
退出
八、备份
mysqldump -hlocalhost -uroot -p3306 -p test >/home/test.sql
九、远程复制
scp test.sql [email protected]:/home/(在当前目录下复制)
十、进入mysql解锁
Mysql> unlock tables;
十一、验证:
Show master status\G; (显示如下内容)
*************************** 1. row ***************************
File: mysql-bin.000005
Position: 537
Binlog_Do_DB: test
Binlog_Ignore_DB:
1 row in set (0.00 sec)
ERROR:
No query specified
从服务器配置(192.168.100.246)
要先在主服务器新建这个用户:
mysql>grant replication slave on *.* to ‘mydata‘@192.168.100.246 identified by ‘123456‘;
十二、编辑主配置文件 /etc/my.cnf
在[mysqld]下添加如下命令
server-id=2
log-bin=/var/lib/mysql/mysql-bin.log
master-host=192.168.100.245
master-user=mydata
master-pass=123456
master-port=3306
master-connect-retry=60
binlog-do-db=test
replicate-do-db=test
保存退出
十三、重启mysql服务
Service mysql restart
十四、验证
Show Slav status \G;(显示如下内容则配置成功)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 192.168.100.245
Master_User: mydata
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 537
Relay_Log_File: mysqld-relay-bin.000007
Relay_Log_Pos: 674
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
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: 537
Relay_Log_Space: 674
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
十五、验证数据是否可以同步
主:
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| aaa |
| ccc |
+----------------+
2 rows in set (0.00 sec)
mysql> create table ooo(id int, date varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql>
从:
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| aaa |
| ccc |
| ooo |
+----------------+
3 rows in set (0.00 sec)
在从服务器进行删除:
mysql> drop table ooo;
Query OK, 0 rows affected (0.00 sec)
在主服务器:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| aaa |
| ccc |
+----------------+
2 rows in set (0.00 sec)
十六、测试完毕!