Maison >base de données >tutoriel mysql >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<br> mysql-5.0.77-4.el5_4.2<br>rpm -e mysql-5.0.77-4.el5_4.2 dovecot-1.0.7-7.el5.i386
1、解压文件
$ groupadd mysql<br>$ useradd -g mysql mysql<br>$ cd /share<br>$ tar zxf mysql-5.1.54-linux-i686-glibc23.tar.gz<br>$ mv mysql-5.1.54-linux-i686-glibc23 /opt<br>$ cd /opt<br>$ ln -s mysql-5.1.54-linux-i686-glibc23 mysql<br>$ chown -R mysql.mysql mysql-5.1.54-linux-i686-glibc23<br>$ cp mysql/support-files/my-large.cnf mysql/my.cnf<br>$ cp mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
2、修改配置
$ vi /opt/mysql/my.cnf (参考下面配置)<br><br>[client]<br>default-character-set=utf8<br>[mysqld]<br>basedir= /opt/mysql<br>#skip-locking<br>skip-external-locking<br>character-set-server=utf8<br>default-storage-engine=INNODB<br>max_connections=500<br><br>$ vi /etc/rc.d/init.d/mysql (参考下面配置)<br><br>basedir=/opt/mysql<br>datadir=$basdir/data
3、创建数据库
$ cd /opt/mysql<br>$ ./scripts/mysql_install_db --user=mysql
4、创建 mysql 服务
$ chkconfig --add mysql<br>$ chkconfig --level 23456 mysql on
5、初始化数据库
$ service mysql start<br>$ cd /opt/mysql<br>$ ./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]<br> server-id = 1<br> log-bin = mysql-bin<br> binlog_format = mixed<br> binlog-do-db = db1<br> binlog-do-db = db2<br> binlog-ignore-db = test<br> binlog-ignore-db = mysql<br> 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';<br> Query OK, 0 rows affected (0.09 sec)<br><br> (完成后可以在从机上做链接测试 [ 从2终端上输入命令: /usr/bin/mysql -h192.168.1.101 -ubackup -p ],如果不能连通请检查主机防火墙或主机 MySQL 端口)
3、(主1终端) 锁定主机数据库表
msyql> FLUSH TABLES WITH READ LOCK;<br> Query OK, 0 rows affected (0.13 sec)
4、(主1终端) 查看主机 Master 状态
mysql> SHOW MASTER STATUS;<br> +------------------+----------+--------------+------------------+<br> | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |<br> +------------------+----------+--------------+------------------+<br> | mysql-bin.000033 | 458 | | |<br> +------------------+----------+--------------+------------------+<br> 1 row in set (0.01 sec)
5、(主2终端) 备份主机数据库
$ cd /var/lib/mysql<br>$ tar zcvf db_backup.tar ibdata* ib_logfile* db1/ db2/<br>$ mv db_backup.tar /tmp
6、(从2终端) 从机导入主机数据库
$ service mysql stop<br>$ cd /var/lib/mysql<br>$ rm -f ibdata* ib_logfile* mysql-bin.* master.info relay-log.info db1/ db2/<br>$ scp 192.168.1.101:/tmp/db_backup.tar .<br>$ tar zxvf db_backup.tar<br>$ chown -R mysql.mysql ibdata* ib_logfile* db1/ db2/<br>$ rm -f db_backup.tar
######################## 步骤 5/6 的另一种方法 (相对较慢) ########################<br># 5、(主2终端) 备份主机数据库<br># ------------------------------<br># $ cd /tmp<br># $ /usr/bin/mysqldump -uroot -pmypass --default-character-set=utf8 --opt --extended-insert=false --triggers --routines --hex-blob -x -q db1 > db1.sql<br># $ /usr/bin/mysqldump -uroot -pmypass --default-character-set=utf8 --opt --extended-insert=false --triggers --routines --hex-blob -x -q db2 > db2.sql<br># $ tar zcvf db_backup.tar db1.sql db2.sql<br><br># 6、(从2终端) 导入主机数据库<br># ------------------------------<br># $ cd /tmp<br># $ scp 192.168.1.101:/tmp/db_backup.tar .<br># $ tar zxvf db.tar<br># $ /usr/bin/mysql -uroot -p<br># Enter password:<br># mysql> create database db1;<br># mysql> use db1;<br># mysql> source /tmp/db1.sql;<br># mysql> create database db2;<br># mysql> use db2;<br># mysql> source /tmp/db2.sql;<br># mysql> exit;<br># $ rm -f db.tar db1.sql db2.sql<br>##################################################################################
7、(从2终端) 编辑从机 MySQL 配置文件
$ vi /etc/my.cnf<br><br> [mysqld]<br> server-id = 2<br> log-bin=mysql-bin<br> binlog_format=mixed<br> replicate-do-db = db1<br> replicate-do-db = db2<br> replicate-ignore-db = test<br> replicate-ignore-db = mysql<br> replicate-ignore-db = information_schema<br> relay-log = db-server-2-relay-bin<br> log-slave-updates<br>$ service mysql start
8、(从1终端) 设置备份点
mysql> slave stop;<br> Query OK, 0 rows affected, 1 warning (0.00 sec)<br>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;<br> Query OK, 0 rows affected (0.02 sec)<br>mysql> slave start;<br> Query OK, 0 rows affected (0.02 sec)
9、解锁主机数据库表
msyql> UNLOCK TABLES;<br> Query OK, 0 rows affected (0.00 sec)
10、其他工作
(删除主从机器的临时文件: db_backup.tar)
==============================================================
=================== 配置 MySQL 互为主从备份 ==================
==============================================================
(假设已根据上面的步骤配置好 MySQL 主从备份)
1、(从2终端) 编辑从机 MSQL 配置文件
$ vi /etc/my.cnf<br> [mysqld]<br> # 加入下面配置<br> binlog-do-db = db1<br> binlog-do-db = db2<br> binlog-ignore-db = test<br> binlog-ignore-db = mysql<br> binlog-ignore-db = information_schema<br>$ 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';<br> Query OK, 0 rows affected (0.09 sec)<br><br> (完成后可以在主机上做链接测试 [ 主2终端上输入命令: /usr/bin/mysql -h192.168.1.110 -ubackup -p ],如果不能连通请检查主机防火墙或主机 MySQL 端口)
3、(主2终端) 编辑主机 MySQL 配置文件
$ vi /etc/my.cnf<br><br> [mysqld]<br> # 加入下面配置<br> replicate-do-db = db1<br> replicate-do-db = db2<br> replicate-ignore-db = test<br> replicate-ignore-db = mysql<br> replicate-ignore-db = information_schema<br> relay-log = db-server-1-relay-bin<br> log-slave-updates<br>$ service mysql start
4、(主1终端) 设置备份点
mysql> slave stop;<br> Query OK, 0 rows affected, 1 warning (0.00 sec)<br>mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.101', MASTER_PORT=3306, MASTER_USER='backup', MASTER_PASSWORD='mypass';<br> Query OK, 0 rows affected (0.02 sec)<br>mysql> slave start;<br> Query OK, 0 rows affected (0.02 sec)
怪兽的博客 怪兽的微博bitsCN.com