Linux&&mysql基礎使用


主從複製或主主複製,讀寫分離
mysql完全備份與mysql增量備份相搭配
多點異地備份

關於數據庫備份方案

冷備份

場景:
一般主要用於非核心業務,這類業務一般都是允許業務中斷的,冷備份的特點就是數度快,恢復時也最爲簡單。通常直接復物理文件來實現冷備份
缺點:

  • 需要停止MySQL服務。
  • 僅支持在完全相同版本的mysql服務之間實現數據備份、轉移、恢復
  • 完全備份(全部數據庫的全部數據),數據量大時不方便。

實現方法

備份前的數據

# 以fafamcu數據庫爲例演示
root@ubuntu:/var/lib/mysql# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 50
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Database changed
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fafamcu            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> use fafamcu;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_fafamcu |
+-------------------+
| mcuidx            |
| mculist           |
| mcuroom           |
| meet              |
| meetTimeRecord    |
| netinfo           |
| user              |
+-------------------+
7 rows in set (0.00 sec)

mysql> select * from mcuidx;
+----+---------+
| id | idx     |
+----+---------+
|  1 |  112123 |
|  2 | 1122334 |
|  3 |  112233 |
+----+---------+
3 rows in set (0.00 sec)

mysql> quit
Bye

進行數據備份

# 查看數據存放路徑
root@ubuntu:/var/lib/mysql# vim /etc/mysql/my.cnf 
    [mysqld]
    datadir         = /var/lib/mysql
# 直接對數據路徑進行打包即可
root@ubuntu:/var/lib/mysql# cd ..
root@ubuntu:/var/lib# tar -zcPf mysql.tar.gz mysql/*
root@ubuntu:/var/lib# ls mysql
debian-5.5.flag  fafamcu  ibdata1  ib_logfile0  ib_logfile1  mysql  mysql_upgrade_info  performance_schema

模擬誤刪數據庫

root@ubuntu:/var/lib# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
root@ubuntu:/var/lib# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 52
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fafamcu            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> drop databases fafamcu;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databases fafamcu' at line 1
mysql> drop database fafamcu;
Query OK, 7 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql> quit
Bye

進行數據恢復

# 停止mysql服務
root@ubuntu:/var/lib# service  mysql stop 
mysql stop/waiting
root@ubuntu:/var/lib# netstat -anlp| grep :3306
# 轉移舊數據目錄
root@ubuntu:/var/lib# mv mysql  mysql-err
# 恢復備份的數據目錄
root@ubuntu:/var/lib# tar -xf mysql.tar.gz 
root@ubuntu:/var/lib# ls mysql
debian-5.5.flag  fafamcu  ibdata1  ib_logfile0  ib_logfile1  mysql  mysql_upgrade_info  performance_schema
root@ubuntu:/var/lib# service  mysql start
mysql start/running, process 2564
root@ubuntu:/var/lib# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fafamcu            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> use fafamcu;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_fafamcu |
+-------------------+
| mcuidx            |
| mculist           |
| mcuroom           |
| meet              |
| meetTimeRecord    |
| netinfo           |
| user              |
+-------------------+
7 rows in set (0.00 sec)

mysql> select * from mcuidx;
+----+---------+
| id | idx     |
+----+---------+
|  1 |  112123 |
|  2 | 1122334 |
|  3 |  112233 |
+----+---------+
3 rows in set (0.00 sec)

熱備份

邏輯備份

缺點:

  • 消耗服務器資源,會對服務器造成壓力
  • 當數據量較大時(1G時就很明顯了),備份及恢復速度慢慢慢

實現方法

備份前的數據

root@ubuntu:/var/lib# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fafamcu            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> use fafamcu;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_fafamcu |
+-------------------+
| mcuidx            |
| mculist           |
| mcuroom           |
| meet              |
| meetTimeRecord    |
| netinfo           |
| user              |
+-------------------+
7 rows in set (0.00 sec)

mysql> select * from mcuidx;
+----+---------+
| id | idx     |
+----+---------+
|  1 |  112123 |
|  2 | 1122334 |
|  3 |  112233 |
+----+---------+
3 rows in set (0.00 sec)

