准备机器两台:
192.168.1.22 master
192.168.1.23 slave
mysql版本:5.5.21
下载: wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.5/mysql-5.5.21-linux2.6-x86_64.tar.gz
解压到 /usr/local/ 目录下,并将解压后的文件夹改名为 mysql-master,并建立软连接:
ln -s /usr/local/mysql-master /usr/local/mysql
安装过程:
1)groupadd mysql
2)mkdir /home/mysql
3)useradd -g mysql -d /home/mysql mysql
4)cd /usr/local/mysql
5)chown -R mysql .
6)chgrp -R mysql .
7) scripts/mysql_install_db --user=mysql
8)为root用户设置密码: ./bin/mysqladmin -u root password 'password'
9)cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
10) cp /usr/local/mysql/support-files/ my-huge.cnf /etc/my.cnf
测试是否安装成功: service mysqld start (没有报错即说明启动成功)
用 netstat -na | grep 3306 查看一下端口情况,或者可以通过mysql命令连上去小试一把
Master的设置
1、配置/etc/my.cnf:
[mysqld]
port=3308
server-id= 1
binlog-do-db=mydb ##要同步的数据库名为test1
log-bin=mysql-bin
2、在mysql命令行中执行如下命令创建一个专门的slave用户
GRANT FILE,select,REPLICATION SLAVE ON *.* TO 'slaver1'@'127.0.0.1' IDENTIFIED BY 'slaver1pwd'; (注意红色字部分不能为小写)
3、service mysqld restart
4、在mysql命令行中执行如下命令查看master的状态信息:
show master status;
回车即可看到如下信息:
File: mysql-bin.000305
Position: 98
Binlog_Do_DB: mydb
Binlog_Ignore_DB:
Slave的安装过程参见master的安装过程,以下说明以下配置:
由于mysql从5.1.7以后开始就不支持“master-host”类似的参数,因此可以如下配置:
[mysqld]
port=3306
server-id=2
#master-host=192.168.1.22
#master-user=slaver1
#master-password=slaver1pwd
#master-port=3306
replicate-do-db=mydb
replicate-ignore-db=test
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
此时启动完从数据库后,在从库上执行如下命令;
change master to master_host='192.168.1.22',master_port=3306,
master_user='slaver1', master_password='slaver1pwd',
master_log_file='mysql-bin.000305',
master_log_pos=98;
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.100
Master_User: datasync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000010
Read_Master_Log_Pos: 16860
Relay_Log_File: relaylog.000014
Relay_Log_Pos: 17003
Relay_Master_Log_File: binlog.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: db
Replicate_Ignore_DB: mysql,test,information_schema
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: 16860
Relay_Log_Space: 17295
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
安装过程可能会出现的问题及解决办法:
can't connect to local mysql server through socket 'tmp/mysql.sock'
找不到mysql socket的问题,我最近碰到了好多次
重装系统以前,我的mysql,apache,php都是自己编译安装的,当时并没有碰到这些问题,
重装系统以后,我的mysql是通过yum安装的,apache1.37和php5.2是自己编译安装的,出现了这个问题
首先是mysqld启动不了,
我通过vim /etc/my.cnf,修改了[mysqld]选项下面的socket的值
ok,mysqld可以启动了
接下来,是mysql启动不了,同样,vim
/etc/my.cnf,添加了如下脚本:
[mysql]
socket=/var/lib/mysql/mysql.sock
ok,可以启动了
验证配置是否正常,mysql主从能否正常复制。
在主数据库上新建一个库,并且在库中写一个表和一些数据。
[root@localhost ~]# mysql -uroot –pPassword
mysql> create database mydb;
mysql> use mydb;
mysql> create table user(id int(5),name char(10));
mysql> insert into user values (00001,'zhangsan');
在从数据库中验证一下,是否正常复制到数据。
[root@localhost ~]# mysql -uroot –pPassword
mysql> show databases;
mysql> select * from mysqltest.user;
利用上面的命令看看是否有同步过来的数据了。