配置主从准备工作
在主上创建一个测试的数据库
首先登录主的mysql,或者用绝对路径 /usr/local/mysql/bin/mysql
mysql
> create database db1; > quit
复制mysql库
mysqldump -S /tmp/mysql.sock mysql > 123.sql
拷贝mysql库
mysql -S /tmp/mysql.sock db1 < 123.sql
登录主mysql,查看表。 已经拷被过来了。
mysql > use db1; > show tables;> quit
编制配置文件
vim /etc/my.cnf
server-id = 1 # Uncomment the following if you want to log updates log-bin=bin-log 改为 server-id = 1 # Uncomment the following if you want to log updates log-bin=wangshaojunbinlog-ignore-db=mysql
服务器id 和其他不一样就可以
log-bin 可以自定义
binlog-do-db=db1,db2 列表白名单
binlog-ignore-db=mysql 列表黑名单
重启mysql
/etc/init.d/mysqld restart
查看新的binlog
ls /data/mysql
wangshaojun.000001 wangshaojun.index
wangshaojun.err wangshaojun.pid
授权
登录主mysql,创建用户
mysql > grant replication slave on *.* to ‘repl‘@‘127.0.0.1‘ identified by ‘123123‘;
授予 replication 权限 用户名repl 密码 123123
刷新
> flush privileges;
锁死表的读
> flush tables with read lock;
读master
show master status;
File wangshaojun.000001
Position 331
编辑从mysql 的配置文件
vim /usr/local/mysql_slave/my.cnf
server-id = 1 改为 server-id = 111
创建mysql库,并拷贝和主mysql一样的表
mysql -S /tmp/mysql_slave.sock -e "create database db1"
mysql -S /tmp/mysql_slave.sock db1 < 123.sql
登录从mysql
mysql -S /tmp/mysql_slave.sock
先把运行的停掉,
> slave stop; > change master to master_host=‘127.0.0.1‘, master_port=3306, master_user=‘repl‘, master_password=‘123123‘, master_log_file=‘wangshaojun.000001‘, master_log_pos=331;
开启
> slave start;
查看配置是否成功
show slave status\G;
Slave_IO_Running: No
Slave_SQL_Running: Yes
是否是两个yes
先把mysql停掉
> slave stop; > quit
重启
/etc/init.d/mysqldslave restart
登录从mysql
mysql -S /tmp/mysql_slave.sock
> slave start; > show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
因为没有重启从的配置文件,所以失败。
时间: 2024-10-29 00:54:10