mysql> grant replication slave,replication client on *.* to ludy@'192.168.6.5'>
mysql > grant replication slave on *.* to ludy@192.168.6.5> 到这里要注意了,我的两台数据库都是空的.
重启mysql服务
4.修改 mysql2 服务器slave的 my.cnf配置文件
找到 bind-address = 127.0.0.1
替换 bind-address = 0.0.0.0
找到
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
把 注释符号去掉 改为如下
server-id = 2
master-host = 192.168.6.4
master-user = ludy
master-password = ypmwbg
master-port = 3306
log_bin = /var/log/mysql/mysql-bin.log
log-slave-updates
skip-slave-start
配置完后 重新启动mysql
然后进入 mysql1 master 服务器
root@msyql:~# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection> Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 98 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记录下来以后 进入 mysql2 slave mysql
root@msyql2:~# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection> Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> change master to master_log_file='mysql-bin.000001', master_log_pos=98;
//这个地方就是记录下来的 mysql1 master 的数据
mysql > start slave; //启动slave 服务
mysql > show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.6.4
Master_User: ludy
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 181
> 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
1 row in set (0.01 sec)
哈哈 Slave_IO_Running: Yes
Slave_SQL_Running: Yes
说明启动成功
然后在 master 新建 一个数据库看看
root@msyq1l:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection> Server version: 5.0.51a-3ubuntu5.4-log (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database chenggong;
Query OK, 1 row affected (0.00 sec)
在 salve 查看看是否同步
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| chenggong | //同步了哈哈 ~
| mysql |
| test |
+--------------------+
4 rows in set (0.01 sec)
好了就写这么多,如果你作 master-slave 的时候 你的master 数据里有数据那么
你必须 在 我写的 第三步与第四步中加入一下步骤:
接 上文 第三步
进入master数据库的Mysql控制台执行
mysql >FLUSH TABLES WITH READ LOCK; //锁表
然后从新打开一个 终端 拷贝 master 的所有的数据到 slave 服务器覆盖
读取 master 二进制文件与偏移量
mysql > show master status;
同样 要记录下 file 与 position 的值
然后解锁
mysql > unlock tables;