mysql> quit
Bye

進行數據備份

root@ubuntu:/var/lib# mysqldump -uroot -p fafamcu --default-character-set=utf8 --opt -Q -R --skip-lock-tables> fafamcu.sql
Enter password: 
root@ubuntu:/var/lib# ls fafamcu.sql 
fafamcu.sql

模擬誤刪數據庫

root@ubuntu:/var/lib# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 43
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fafamcu            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> drop database fafamcu;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql> quit
Bye

進行數據恢復

# 提前創建數據庫
root@ubuntu:/var/lib# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database fafamcu;
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye
# 導入數據庫
root@ubuntu:/var/lib# mysql -uroot -p fafamcu < fafamcu.sql 
Enter password: 
root@ubuntu:/var/lib# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 46
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| fafamcu            |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> use fafamcu
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_fafamcu |
+-------------------+
| mcuidx            |
| mculist           |
| mcuroom           |
| meet              |
| meetTimeRecord    |
| netinfo           |
| user              |
+-------------------+
7 rows in set (0.00 sec)

mysql> select * from mcuidx;
+----+---------+
| id | idx     |
+----+---------+
|  1 |  112123 |
|  2 | 1122334 |
|  3 |  112233 |
+----+---------+
3 rows in set (0.00 sec)

一個參考腳本

#!/bin/bash
#使用:./xx.sh -uroot -p'123456',使用前修改腳本進行變量配置
#過程:備份並刷新binlog,將最新的binlog文件名記錄並整體壓縮打包
#恢復:先進行全量備份,再對根據tim-binlog.txt中的記錄,進行逐個恢復
#提示:最多每分鐘執行一次,否則會覆蓋同分鍾內的文件,可以修改腳本來改善
#      出現問題會退出,可以到指定的日誌目錄查看日誌輸出
#      同年的tar包超過指定天數的會刪除掉

#[變量]
begin_time=`date +%F-%H-%M-%S`
my_sql="/usr/local/mysql/bin/mysql"
bak_sql="/usr/local/mysql/bin/mysqldump"
binlog_dir=/data/mysql/
bak_dir=/ops/bak
log_dir=/ops/log/mybak-all.log
#保存的天數,4周就是28天
save_day=28

#[自動變量]
#當前年月
date_nian=`date +%Y-`
#所有天數的數組
save_day_zu=($(for i in `seq 1 ${save_day}`;do date -d -${i}days "+%F";done))

#開始
/usr/bin/echo >> ${log_dir}
/usr/bin/echo "time:$(date +%F-%H-%M-%S) info:開始全備份" >> ${log_dir}

