1. 程式人生 > 實用技巧 >MYSQL安裝及備份

MYSQL安裝及備份

目錄

二進位制包安裝mysql

  • 將下載的二進位制包放置 /usr/src目錄下
[root@localhost ~]# cd /usr/src
[root@localhost src]# ls
debug  kernels  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
  • 解壓檔案到/usr/local/
[root@localhost src]# tar -xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.22-linux-glibc2.12-x86_64  share
  • 新建mysql系統使用者,並將mysql目錄檔案更改使用者及組
[root@localhost local]# useradd -r -M -s /sbin/nologin mysql
[root@localhost local]# chown -R mysql.mysql mysql*
[root@localhost local]# ll
lrwxrwxrwx. 1 mysql mysql  36 Oct 22 17:01 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Oct 22 17:01 
......
  • 新建mysql資料庫資料存放目錄,更改此檔案所屬使用者及組
[root@localhost mysql]# mkdir /var/mysql_data;chown -R mysql.mysql /var/mysql_data
[root@localhost mysql]# ll -d /var/mysql_data
drwxr-xr-x. 2 mysql mysql 6 Oct 22 17:06 /var/mysql_data
  • 將mysql命令列入PATH環境變數中
[root@localhost mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@localhost mysql]# source /etc/profile.d/mysql.sh 
[root@localhost mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
  • 初始化資料庫
[root@localhost mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/var/mysql_data/
2020-10-22T09:10:19.831335Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-22T09:10:21.859987Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-10-22T09:10:22.075321Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-10-22T09:10:22.162614Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 69e222bd-1446-11eb-832e-000c291ef71e.
2020-10-22T09:10:22.196056Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-10-22T09:10:22.197776Z 1 [Note] A temporary password is generated for root@localhost: -=/V:0#Bs9we   ## 系統將提供臨時密碼,用於第一次登入,登入後修改密碼即可
  • 將初始密碼存入檔案中
[root@localhost mysql]# echo '-=/V:0#Bs9we' > ~/.passwd
[root@localhost mysql]# cat ~/.passwd 
-=/V:0#Bs9we
  • 新建及配置mysql主配置檔案
[root@localhost mysql]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql   ## 主程式存放位置
datadir = /var/mysql_data    ## 資料庫檔案存放位置
socket = /tmp/mysql.sock     ## 所用套接字檔案
port = 3306                  ## 預設埠號
pid-file = /var/mysql_data/mysql.pid     ## pid檔案
user = mysql                             ## 服務所屬使用者名稱
skip-name-resolve                        ## 跳過域名解析。只允許ip方式登入
  • 編輯服務啟動檔案,將程式位置及檔案存放位置的目錄補全,且將檔案複製到init.d檔案下,用於service管理
[root@localhost mysql]# vim support-files/mysql.server
......
basedir=/usr/local/mysql
datadir=/var/mysql_data
......
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  • 開啟mysql服務
[root@localhost mysql]# service mysqld start
Starting MySQL.Logging to '/var/mysql_data/localhost.localdomain.err'.
 SUCCESS! 
  • 檢視程序
 [root@localhost mysql]# ps -ef |grep mysqld
root      11175      1  0 17:15 pts/3    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/var/mysql_data --pid-file=/var/mysql_data/mysql.pid
mysql     11363  11175  0 17:15 pts/3    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/mysql_data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/var/mysql_data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      11400   2621  0 17:17 pts/3    00:00:00 grep --color=auto mysqld
  • 運用臨時密碼登入,並修改密碼
[root@localhost mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22

Copyright (c) 2000, 2018, 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> 


mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

[root@localhost mysql]# mysql -uroot -p'123456'
......

mysql> 
  • 設定開機自己,預設級別2345
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

mysql資料備份及恢復

備份方案

全量備份

全量備份就是指對某一個時間點上的所有資料或應用進行的一個完全拷貝。
資料恢復快。備份時間長

完全備份的好處是資料恢復方便,因為所有的資料都在同一個備份中,所以只要恢復完全備份,所有的資料都會被恢復。如果完全備份備份的是整塊硬碟,那麼甚至不需要資料恢復,只要把備份硬碟安裝上,伺服器就會恢復正常.

增量備份

完全備份隨著資料量的加大,備份耗費的時間和佔用的空間會越來越多,所以完全備份不會也不能每天進行,這時增量備份的作用就體現了出來。
增量備份是指先進行一次完全備份,伺服器執行一段時間之後,比較當前系統和完全備份的備份資料之間的差異,只備份有差異的資料.這就是增量備份

差異備份

差異備份也要先進行一次完全備份,但是和增量備份不同的是,每次差異備份都備份和原始的完全備份不同的資料

相比較而言,差異備份既不像完全備份一樣把所有資料都進行備份,也不像增量備份在進行資料恢復時那麼麻煩,只要先恢復完全備份的資料,再恢復差異備份的資料即可

全量備份與恢復

  • 語法:
mysqldump [OPTIONS] database [tables ...]
mysqldump [OPTIONS] --all-databases [OPTIONS]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]

mysqldump對整個資料庫進行備份

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sawyer             |
| sy                 |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

[root@localhost mysql]# mysqldump --all-databases > ~/all_backup

[root@localhost ~]# ll all_backup 
-rw-r--r--. 1 root root 803238 Oct 23 15:08 all_backup

對單個數據庫的指定表進行備份

mysql> show tables;
+------------------+
| Tables_in_sawyer |
+------------------+
| nation_list      |
| student1         |
+------------------+
2 rows in set (0.00 sec)


# 將sawyer資料庫中的兩張表進行備份到一個檔案中


[root@localhost ~]# mysqldump sawyer student1 nation_list > 231518_backup
[root@localhost ~]# ll 231518_backup 
-rw-r--r--. 1 root root 2637 Oct 23 15:18 231518_backup

全量備份的資料恢復

  • 方式一:

1.將sy庫刪除掉

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sawyer             |
| sy                 |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database sy;
Query OK, 1 row affected (0.00 sec)

2.在mysql互動中進行恢復

mysql> source all_backup;

......

## sy庫已經恢復
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sawyer             |
| sy                 |
| sys                |
+--------------------+
6 rows in set (0.00 sec)
  • 方式二
  1. 將sawyer庫中的nation_list刪除掉
mysql> show tables;
+------------------+
| Tables_in_sawyer |
+------------------+
| nation_list      |
| student1         |
+------------------+
2 rows in set (0.00 sec)

mysql> drop table nation_list;
Query OK, 0 rows affected (0.00 sec)

2.在shell互動中進行恢復nation_list

[root@localhost ~]# mysql sawyer < 231518_backup    # 指定庫為sawyer


## nation_list已經恢復

[root@localhost ~]# mysql -e'show tables from sawyer'
+------------------+
| Tables_in_sawyer |
+------------------+
| nation_list      |
| student1         |
+------------------+

注意:在shell中恢復指定表時,需要指定庫名。如果是恢復資料庫則不需要指定

差異備份與恢復

mysqldump差異備份

現有sawyer庫中的兩張表

  1. nation_list

  1. student1

  • 開啟二進位制日誌檔案
[root@localhost ~]# vim /etc/my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/var/mysql_data
socket=/tmp/mysql.sock
port=3306
pid-file=/var/mysql_data/mysql.pid
user=mysql
skip-name-resolve

server-id=1       ## 指定日誌檔案中的server-id
log-bin=mysql_bin  ## 開啟二進位制日誌功能

[client]
port=3306
user=root
password=123456
  • 重啟服務
[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
  • 將DB.sawyer進行全量備份
[root@localhost mysql_data]# mysqldump --single-transaction --flush-logs --master-data=2 sawyer student1 nation_list --delete-master-logs > ~/231554.all_backup

[root@localhost mysql_data]# ll ~/231554.all_backup 
-rw-r--r--. 1 root root 2789 Oct 23 15:56 /root/231554.all_backup

  • 模擬對sawyer庫中表進行刪除操作
## 修改ID為1的age欄位的內容
mysql> select * from student1;
+----+-----+
| id | age |
+----+-----+
|  1 |  20 |
|  2 |  34 |
|  3 |  19 |
|  4 |  50 |
+----+-----+
4 rows in set (0.00 sec)

mysql> update student1 set age = 30 where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

## 刪除掉nation_list這張表

mysql> drop table nation_list;
Query OK, 0 rows affected (0.00 sec)
  • 重新整理二進位制檔案
[root@localhost mysql_data]# mysqladmin flush-logs  新建新編號的日誌檔案


[root@localhost mysql_data]# cat mysql_bin.index 
./mysql_bin.000002
./mysql_bin.000003   #目前日誌將存放於00003日誌檔案中

利用差異備份恢復資料

  • 全量備份恢復
[root@localhost ~]# ls
231518_backup  231554.all_backup  all_backup  anaconda-ks.cfg
[root@localhost ~]# mysql sawyer < 231554.all_backup

  • 檢視全量備份之後操作的日誌檔案,檢視想要恢復的pos
mysql> show binlog events in 'mysql_bin.000002'\G

......
*************************** 9. row ***************************
   Log_name: mysql_bin.000002
        Pos: 491                  ##491為刪除表之前的pos值
 Event_type: Query
  Server_id: 1
End_log_pos: 619
       Info: use `sawyer`; DROP TABLE `nation_list` /* generated by server */
*************************** 10. row ***************************
   Log_name: mysql_bin.000002
        Pos: 619
 Event_type: Rotate
  Server_id: 1
End_log_pos: 666
       Info: mysql_bin.000003;pos=4
10 rows in set (0.00 sec)

  • 指定pos值來進行差異備份的恢復
[root@localhost mysql_data]# mysqlbinlog --stop-position=491 /var/mysql_data/mysql_bin.000002 | mysql

  • 檢視恢復情況
mysql> use sawyer
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_sawyer |
+------------------+
| nation_list      |
| student1         |
+------------------+
2 rows in set (0.01 sec)



## 檢視修改的年齡內容

mysql> select * from student1;
+----+-----+
| id | age |
+----+-----+
|  1 |  30 |
|  2 |  34 |
|  3 |  19 |
|  4 |  50 |
+----+-----+
4 rows in set (0.00 sec)