Note that the master server above the uniform yum install MySQL
Master-Slave installation MySQL
shell> yum-y install MySQL Mysql-server
/etc/init.d/mysqld start #初始化数据库 and start the database
主服务器上面添加以下配置server-id=1log-bin=mysql-bin #这个一定得设置,否则没有日志的话,从数据库上会报错
[[Email protected] etc]# service mysqld stop
Stopping mysqld: [OK]
[[Email protected] etc]# service mysqld start
Starting mysqld: [OK]
[[Email protected] etc]# service mysqld status
MYSQLD (PID 3129) is running ...
创建主从复制的帐号:mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO [email protected]‘192.168.137.197‘ IDENTIFIED BY ‘123456‘;Query OK, 0 rows affected (0.00 sec)如果该命令无法执行,请检查是否用了圆角的’,需要用半角的‘,再不行,那就是mysql数据库安装不成功。192.168.137.197是从库的地址backup是需要同步的用户名(有些博客说是数据库名称,坑人不倦)123456为远程同步密码解锁表
Mysql> UNLOCK TABLES;
Query OK, 0 rows Affected (0.00 sec)
正确的状态如下:mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | binlog_do_db | binlog_ignore_db |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 106 | | |
+------------------+----------+--------------+------------------+
1 row in Set (0.00 sec)
配置错误的情况,解决办法见文章最后的疑难解答。mysql> SHOW MASTER STATUS;Empty set (0.00 sec)修改从库配置:
[Email protected] ~]# VI/ETC/MY.CNF
Add statement
Log-bin = Mysql-bin
server_id = 2
Master-host = 192.168.137.33
Master-user = Backup
Master-pass = 123456
Master-port = 3306
Master-connect-retry = 60
Mysql> Change MASTER to
Master_host= ' 192.168.137.33 ',
Master_user= ' backup ',
Master_password= ' 123456 ',
master_port=3306,
Master_log_file= ' mysql-bin.000001 ',
master_log_pos=106,
master_connect_retry=10;
Query OK, 0 rows affected (0.02 sec)
Test whether the master is configured successfully.
Mysql> show Slave status\g;
#注意在slave上面检测 show slave status时要保证 1、 Slave_IO_State 和Slave_IO_Running 都是有状态的才算salve进程启动,后续才能进行主从操作。切记!测试:新建表:CREATE TABLE Persons-> (-> Id_P int,-> LastName varchar(255),-> FirstName varchar(255),-> Address varchar(255),-> City varchar(255)-> ); mysql> desc Persons;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id_p | Int (11) | YES | | NULL | |
| LastName | varchar (255) | YES | | NULL | |
| FirstName | varchar (255) | YES | | NULL | |
| Address | varchar (255) | YES | | NULL | |
| City | varchar (255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in Set (0.01 sec)
The same result appears after Mysql>desc persons from the server above, which indicates the configuration, master-slave synchronization is successful. But this is only the main server file changes after synchronization to the server from the top, from the server's SQL statement execution, does not affect the master server changes, and does not realize the real sense of master-slave synchronization. Please be aware!
mysql5.1 master-Slave synchronization Basic Experiment on CentOS 6.6