#檢查
${my_sql} $* -e "show databases;" &> /tmp/info_error.txt
if [[ $? -ne 0 ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) info:登陸命令錯誤" >> ${log_dir}
    /usr/bin/cat /tmp/info_error.txt #如果錯誤則顯示錯誤信息
    exit 1
fi

#移動到目錄
cd ${bak_dir}
bak_time=`date +%F-%H-%M`
bak_timetwo=`date +%F`

#備份
${bak_sql} $* --all-databases --flush-privileges --single-transaction --flush-logs --triggers --routines --events --hex-blob > mybak-all-${bak_time}.sql
if [[ $? -ne 0 ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) error:備份失敗"
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) error:備份失敗" >> ${log_dir}
    /usr/bin/cat /tmp/bak_error.txt #如果錯誤則顯示錯誤信息
    exit 1
else
    bin_dian=`tail -n 1 ${binlog_dir}/mysql-bin.index`
    echo "${bin_dian}" > ${bak_time}-binlog.txt
fi

#壓縮
if [[ -f mybak-all-${bak_time}.tar.gz ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) info:壓縮包mybak-section-${bak_time}.tar.gz 已存在" >> ${log_dir}
    /usr/bin/rm -irf mybak-all-${bak_time}.tar.gz ${bak_sql}-binlog.txt
fi
/usr/bin/tar -cf mybak-all-${bak_time}.tar.gz mybak-all-${bak_time}.sql ${bak_time}-binlog.txt
if [[ $? -ne 0 ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) error:壓縮失敗" >> ${log_dir}
    exit 1
fi

#刪除sql文件
/usr/bin/rm -irf mybak-all-${bak_time}.sql ${bak_time}-binlog.txt
if [[ $? -ne 0 ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) info:刪除sql文件失敗" >> ${log_dir}
    exit 1
fi

#整理壓縮的日誌文件
for i in `ls | grep .tar.gz$`
   do
    echo $i | grep "^mybak-all.*tar.gz$" &> /dev/null
        if [[ $? -eq 0 ]];then
            a=`echo ${i%%.tar.gz}`
            b=`echo ${a:(-16)}`
            c=`echo ${b%-*}`
            d=`echo ${c%-*}`
            #看是否在數組中,不在則刪除
            echo ${save_day_zu[*]} |grep -w $d &> /dev/null
            if [[ $? -ne 0 ]];then
                [[ "$d" != "$bak_timetwo" ]] && rm -rf $i
            fi
        else
            #不是當月的,其他類型壓縮包,跳過
            continue
        fi
done

#結束
last_time=`date +%F-%H-%M-%S`
/usr/bin/echo "begin_time:${begin_time}   last_time:${last_time}" >> ${log_dir}
/usr/bin/echo "time:$(date +%F-%H-%M-%S) info:全備份完成" >> ${log_dir}
/usr/bin/echo >> ${log_dir}

差量備份

增量備份

一個參考腳本

#!/bin/bash
#使用:./xx.sh -uroot -p'123456',將第一次增量備份後的binlog文件名寫到/tmp/binlog-section中,若都沒有,自動填寫mysql-bin.000001
#過程:增量先刷新binlog日誌,再查詢/tmp/binlog-section中記錄的上一次備份中最新的binlog日誌的值
#      cp中間的binlog日誌,並進行壓縮。再將備份中最新的binlog日誌寫入。

#恢復:先進行全量恢復,再根據全量備份附帶的time-binlog.txt中的記錄逐個恢復。當前最新的Binlog日誌要去掉有問題的語句,例如drop等。
#提示:最多每分鐘執行一次,否則會覆蓋同分鍾內的文件,可以修改腳本來改善
#      出現問題會退出,可以到指定的日誌目錄查看日誌輸出
#      同年的tar包超過指定天數的會刪除掉

#[變量]
begin_time=`date +%F-%H-%M-%S`
my_sql="/usr/local/mysql/bin/mysql"
bak_sql="/usr/local/mysql/bin/mysqldump"
binlog_dir=/data/mysql/
binlog_index=${binlog_dir}/mysql-bin.index
bak_dir=/ops/bak
log_dir=/ops/log/mybak-section.log
#保存的天數,4周就是28天
save_day=7

#[自動變量]
#當前年
date_nian=`date +%Y-`
#所有天數的數組
save_day_zu=($(for i in `seq 1 ${save_day}`;do date -d -${i}days "+%F";done))


#開始
/usr/bin/echo >> ${log_dir}
/usr/bin/echo "time:$(date +%F-%H-%M-%S) info:開始增量備份" >> ${log_dir}

#檢查
${my_sql} $* -e "show databases;" &> /tmp/info_error.txt
if [[ $? -ne 0 ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) info:登陸命令錯誤" >> ${log_dir}
    /usr/bin/cat /tmp/info_error.txt #如果錯誤則顯示錯誤信息
    exit 1
fi

#移動到目錄
cd ${bak_dir}
bak_time=`date +%F-%H-%M`
bak_timetwo=`date +%F`

#刷新
${my_sql} $* -e "flush logs"
if [[ $? -ne 0 ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) error:刷新binlog失敗" >> ${log_dir}
    exit 1
fi

#獲取開頭和結尾binlog名字
last_bin=`cat /tmp/binlog-section`
next_bin=`tail -n 1 ${binlog_dir}/mysql-bin.index`
echo ${last_bin} |grep 'mysql-bin' &> /dev/null
if [[ $? -ne 0 ]];then
    echo "mysql-bin.000001" > /tmp/binlog-section #不存在則默認第一個
    last_bin=`cat /tmp/binlog-section`
fi

#截取需要備份的binlog行數
a=`/usr/bin/sort ${binlog_dir}/mysql-bin.index | uniq | grep -n ${last_bin} | awk -F':' '{print $1}'`
b=`/usr/bin/sort ${binlog_dir}/mysql-bin.index | uniq | grep -n ${next_bin} | awk -F':' '{print $1}'`
let b--

#輸出最新節點
/usr/bin/echo "${next_bin}" > /tmp/binlog-section

#創建文件
rm -rf mybak-section-${bak_time}
/usr/bin/mkdir mybak-section-${bak_time}
for i in `sed -n "${a},${b}p" ${binlog_dir}/mysql-bin.index  | awk -F'./' '{print $2}'`
do
    if [[ ! -f ${binlog_dir}/${i} ]];then
        /usr/bin/echo "time:$(date +%F-%H-%M-%S) error:binlog文件${i} 不存在" >> ${log_dir}
        exit 1
    fi
    cp -rf ${binlog_dir}/${i} mybak-section-${bak_time}/
    if [[ ! -f mybak-section-${bak_time}/${i} ]];then
        /usr/bin/echo "time:$(date +%F-%H-%M-%S) error:binlog文件${i} 備份失敗" >> ${log_dir}
        exit 1
    fi
done

#壓縮
if [[ -f mybak-section-${bak_time}.tar.gz ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) info:壓縮包mybak-section-${bak_time}.tar.gz 已存在" >> ${log_dir}
    /usr/bin/rm -irf mybak-section-${bak_time}.tar.gz
fi


/usr/bin/tar -cf mybak-section-${bak_time}.tar.gz mybak-section-${bak_time}
if [[ $? -ne 0 ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) error:壓縮失敗" >> ${log_dir}
    exit 1
fi

#刪除binlog文件夾
/usr/bin/rm -irf mybak-section-${bak_time}
if [[ $? -ne 0 ]];then
    /usr/bin/echo "time:$(date +%F-%H-%M-%S) info:刪除sql文件失敗" >> ${log_dir}
    exit 1
fi

#整理壓縮的日誌文件
for i in `ls | grep "^mybak-section.*tar.gz$"`
   do
    echo $i | grep ${date_nian} &> /dev/null
        if [[ $? -eq 0 ]];then
            a=`echo ${i%%.tar.gz}`
            b=`echo ${a:(-16)}` #當前日誌年月日
            c=`echo ${b%-*}`
            d=`echo ${c%-*}`
 
            #看是否在數組中,不在其中,並且不是當前時間,則刪除。
            echo ${save_day_zu[*]} |grep -w $d &> /dev/null
            if [[ $? -ne 0 ]];then
                [[ "$d" != "$bak_timetwo" ]] && rm -rf $i
            fi
        else
            #不是當月的,其他類型壓縮包,跳過
            continue
        fi
done

#結束
last_time=`date +%F-%H-%M-%S`
/usr/bin/echo "begin_time:${begin_time}   last_time:${last_time}" >> ${log_dir}
/usr/bin/echo "time:$(date +%F-%H-%M-%S) info:增量備份完成" >> ${log_dir}
/usr/bin/echo >> ${log_dir}

# 週六晚3點i分進行全量備份 週一到週六每天進行增量備份, 全量保存4周 增量保存近一週的每天數據
crontab -e 添加計劃任務。
    1 3 * * 6 /bin/bash /shell/mybak-all.sh -uroot -p'123456'
    1 2 * * * /bin/bash /shell/mybak-section.sh -uroot -p'123456'

參考地址:
https://blog.csdn.net/weixin_34032779/article/details/93204960

實現方法

備份前數據

# 進行一次完全備份,基於完全備份按順序執行之後二進制日誌可進行數據的恢復。
root@ubuntu:/var/log/mysql# mysqldump -uroot -p fafamcu --default-character-set=utf8 --opt -Q -R --skip-lock-tables> fafamcu.sql
Enter password: 
root@ubuntu:/var/log/mysql# ls
error.log  fafamcu.sql  mysql-bin.000001  mysql-bin.000002  mysql-bin.000003  mysql-bin.000004  mysql-bin.index
root@ubuntu:/var/log/mysql# mysqladmin -uroot -p flush-logs
Enter password: 
root@ubuntu:/var/log/mysql# ls
error.log  fafamcu.sql  mysql-bin.000001  mysql-bin.000002  mysql-bin.000003  mysql-bin.000004  mysql-bin.000005  mysql-bin.index
root@ubuntu:/var/log/mysql# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 51
Server version: 5.5.55-0ubuntu0.14.04.1-log (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use fafamcu
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from mcuidx;
+----+---------+
| id | idx     |
+----+---------+
|  1 |  112123 |
|  2 | 1122334 |
|  3 |  112233 |
|  4 |  200619 |
+----+---------+
4 rows in set (0.00 sec)

模擬第一次增量備份

# 增加數據
mysql> insert into mcuidx values (5.200620);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into mcuidx values (5,200620);
Query OK, 1 row affected (1.60 sec)

mysql> select * from mcuidx;
+----+---------+
| id | idx     |
+----+---------+
|  1 |  112123 |
|  2 | 1122334 |
|  3 |  112233 |
|  4 |  200619 |
|  5 |  200620 |
+----+---------+
5 rows in set (0.00 sec)

mysql> quit
Bye

模擬第二次增量備份

# 生成新的二進制日誌文件
root@ubuntu:/var/log/mysql# mysqladmin -uroot -p flush-logs
Enter password: 
root@ubuntu:/var/log/mysql# ls
error.log  fafamcu.sql  mysql-bin.000001  mysql-bin.000002  mysql-bin.000003  mysql-bin.000004  mysql-bin.000005  mysql-bin.000006  mysql-bin.index
# 模式失誤刪除數據
root@ubuntu:/var/log/mysql# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.5.55-0ubuntu0.14.04.1-log (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use fafamcu
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from mcuidx;
+----+---------+
| id | idx     |
+----+---------+
|  1 |  112123 |
|  2 | 1122334 |
|  3 |  112233 |
|  4 |  200619 |
|  5 |  200620 |
+----+---------+
5 rows in set (0.00 sec)

mysql> delete from mcuidx where id=1;
Query OK, 1 row affected (0.00 sec)

mysql> delete from mcuidx where id=2;
Query OK, 1 row affected (0.01 sec)

mysql> select * from mcuidx;
+----+--------+
| id | idx    |
+----+--------+
|  3 | 112233 |
|  4 | 200619 |
|  5 | 200620 |
+----+--------+
3 rows in set (0.00 sec)

mysql> quit
Bye

進行數據恢復

# 恢復完全備份
root@ubuntu:/var/log/mysql# mysql -uroot -p fafamcu < fafamcu.sql 
Enter password: 
root@ubuntu:/var/log/mysql# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 55
Server version: 5.5.55-0ubuntu0.14.04.1-log (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use fafamcu;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from mcudix;
ERROR 1146 (42S02): Table 'fafamcu.mcudix' doesn't exist
mysql> show tables;
+-------------------+
| Tables_in_fafamcu |
+-------------------+
| mcuidx            |
| mculist           |
| mcuroom           |
| meet              |
| meetTimeRecord    |
| netinfo           |
| user              |
+-------------------+
7 rows in set (0.00 sec)

mysql> select * from mcuidx;
+----+---------+
| id | idx     |
+----+---------+
|  1 |  112123 |
|  2 | 1122334 |
|  3 |  112233 |
|  4 |  200619 |
+----+---------+
4 rows in set (0.00 sec)

mysql> quit
Bye

使用二進制日誌恢復數據

# 使用第一次增量備份產生的二進制日誌恢復第一部分數據
root@ubuntu:/var/log/mysql# ls
error.log  fafamcu.sql  mysql-bin.000001  mysql-bin.000002  mysql-bin.000003  mysql-bin.000004  mysql-bin.000005  mysql-bin.000006  mysql-bin.index
root@ubuntu:/var/log/mysql# mysqlbinlog  --no-defaults mysql-bin.000005 | mysql -uroot -p
Enter password: 
root@ubuntu:/var/log/mysql# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 58
Server version: 5.5.55-0ubuntu0.14.04.1-log (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use fafamcu;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from mcuidx;
+----+---------+
| id | idx     |
+----+---------+
|  1 |  112123 |
|  2 | 1122334 |
|  3 |  112233 |
|  4 |  200619 |
|  5 |  200620 |
+----+---------+
5 rows in set (0.00 sec)

關於數據庫容災方案

關於數據庫守護方案

關於數據庫(1T)備份、還原方案

https://blog.csdn.net/yincg/article/details/71409502

關於數據庫備份

參考地址1(推薦,如何實現mysql的1T數據庫備份):
https://blog.csdn.net/enweitech/article/details/51612858?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
參考地址2:
https://blog.csdn.net/longyuhome/article/details/35786487/

關於性能優化

架構優化

1、選擇爭取的存儲引擎
InnoDB支持事務,大量的insert update用它,count需要掃描整個表,提供行鎖,支持 外鍵,主鍵
範圍大
MyISAM執行速度看性能高,大量的select用它,表鎖,支持全文索引支持GIS數據
2、視圖
3、字段類型
4、不要是使用null
5、有固定的值用emum
6、爲每張表設置一個ID作爲主鍵,並且自增
7、使用索引
8、-static 連接的話,MySQL執行速度的速度是最快的

查找技巧優化

1、拆分大的delete insert語句
2、不要用select *
3、不要order by rand

配置文件優化

1、慢查詢 slow_qurey_log
2、物理內存越大,設置就越大.默認爲2402,調到512-1024最佳
innodb_additional_mem_pool_size=4M
3、指定MySQL允許的最大連接進程數。如果在訪問論壇時經常出現Too Many Connection(www.111cn.net)s的錯
誤提 示,則需要增大該參數值。
max_connect_errors = 1000000
4、指定MySQL查詢緩衝區的大小
tmp_table_size = 256M

發現問題優化

PHP查詢數據庫慢?

問題現象:
通過測試得出:服務器執行php文件速度正常,從MySQL數據庫讀取數據速度異常。進一步測試發現,MySQL -uroot -p -h 12.0.2.49過程緩慢。
解決方法:
更改MySQL配置文件my.cnf,添加以下代碼:
[mysqld]
skip-name-resolve #Don’t resolve hostnames
重啓MySQL服務即可。
問題總結:
服務器做了一個本地dns服務,每次執行訪問DB數據庫操作時,MySQL就會試圖去解析來訪問的機器的domain name,如果這時解析不成功,等一段時間會失敗,數據才能被取過來。

mysql服務器頻繁掛掉(down)&&CPU高

問題現象:
公司網站剛改完版,優化了數據庫結構,使用的memcache分佈式緩存加mysql,剛開始幾天還好,數據庫服務幾天沒down過了.但這幾天頻繁down機.是個大問題,需要解決.
問題分析:

  1. 確保服務器硬件配置是合理的
  2. 查看服務器負載(top命令),見mysql竟然經常百分之九十幾的cpu,恐怖.早之前就知道,數據表有些字段沒有做索引的,引起MYSQL負載這麼高的原因主要應該是索引問題和某些變態SQL語句.

如何知道是MYSQL哪些索引和SQL引起的問題呢?老大教有絕招
解決方法:
編輯MYSQL配置文件my.cnf,加上以下幾行:
log_slow_queries=/usr/local/mysql/data/log_slow_queries.log //慢語句日誌保存目錄
long_query_time=10 //記錄SQL查詢超過10S的語句
log-queries-not-using-indexes=1 //記錄沒有使用索引的sql

這樣,慢語句就被記錄在日誌文件中了.
tail -f /usr/local/mysql/data/log_slow_queries.log 查看日誌.
四個參數
Query_time: 0 Lock_time: 0 Rows_sent: 1 Rows_examined: 54
分別意思爲:查詢時間 鎖定時間 查詢結果行數 掃描行數
主要看那些掃描行數超多的語句.然後去數據庫加上對應的索引
再優化下變態的sql
完成後mysql負載降到了二十幾.還行.應該不會down機了.

Mysql 高負載排查思路

問題描述:
top命令 查看服務器負載,發現 mysql竟然百分之兩百的cpu,引起Mysql 負載這麼高的原因,估計是索引問題和某些變態SQL語句.

排查思路 :

  1. 確定高負載的類型,top命令看負載高是CPU還是IO。
  2. mysql 下執行查看當前的連接數與執行的sql 語句。
  3. 檢查慢查詢日誌,可能是慢查詢引起負載高。
  4. 檢查硬件問題,是否磁盤故障問題造成的。
  5. 檢查監控平臺,對比此機器不同時間的負載。

確定負載類型(top)

top - 10:14:18 up 23 days, 11:01,  1 user, load average: 124.17, 55.88, 24.70 Tasks: 138 total,   1 running, 137 sleeping,   0 stopped,   0 zombie Cpu(s):  2.4%us,  1.0%sy,  0.0%ni, 95.2%id,  2.0%wa,  0.1%hi,  0.2%si,  0.0%st Mem:   3090528k total,  2965772k used,   124756k free,    93332k buffers Swap:  4192956k total,  2425132k used,  1767824k free,   756524k cached            PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 30833 mysql     15   0 6250m 2.5g 4076 S 257.1 49.9 529:34.45 mysqld  

查看當前的連接數與執行的sql 語句

>show processlist; 
Id  User    Host    db  Command Time    State   Info 192 slave   8.8.8.142:39820 NULL    Binlog Dump 58982   Has sent all binlog to slave;
waiting for binlog to be updated  NULL 194 slave   8.8.8.120:41075 NULL    Binlog Dump 58982   Has sent all binlog to slave; 
waiting for binlog to be updated  NULL 424891 biotherm    8.8.8.46:57861  biotherm    Query   493 Sending data    
>SELECT * FROM xxx_list WHERE tid = '1112'  AND del = 0  ORDER BY  id DESC  LIMIT 0, 4 
424917 biotherm    8.8.8.49:50984  biotherm    Query   488 Sending data   
>SELECT * FROM xxx_list WHERE tid = '1112'  AND del = 0  ORDER BY  id DESC  LIMIT 0, 4 
430330 biotherm    8.8.8.42:35982  biotherm    Query   487 Sending data    
>SELECT * FROM xxx_list WHERE tid = '1112'  AND del = 0  

記錄慢查詢
編輯Mysql 配置文件(my.cnf),在[mysqld]字段添加以下幾行:
log_slow_queries = /usr/local/mysql/var/slow_queries.log #慢查詢日誌路徑
long_query_time = 10 #記錄SQL查詢超過10s的語句
log-queries-not-using-indexes = 1 #記錄沒有使用索引的sql

查看慢查詢日誌

tail /usr/local/mysql/var/slow_queries.log 
# Time: 130305  9:48:13 # User@Host: biotherm[biotherm] @  [8.8.8.45] 
# Query_time: 1294.881407  Lock_time: 0.000179 Rows_sent: 4  Rows_examined: 1318033 SET timestamp=1363916893; SELECT * FROM xxx_list WHERE tid = '11xx'  AND del = 0  ORDER BY  id DESC  LIMIT 0, 4;  

4個參數
Query_time: 0 Lock_time: 0 Rows_sent: 1 Rows_examined: 54
分別意思爲:查詢時間 鎖定時間 查詢結果行數 掃描行數
主要看掃描行數多的語句,然後去數據庫加上對應的索引,再優化下變態的sql 語句。

極端情況kill sql進程
找出佔用cpu時間過長的sql,在mysql 下執行如下命令: show processlist; 確定後一條sql處於Query狀態,且Time時間過長,鎖定它的ID,執行如下命令: kill QUERY 269815764;
注意:殺死 sql進程,可能導致數據丟失,所以執行前要衡量數據的重要性。

修改mysql密碼

1.首先確認服務器出於安全的狀態,也就是沒有人能夠任意地連接MySQL數據庫。
因爲在重新設置MySQL的root密碼的期間,MySQL數據庫完全出於沒有密碼保護的狀態下,其他的用戶也可以任意地登錄和修改MySQL的信息。可以採用將MySQL對外的端口封閉,並且停止Apache以及所有的用戶進程的方法實現服務器的準安全狀態。最安全的狀態是到服務器的Console上面操作,並且拔掉網線。
2.修改MySQL的登錄設置:

vi /etc/my.cnf 
#在[mysqld]的段中加上一句:skip-grant-tables 
#例如: 
    [mysqld] 
    datadir=/var/lib/mysql 
    socket=/var/lib/mysql/mysql.sock 
    skip-grant-tables 
#保存並且退出vi。 

3.重新啓動mysqld

/etc/init.d/mysqld restart 
    Stopping MySQL: [ OK ] 
    Starting MySQL: [ OK ] 

4.登錄並修改MySQL的root密碼

# /usr/bin/mysql 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 3 to server version: 3.23.56 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
mysql> USE mysql ; 
Reading table information for completion of table and column names 
You can turn off this feature to get a quicker startup with -A 
Database changed 
mysql> UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ; 
Query OK, 0 rows affected (0.00 sec) 
Rows matched: 2 Changed: 0 Warnings: 0 
mysql> flush privileges ; 
Query OK, 0 rows affected (0.01 sec) 
mysql> quit 
Bye 

5.將MySQL的登錄設置修改回來

vi /etc/my.cnf 
#將剛纔在[mysqld]的段中加上的skip-grant-tables刪除 
#保存並且退出vi。 

6.重新啓動mysqld

/etc/init.d/mysqld restart 
    Stopping MySQL: [ OK ] 
    Starting MySQL: [ OK ]

mysql主從同步故障排錯

問題描述:
基本上用mysq都會配置mysql的主從,一方面用mysql的主從做數據庫的讀寫分離,另一方面mysql本身的單機備份不是很強,一般採用主從架構,在從上進行數據備份。在這過程中或多或少出現一些主從不同步的情況,本文將對數據主從不同步的情況進行簡單的總結,主要從數據庫層面上探討數據庫的主從不一致的情況,並不對主從的本身數據不一致引起的主從不同步進行說明:
問題分析:

  • 確認mysql主從服務器的時間是否同步
  • 網絡的延遲

由於mysql主從複製是基於binlog的一種異步複製,通過網絡傳送binlog文件,理所當然網絡延遲是主從不同步的絕大多數的原因,特別是跨機房的數據同步出現這種機率非常的大,所以做讀寫分離,注意從業務層進行前期設計。

  • 主從兩臺機器的負載不一致

由於mysql主從複製是主上面啓動1個io線程,而從上面啓動1個sql線程和1個io線程,當中任何一臺機器的負載很高,忙不過來,導致其中的任何一個線程出現資源不足,都將出現主從不一致的情況。

  • max_allowed_packet設置不一致

主上面設置的max_allowed_packet比從大,當一個大的sql語句,能在主上面執行完畢,從上面設置過小,無法執行,導致的主從不一致。

  • key自增鍵開始的鍵值跟自增步長設置不一致引起的主從不一致。
  • mysql異常宕機情況下,如果未設置sync_binlog=1或innodb_flush_log_at_trx_commit=1很有可能出現binlog或者relaylog文件出現損壞,導致主從不一致。
  • mysql本身的bug引起的主從不同步。
  • 版本不一致,特別是高版本是主,低版本爲從的情況下,主上面支持的功能,從上面不支持該功能。

mysql的innodb_buffer_pool_size

參考資料:
https://www.cnblogs.com/wanbin/p/9530833.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章