cacti mysql數(shù)據(jù)庫的備份
cacti mysql數(shù)據(jù)庫的備份步驟如下:
為了防止cacti mysql數(shù)據(jù)庫損壞導(dǎo)致的問題,在另一服務(wù)器上建立從服務(wù)器,現(xiàn)記錄步驟
1 建立從數(shù)據(jù)庫
groupadd mysql
useradd -g mysql mysql
./configure --prefix=/cache/mysql --enable-assembler --enable-largefile --with-extra-charsets=all
make
make install
cp support-files/my-medium.cnf /etc/my.cnf
bin/mysql_install_db --user=mysql --datadir=/cache/mysql/var/
#在/cache/mysql/var/下 建立mysql數(shù)據(jù)庫,屬主為mysql
chgrp -R mysql .
將mysql文件夾下的屬組變?yōu)閙ysql
啟動mysql
. mysql/bin/mysqld_safe
將mysql設(shè)置為自動啟動
cp support/mysql.server /etc/rc.d/init.d/mysqld
chown root.root /etc/rc.d/init.d/mysqld
chmod 755 /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld restart
設(shè)置mysql root用戶的密碼
./bin/mysqladmin -u root password 123456
從服務(wù)器配置完畢。
#p#
2 同步的建立
2.1 主服務(wù)器上的更改
vi /etc/my.cnf
log-bin #啟動支持replication的二進制日志
binlog-do-db=cacti #指出要備份的數(shù)據(jù)庫名稱
server-id=1 #指出此數(shù)據(jù)庫為主數(shù)據(jù)庫
binlog-ignore-db=mysql #不備份mysql數(shù)據(jù)庫
注意:如果想做復(fù)雜的結(jié)構(gòu):比如說A備份到B,B再備份到C,那么B除了打開log-bin,還要打開log-slave-updates
2.2 從服務(wù)器上的配置更改
vi /etc/my.cnf
server-id=2
master-host=主機
master-user=用戶名
master-password=密碼
master-port=端口
replicate-do-db=需要復(fù)制的數(shù)據(jù)名
replicate-ignore-db=需要復(fù)制的數(shù)據(jù)庫名
2.3 在主數(shù)據(jù)庫上建立同步用戶。
grant replication slave on *.* to qxt@"%" identified by "qxt";
grant replication client,reload,super on *.* to qxt@"%"
flush privileges;
#修改權(quán)限后需要刷新才能生效
2.4 利用熱備份將本地數(shù)據(jù)庫拷貝到從數(shù)據(jù)庫上
2.4.1 數(shù)據(jù)庫的導(dǎo)出
mysqldump -u root -p cacti > /root/cacti.sql
#mysqldump將cacti數(shù)據(jù)庫的所有數(shù)據(jù)導(dǎo)出
scp /root/cacti.sql IP:/root
#將主數(shù)據(jù)庫文件拷貝到從數(shù)據(jù)庫所在的機器上
并在上面執(zhí)行
mysql -u root -p cacti < /root/cacti.tzt
#裝載數(shù)據(jù)庫
2.5 鎖住主數(shù)據(jù)庫保證備份時沒有數(shù)據(jù)進入主庫干擾備份
flush tables with read lock;
停止主服務(wù)器上的mysql服務(wù)
mysqladmin -u root shutdown
注意:如果表的類型只是mylsam,可以不停止mysql數(shù)據(jù)庫,但復(fù)制過程中要鎖住,如果表類型是InnoDB則必須停止主數(shù)據(jù)庫。查看)(show table status from cacti;)
2.6 查看主庫狀態(tài)
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000008 | 59170349 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
2.7 啟動復(fù)制過程
start slave;
2.8 解鎖
unlock tables;
#p#
2.9 測試
1 在從數(shù)據(jù)庫上
mysql > show slave status\G;
Slave_IO_Running: Yes #IO同步成功
Slave_SQL_Running: Yes #數(shù)據(jù)庫同步成功
2 因為是主從數(shù)據(jù)庫,所以在主數(shù)據(jù)庫中插入數(shù)值,從數(shù)據(jù)庫就會同步過去。
例子: 在主數(shù)據(jù)庫中插入test表
mysql> use cacti;
mysql> create table test(i int)
#在test中插入一個值為1
mysql> insert into test values(1);
mysql> commit;
#確認
查詢一下是否插入
select * from test;
+------+
| i |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
進入從數(shù)據(jù)庫檢查:
mysql> use cacti;
Database changed
mysql> select * from test;
+------+
| i |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
實驗證明從數(shù)據(jù)庫已經(jīng)復(fù)制過來了。
附: mysql> show processlist\G 查看從主數(shù)據(jù)庫和從數(shù)據(jù)庫復(fù)制的相關(guān)進程
mysql> show processlist\G;
*************************** 1. row ***************************
Id: 1
User: system user
Host:
db: NULL
Command: Connect
Time: 5159
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 81
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 3. row ***************************
Id: 4
User: root
Host: localhost
db: NULL
Command: Sleep
Time: 1166
State:
Info: NULL
問題解決
Slave_SQL_Running: No
1.程序可能在slave上進行了寫操作
2.也可能是slave機器重起后,事務(wù)回滾造成的.
一般是事務(wù)回滾造成的:
解決辦法:
mysql> slave stop;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> slave start;
解決辦法二、
首先停掉Slave服務(wù):slave stop
到主服務(wù)器上查看主機狀態(tài):
記錄File和Position對應(yīng)的值
進入master
mysql> show master status;
+----------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| localhost-bin.000094 | 33622483 | | |
+----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
然后到slave服務(wù)器上執(zhí)行手動同步:
mysql> change master to
> master_host='master_ip',
> master_user='user',
> master_password='pwd',
> master_port=3306,
> master_log_file='localhost-bin.000094',
> master_log_pos=33622483 ;
1 row in set (0.00 sec)
mysql> slave start;
解決3 有些是日志錯誤造成的。
方法刪掉數(shù)據(jù)庫日志。
指定正確的日志位置和名稱,再做同步。
通過上面的數(shù)據(jù),我們知道如何進行cacti mysql數(shù)據(jù)庫的備份!希望你們能學(xué)會,以便預(yù)防問題的出現(xiàn)!
【編輯推薦】