Install, configure MySQL
Refer to previous steps to build MySQL service
To make the experiment convenient, we configured two MySQL services on the same machine (running two ports)
cd/usr/local/; Cp-r MySQL mysql_2; CD mysql_2;
Initialize MYSQL2:./scripts/mysql_install_db--user=mysql--DATADIR=/DATA/MYSQL2
Copy configuration file: Cp/etc/my.cnf./my.cnf
Modify profile related parameters: Vim my.cnf #更改port and socket
Start:/usr/local/mysql_2/bin/mysqld_safe--defaults-file=/usr/local/mysql_2/my.cnf--user=mysql &
If you boot it, you need to add it to the/etc/rc.local.
Configure Master-Slave preparation
Set Mysql_2 to primary port 3307,mysql to 3306 from port
Create a test library in our Lord: Creation database db1;
Then export the master MySQL library data and import it to DB1
Mysqldump-uroot-s/tmp/mysql2.sock mysql > 123.sql;
Mysql-uroot-s/tmp/mysql2.sock DB1 < 123.sql
Configure Primary (Master)
VIM/USR/LOCAL/MYSQL_2/MY.CNF #修改或添加:
Server-id=1
Log-bin=mysql-bin
Two optional parameters (2 Select 1):
BINLOG-DO-DB=DB1,DB2 #需要同步的库
BINLOG-IGNORE-DB=DB1,DB2 #忽略不同步的库
After modifying the configuration file, restart Mysql_2
pid=; ps uax |grep mysql2.sock |grep -v grep |awk ‘{print $2}‘
kill $pid; cd/usr/local/mysql_2/bin/;/mysqld_safe--defaults-file=. /MY.CNF--user=mysql &
Set root password: mysqladmin-u root-s/tmp/mysql2.sock password ' 123456 '
Mysql-u root-s/tmp/mysql2.sock-p ' 123456 '
Grant replication Slave on . to ' repl ' @ ' 127.0.0.1 ' identified by ' 123123 ';
Flush tables with read lock;
Show master status; #一定要记住前两列的内容, I'll use it in a minute.
-
Set from (slave)
vim/etc/my.cnf #修改或增加
Server-id = 2 #这个数值不能和主一样
Optional parameters: REPLICATE-DO-DB=DB1,DB2 and REPLICATE-IGNORE-DB=DB1,DB2 #意义同主的那两个可选参数
Service mysqld Restart
Copy the master's DB1 library data from: Mysqldump-uroot-s/tmp/ mysql2.sock-p123456 db1 > Db1.sql; Mysql-uroot-p-E "CREATE database DB1"; Mysql-uroot-p DB1 < Db1.sql
Mysql-uroot-p #登陆从的mysql
slave stop;
Change Master to master_host= ' 127.0.0.1 ', master_port=3307, master_user= ' repl ', master_password= ' 123123 ', Master_log_ File= ' mysql-bin.000006 ', master_log_pos=474952;
Slave start;
Lord: Mysql-uroot-s/tmp/mysql2.sock-p123456-e "Unlock tables"
View from the status from: Show slave status\g;
- Test Master
The Lord emptied the DB table of the DB1 library: use DB1; Select COUNT () from DB "; TRUNCATE table db;
Enter slave to view the DB1 Library DB table: use DB1; Select COUNT () from DB ";
Lord Delete table Db:drop tables db;
From the top, the DB table doesn't exist.
Recommendation: MySQL master-slave mechanism is relatively fragile, cautious operation. If you restart Master, be sure to stop the slave first, that is, you need to slave up the slave Stop command, and then go to restart the master MySQL service, otherwise it is likely to be interrupted. Of course, after the restart, you also need to slave to open slave start.
Install, configure MySQL