一、MySQL安装与配置
具体的安装步骤,建议参考我的另一篇文章 http://blog.csdn.net/a2350314814/article/details/53317599
二、MySQL主从复制
主数据库服务器:192.168.40.129
从数据库服务器:192.168.40.130
2.1 配置主服务器:
2.1.1 启动mysql服务
# service mysqld start
2.1.2 登录mysql服务器
# cd /usr/local/mysql
# mysql -uroot -p'password'
2.1.3 授权给从数据库 192.168.40.130
mysql> GRANT REPLICATION SLAVE ON *.* to 'yourname'@'192.168.40.130' identified by 'password';
2.1.4 查询主数据库状态
mysql> show master status;
记下File和Position的值,在后面配置从服务器时候要用到。
2.2 配置从服务器
2.2.1 修改从服务器的配置文件(将 server-id = 1修改为 server-id = 10,并确保这个ID没有被别的MySQL服务所使用)
# cd /etc
# vim my.cnf
2.2.2 启动mysql服务
# service mysqld start
2.2.3 登录mysql服务器
# cd /usr/local/mysql
# mysql -uroot -p'password'
2.2.4 执行同步SQL语句
mysql> change master to
master_host='192.168.40.129',
master_user='yourname',
master_password='password',
master_log_file='mysql-bin.0000011,
master_log_pos=4582;
2.2.5 启动Slave同步进程
mysql> start slave;
2.2.6 主从同步检查
mysql> show slave status\G
*********************** 1. row ************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.40.129
Master_User: copy
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000011
Read_Master_Log_Pos: 5004
Relay_Log_File: bogon-relay-bin.000002
Relay_Log_Pos: 494
Relay_Master_Log_File: mysql-bin.000011
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
....................................省略..............................................
Auto_Position: 0
1 row in set (0.00 sec)
Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。
注:如果主服务器已经存在应用数据需要将主服务器的数据文件复制到从服务器
2.3 验证主从复制
2.3.1 主服务器
在主服务器数据库test上创建表first_test
mysql> use test;
mysql> create table first_test(id int(3),name char(10));
在表first_test中插入记录
mysql> insert into first_test values (1,'test1');
2.3.2 从服务器
mysql> use test
mysql> show tables;
数据库表first_test已经自动创建
mysql> select * from first_test;
数据也已经自动插入
到此,MySQL主从复制功能就完成了。