站内搜索

Linux 系统安装各种服务(三) MySQL / MySQL 主从备份_MySQL

bitsCN.com

!! 假定所有安装包均在 /share目录,安装目录为 /opt !!

$ ll /share
-rw-r--r-- 1 root root 129041873 Nov 29 21:06 mysql-5.1.54-linux-i686-glibc23.tar.gz

==============================================================
========================= 安装 MySQL =========================
==============================================================

0、卸载自带 MSQL

rpm -qa | grep mysql
mysql-5.0.77-4.el5_4.2
rpm -e mysql-5.0.77-4.el5_4.2 dovecot-1.0.7-7.el5.i386

1、解压文件

$ groupadd mysql
$ useradd -g mysql mysql
$ cd /share
$ tar zxf mysql-5.1.54-linux-i686-glibc23.tar.gz
$ mv mysql-5.1.54-linux-i686-glibc23 /opt
$ cd /opt
$ ln -s mysql-5.1.54-linux-i686-glibc23 mysql
$ chown -R mysql.mysql mysql-5.1.54-linux-i686-glibc23
$ cp mysql/support-files/my-large.cnf mysql/my.cnf
$ cp mysql/support-files/mysql.server /etc/rc.d/init.d/mysql

2、修改配置

$ vi /opt/mysql/my.cnf (参考下面配置)

[client]
default-character-set=utf8
[mysqld]
basedir= /opt/mysql
#skip-locking
skip-external-locking
character-set-server=utf8
default-storage-engine=INNODB
max_connections=500

$ vi /etc/rc.d/init.d/mysql (参考下面配置)

basedir=/opt/mysql
datadir=$basdir/data

3、创建数据库

$ cd /opt/mysql
$ ./scripts/mysql_install_db --user=mysql

4、创建 mysql 服务

$ chkconfig --add mysql
$ chkconfig --level 23456 mysql on

5、初始化数据库

$ service mysql start
$ cd /opt/mysql
$ ./bin/mysql_secure_installation

------------------------------------------

下载 service 脚本:services/mysql

------------------------------------------

==============================================================
========================= MySQL 主从备份 =========================
==============================================================

主机 IP: 192.168.1.101 (db-server-1)
从机 IP: 192.168.1.110 (db-server-2)
----------------------------------------------------------
需要备份的数据库: db1、db2
----------------------------------------------------------
!!! 假设 MySQL 默认 RPM 安装 !!!
配置文件: /etc/my.cnf
安装目录: /usr/
数据库目录: /var/lib/mysql/
启动开关: service mysql {start|stop|status|restart|condrestart|try-restart|reload|force-reload}

----------------------------------------------------------
准备工作: 主机和从机个开启两个终端:
A、主1终端、从1终端运行 mysql
$ /usr/bin/mysql -uroot -p
Enter password:
B、主2终端、从2终端执行 shell 命令
(首先打开 mysql 日志)
(主2终端) $ tail -f /var/lib/mysql/db-server-1.err &
(从2终端) $ tail -f /var/lib/mysql/db-server-2.err &
----------------------------------------------------------

1、(主2终端) 编辑主机 MSQL 配置文件

$ vi /etc/my.cnf
  [mysqld]
server-id = 1
log-bin = mysql-bin
binlog_format = mixed
binlog-do-db = db1
binlog-do-db = db2
binlog-ignore-db = test
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
$ service mysql restart

2、(主1终端) 主机 MySQL 建立备份用户 (backup / mypass)

msyql> GRANT FILE, REPLICATION SLAVE, REPLICATION CLIENT, SUPER, RELOAD ON *.* TO backup@'192.168.1.110'IDENTIFIED by 'mypass';
Query OK, 0 rows affected (0.09 sec)

(完成后可以在从机上做链接测试 [ 从2终端上输入命令: /usr/bin/mysql -h192.168.1.101 -ubackup -p ],如果不能连通请检查主机防火墙或主机 MySQL 端口)

3、(主1终端) 锁定主机数据库表

msyql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.13 sec)

4、(主1终端) 查看主机 Master 状态

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000033 | 458 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)

5、(主2终端) 备份主机数据库

$ cd /var/lib/mysql
$ tar zcvf db_backup.tar ibdata* ib_logfile* db1/ db2/
$ mv db_backup.tar /tmp

