mysql 主从部署
主从配置mysql
grant all privileges on *.* to 'root'@'%' identified by 'root';
flush privileges;
》》》》》》主服务器配置 10.10.6.169
grant replication slave on *.* to 'testuser'@'10.10.6.141' identified by '12345678';
flush privileges;
vi /etc/my.cnf
[mysqld]下添加以下参数,若文件中已经存在,则不用添加
server-id=1
log-bin=mysql-bin #启动MySQL二进制日志系统,
binlog-do-db=ourneeddb #需要同步的数据库
binlog-ignore-db=mysql #不同步mysql系统数据库,若还有其它不想同步的,继续添加
重启数据库
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000008 | 107 | bak | mysql |
+------------------+----------+--------------+------------------+
》》》》》导出数据库
flush tables with read lock; #数据库只读锁定命令,防止导出数据库的时候有数据写入
unlock tables; #解除锁定
导出数据库结构及数据:mysqldump -uroot -p ourneeddb > /home/ourneeddb.sql
导出存储过程及函数 :mysqldump -uroot -p -ntd -R ourneeddb > ourneeddb_func.sql
tips:-ntd导出存储过程、-R导出函数
》》》》》从服务器配置 10.10.6.141
[mysqld]下添加以下参数,若文件中已经存在,则不用添加
server-id=2 #设置从服务器id,必须于主服务器不同
log-bin=mysql-bin #启动MySQ二进制日志系统
replicate-do-db=ourneeddb #需要同步的数据库名
replicate-ignore-db=mysql #不同步mysql系统数据库
[root@localhost~ ]/etc/init.d/mysqld restart #重启服务
use mysql
stop slave;
change master to
master_host='10.10.6.169',
master_user='testuser',
master_password='12345678',
master_log_file='mysql-bin.000008',
master_log_pos=107; #log_file与log_pos是主服务器master状态下的File与Position
start slave;
show slave status\G;
主主复制
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; rpm安装方式 复制配置文件 cp /usr/share/mysql/my-default.cnf /etc/my.cnf 105mysql>>>my.cnf #开启二进制日志 log_bin=mysql-bin binlog_format=mixed #数据存放文件夹 datadir =/var/lib/mysql port =3306 #与另一台机器不同的ID server_id = 1 socket =/var/lib/mysql/mysql.sock # 步进值,根据mysql机器数量决定 auto_increment_increment=2 #起始值。一般填第n台主MySQL。此时为第一台主MySQL auto_increment_offset=1 # 忽略mysql库,一般不需要写 binlog-ignore-db=mysql binlog-ignore-db=information_schema #要同步的数据库,默认所有库 replicate-do-db=test sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES <<< 创建同步用户 CREATE USER 'mysqluser'@'10.10.6.106' IDENTIFIED BY 'root'; GRANT REPLICATION SLAVE ON *.* TO 'mysqluser'@'10.10.6.106' IDENTIFIED BY 'root'; flush privileges; 主从复制配置 主机105运行 show master status; 从机106运行 change master to master_host='10.10.6.105', master_user='mysqluser', master_password='root', master_log_file='mysql-bin.000003', master_log_pos=1412; start slave; show slave status\G Last_SQL_Errno: 0 Last_SQL_Error: Master_Host: 主库地址 Master_User: 连接主库使用的 用户 Master_Log_File: 主库二进制文件 Read_Master_Log_Pos: 主库二进制文件坐标 Slave_IO_Running: 与主库 IO 通信状态,需要为 Yes 方可 Slave_SQL_Running: 负责自己的 Slave MySQL 进行状态,需要为 Yes 方可 Replicate_Do_DB: 同步的 DB 单台105主106从复制配置完毕 106>>>my.cnf log_bin=mysql-bin binlog_format=mixed # These are commonly set, remove the # and set as required. # basedir = ..... datadir =/var/lib/mysql port =3306 server_id = 2 socket =/var/lib/mysql/mysql.sock auto_increment_increment=2 auto_increment_offset=2 创建同步用户 CREATE USER 'mysqluser'@'10.10.6.105' IDENTIFIED BY 'root'; GRANT REPLICATION SLAVE ON *.* TO 'mysqluser'@'10.10.6.105' IDENTIFIED BY 'root'; flush privileges; 主从复制配置 主机106运行 show master status; 从机105运行 change master to master_host='10.10.6.106', master_user='mysqluser', master_password='root', master_log_file='mysql-bin.000003', master_log_pos=1412; start slave; show slave status\G