最简单的复制环境部署方法
这里环境如下:
Master一直没有写操作
Master和Slave在一台虚拟机中,端口号分别为3306和3307
数据库服务">1.关闭Master数据库服务
mysqladmin -umysql -p -S /data/mysqldata/3306/mysql.sock shutdown
2.复制数据文件
cp -r /data/mysqldata/3306 /data/mysqldata/3307
3.编辑Master的参数文件
在[mysqld]区块中添加
server_id =1 log_bin =../binlog/mysql-bin
binlog必须要打开
同一套复制环境中的每个成员必须要有独立的server_id。server_id的取值范围在1~(2的32次方 -1)
参数配置好后,就可以启动数据库了,但是不要有写操作
4.创建复制专用账户
在Master端
grant replication slave on *.* to 'repl'@'192.168.134.%' identified by 'repl';
这样就创建了账户
(mysql@localhost) [(none)]> select user,host from mysql.user; +--------+---------------+ | user | host | +--------+---------------+ | repl | 192.168.134.% | | backup | localhost | | mysql | localhost | +--------+---------------+
然后,获取Master端信息
(mysql@localhost) [(none)]> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000003 | 331 | | | | +------------------+----------+--------------+------------------+-------------------+
5.配置Slave端选项文件
在[mysqld]区块中添加
server_id =2 log_bin =../binlog/mysql-bin --非必选项
Slave端并不是必须开启bin-log
sed -i 's/3306/3307/g' /data/mysqldata/330/my.cnf
还需要删除Slave端的auto.cnf文件
rm /data/mysqldata/3307/data/auto.cnf
这个文件中给保存了一项名为server-uuid的重要参数,它用来唯一标识Mysql服务,我们的环境是直接复制的主库,所以需要删除它。Slave节点的数据库服务启动时会自动重新生成一个
6.启动Slave端服务
[mysql@master 3307]$ mysql -umysql -p -S /data/mysqldata/3307/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. ... 配置Slave到Master的连接: mysql> change master to -> master_host='192.168.134.128', -> master_port=3306, -> master_user='repl', -> master_password='repl', -> master_log_file='mysql-bin.000003', -> master_log_pos=331; Query OK, 0 rows affected, 2 warnings (0.05 sec) 启动Slave端的应用服务: mysql> start slave;
之后就可以在主库创建个表,插入数据,然后去从库看看,如果数据一致,那么就OK了