1.配置master的my.cnf文件
# vi /etc/my.cnf
[mysqld]
user=mysql
pid_file = /var/lib/mysql/mysqld.pid
socket = /var/lib/mysql/mysql.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
log-bin = master-bin
log-bin-index = master-bin.index
server_id = 1
innodb_log_file_size = 256M
expire-logs-days = 1
注:log-bin、log-bin-index如不提供值,默认值为hostname-bin。为了避免由于系统名更改可能会日志文件名的混乱,所以最好为服务器创建一个机器人无关的唯一的服务器名。
2.在master创建一个复制用户
mysql> create user repl_user;
Query OK, 0 rows affected (0.04sec)
mysql> grant replication slave on *.*
->to repl_user identified by 'licz';
Query OK, 0 rows affected (0.00sec)
配置slave的my.cnf文件
# vi /etc/my.cnf
[mysqld]
user=mysql
pid_file = /var/lib/mysql/mysqld.pid
socket = /var/lib/mysql/mysql.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
server_id = 2
relay_log_index = slave_relay_bin.index
relay_log = slave_relay_bin
innodb_log_file_size = 256M
expire-logs-days = 1
最后将Slave指向Master,让它知道从哪里进行复制。
需要知道Master四部分信息:
●主机名:可以是IP,也可在/etc/hosts文件配置
●端口号:如果没指定默认是3306
● Master上拥有Replication slave权限的用户帐号
●该用户密码
在slave端执行
--使用change master to命令指向Master
mysql> change master to
master_host = 'host8',
master_port = 3306,
master_user ='repl_user',
master_password ='licz';
--启动复制
mysql> start slave;
--在Master端创建数据库或表
mysql> create database licz;
Query OK, 1 row affected (0.00 sec)
--在Slave端查看是否同步
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)