mysql主从(replication)

1、原理

master上数据更新时会记录到一个bin-log,slave会实时同步该bin-log,然后slave根据bin-log的sql语句进行相应的操作。

实时备份,也可以实现读写分离,减少I/O。

可以1主多从,或者相互主从。

2、安装两个mysql

[root@localhost local]# cp -r mysql/ mysql3/   //复制
[root@localhost local]# mkdir /data/mysql3
[root@localhost local]# chown -R mysql:mysql /data/mysql3
[root@localhost local]# cd mysql3/
[root@localhost mysql3]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql3   //初始化
[root@localhost mysql3]# cp /etc/my.cnf ./
[root@localhost mysql3]# vim my.cnf   //修改配置文件
[mysqld]
port            = 3307
socket          = /tmp/mysql3.sock
datadir         = /data/mysql3
user            = mysql
pid-file        = /data/mysql1/localhost.pid

3307端口作为主。

[root@localhost ~]# mysql -uroot -p123456 -P3307
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.40 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database db1;   //创建一个db1库
Query OK, 1 row affected (0.06 sec)
mysql> Bye
[root@localhost ~]# mysqldump -uroot -p123456 -P3307 db1 > /tmp/db1.sql    //导出
[root@localhost ~]# mysql -uroot -p123456 -P3307 db1 < /tmp/db1.sql   //导入

修改mysql3配置文件

[root@localhost ~]# vim /usr/local/mysql3/my.cnf
//添加下面两句
log-bin=zhouyan
server-id       = 1

重启mysql3

[root@localhost ~]# ps aux |grep mysql3
mysql     2013  0.0  6.5 211560 21380 ?        Sl   17:21   0:00 /usr/local/mysql3/bin/mysqld --basedir=/usr/local/mysql3 --datadir=/data/mysql3 --user=mysql --log-error=/data/mysql3/localhost.localdomain.err --pid-file=/data/mysql1/localhost.pid --socket=/tmp/mysql3.sock --port=3307
root      2162  0.0  0.2   4356   736 pts/1    S+   17:36   0:00 grep mysql3
[root@localhost ~]# kill 2013
[root@localhost ~]# /usr/local/mysql3/bin/mysqld_safe --defaults-file=/usr/local/mysql3/my.cnf --user=mysql &
[root@localhost mysql3]# ls /data/mysql3
ibdata1      ib_logfile1                localhost.localdomain.err-old  test            zhouyan.index
ib_logfile0  localhost.localdomain.err  mysql                          zhouyan.000001  //以这种格式生成
[root@localhost mysql3]# vim /usr/local/mysql3/my.cnf
binlog-do-db=db1  //指定需要同步的库
#binlog-ignore-db=mysql   //忽略不同步的库
[root@localhost mysql3]# kill 2277
[root@localhost mysql3]# ps aux |grep mysql3
root      2167  0.0  0.4   5064  1332 pts/1    S    17:37   0:00 /bin/sh /usr/local/mysql3/bin/mysqld_safe --defaults-file=/usr/local/mysql3/my.cnf --user=mysql
mysql     2277  0.1  6.5 211572 21196 pts/1    Sl   17:37   0:00 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql3/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql3 --user=mysql --log-error=/data/mysql3/localhost.localdomain.err --pid-file=/data/mysql1/localhost.pid --socket=/tmp/mysql3.sock --port=3307
root      2298  0.0  0.2   4356   740 pts/1    S+   17:42   0:00 grep mysql3
//这种kill不掉时,不要使用kill -9,因为还有数据没写完,耐心等待。
[root@localhost mysql3]# kill -HUP 2277   //重新加载

设置从的登录mysql账号和密码

[root@localhost mysql3]# mysql -uroot -P3307 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.40 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave on *.* to 'repl'@'127.0.0.1' identified by '123456';
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

修改从的配置文件

                               《眼睛累,明天继续》









你可能感兴趣的:(mysql,master,sql语句,记录)