!! 假定所有安裝包均在 /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)
CodeProject