6、(从2终端) 从机导入主机数据库

$ service mysql stop
$ cd /var/lib/mysql
$ rm -f ibdata* ib_logfile* mysql-bin.* master.info relay-log.info db1/ db2/
$ scp 192.168.1.101:/tmp/db_backup.tar .
$ tar zxvf db_backup.tar
$ chown -R mysql.mysql ibdata* ib_logfile* db1/ db2/
$ rm -f db_backup.tar

######################## 步骤 5/6 的另一种方法 (相对较慢) ########################
# 5、(主2终端) 备份主机数据库
# ------------------------------
# $ cd /tmp
# $ /usr/bin/mysqldump -uroot -pmypass --default-character-set=utf8 --opt --extended-insert=false --triggers --routines --hex-blob -x -q db1 > db1.sql
# $ /usr/bin/mysqldump -uroot -pmypass --default-character-set=utf8 --opt --extended-insert=false --triggers --routines --hex-blob -x -q db2 > db2.sql
# $ tar zcvf db_backup.tar db1.sql db2.sql

# 6、(从2终端) 导入主机数据库
# ------------------------------
# $ cd /tmp
# $ scp 192.168.1.101:/tmp/db_backup.tar .
# $ tar zxvf db.tar
# $ /usr/bin/mysql -uroot -p
# Enter password:
# mysql> create database db1;
# mysql> use db1;
# mysql> source /tmp/db1.sql;
# mysql> create database db2;
# mysql> use db2;
# mysql> source /tmp/db2.sql;
# mysql> exit;
# $ rm -f db.tar db1.sql db2.sql
##################################################################################


7、(从2终端) 编辑从机 MySQL 配置文件

$ vi /etc/my.cnf

[mysqld]
server-id = 2
log-bin=mysql-bin
binlog_format=mixed
replicate-do-db = db1
replicate-do-db = db2
replicate-ignore-db = test
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
relay-log = db-server-2-relay-bin
log-slave-updates
$ service mysql start

8、(从1终端) 设置备份点

mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.101', MASTER_PORT='3306', MASTER_USER='backup', MASTER_PASSWORD='mypass', MASTER_LOG_FILE='mysql-bin.000033', MASTER_LOG_POS=458;
Query OK, 0 rows affected (0.02 sec)
mysql> slave start;
Query OK, 0 rows affected (0.02 sec)

9、解锁主机数据库表

msyql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

10、其他工作

(删除主从机器的临时文件: db_backup.tar)

==============================================================
=================== 配置 MySQL 互为主从备份 ==================
==============================================================

(假设已根据上面的步骤配置好 MySQL 主从备份)


1、(从2终端) 编辑从机 MSQL 配置文件

$ vi /etc/my.cnf
[mysqld]
# 加入下面配置
binlog-do-db = db1
binlog-do-db = db2
binlog-ignore-db = test
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
$ service mysql restart

2、(从1终端) 从机 MySQL 建立备份用户 (backup / mypass)

msyql> GRANT FILE, REPLICATION SLAVE, REPLICATION CLIENT, SUPER, RELOAD ON *.* TO backup@'192.168.1.101'IDENTIFIED by 'mypass';
Query OK, 0 rows affected (0.09 sec)

(完成后可以在主机上做链接测试 [ 主2终端上输入命令: /usr/bin/mysql -h192.168.1.110 -ubackup -p ],如果不能连通请检查主机防火墙或主机 MySQL 端口)

3、(主2终端) 编辑主机 MySQL 配置文件

$ vi /etc/my.cnf

[mysqld]
# 加入下面配置
replicate-do-db = db1
replicate-do-db = db2
replicate-ignore-db = test
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
relay-log = db-server-1-relay-bin
log-slave-updates
$ service mysql start

4、(主1终端) 设置备份点

mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.101', MASTER_PORT=3306, MASTER_USER='backup', MASTER_PASSWORD='mypass';
Query OK, 0 rows affected (0.02 sec)
mysql> slave start;
Query OK, 0 rows affected (0.02 sec)






怪兽的博客 怪兽的微博bitsCN.com
  • 上一篇:MySQL数据库中的外键约束详解_MySQL
  • 下一篇:工作和学习中经常用到的SQL语句_MySQL