1. 程式人生 > >mysql進階簡單解析

mysql進階簡單解析

軟件 pro kernel username mysql數據恢復 apach reserve 完全備份 sta

mysql進階

1.mysql配置文件

mysql的配置文件為/etc/my.cnf
配置文件查找次序:若在多個配置文件中均有設定,則最後找到的最終生效

/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FI
  LE --> ~/.my.cnf

mysql常用配置文件參數:

參數 說明
port 設置監聽端口
socket=/tmp/mysql.sock 指定套接字文件位置
basedir=/usr/local/mysql 指定mysql數據存放的路徑
pid-file=/data/mysql/mysql.pid 指定進程ID文件存放路徑
user=mysql 指定MySQL以什麽身份來提供服務
skip-name-resolve 禁止MySQL對外部連接進行DNS解析的時間。若開啟該選項,則所有遠程主機連接授權都要使用IP地址方式否則MySQL將無法正常處理連接請求

2.mysql數據庫備份

2.1 數據庫常用備份方案

數據庫備份方案:

  • 全量備份
  • 增量備份
  • 差異備份
備份方案 特點
差異備份 備份上一次的完全備份後發生變化的所有文件。 差異備份是指在一次備份後到進行差異備份的這段時間內,對那些增加或修改文件的備份。進行恢復時,我們只需要對第一次全量備份和最後一次差異備份進行恢復。
全量備份 全量備份就是指對某一個時間點上吃所有數據或應用進行的一個完全拷貝。 數據恢復快備份時間長
增量備份 增量備份是指在一次全備份或上一次增量備份後,以後每次備份只需要備份與前一次相比增加或者被修改的文件。這就意味著,第一次增量備份的對象是進行一次備份後產生的增加和修改的文件,如此類推。 沒有重復的備份數據備份時短恢復數據時必須按一定的順序進行

2.2mysql備份工具mysqldump

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

常用的OPTIONS:

-uUSERNAME  //指定數據庫用戶名
-hHOST     //指定服務器主機名,請使用ip地址
-pPASSWORD  //指定數據庫用戶的密碼
-P#         //指定數據庫監聽的端口,這裏的#需要用實際的端口號代替

備份整個數據庫(全備)

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

mysql> show tables;
+------------------+
| Tables_in_linfan |
+------------------+
| doudou           |
| linfan           |
+------------------+
2 rows in set (0.00 sec)

[root@linfan ~]# ls
anaconda-ks.cfg  doudou.repo  lin  nfs.sh

[root@linfan ~]# mysqldump -uroot -p -h127.0.0.1 --all-databases > all-20180818.sql
Enter password:
[root@linfan ~]# ls
all-20180818.sql  anaconda-ks.cfg  doudou.repo  lin  nfs.sh

備份linfan庫的doudou表


[root@linfan ~]# mysqldump -uroot -p -h127.0.0.1 linfan doudou > tabledoudou-20180818.sql
Enter password:
[root@linfan ~]# ls
all-20180818.sql  anaconda-ks.cfg  doudou.repo  lin  nfs.sh  tabledoudou-20180818.sql 

備份linfan數據庫

[root@linfan ~]# mysqldump -uroot -p -h127.0.0.1 --databases linfan > data-linfan-20180818.sql
Enter password:
[root@linfan ~]# ls
all-20180818.sql  anaconda-ks.cfg  data-linfan-20180818.sql  doudou.repo  lin  nfs.sh  tabledoudou-20180818.sql

模擬誤刪linfan數據庫

mysql> drop  database linfan ;
Query OK, 2 rows affected (0.02 sec)

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

2.3 mysql數據恢復

[root@linfan ~]# ls
all-20180818.sql  anaconda-ks.cfg  data-linfan-20180818.sql  doudou.repo  lin  nfs.sh  tabledoudou-20180818.sql
[root@linfan ~]# mysql -uroot -p -h127.0.0.1 < all-20180818.sql
Enter password:
[root@linfan ~]# mysql -uroot -p -h127.0.0.1 -e ‘show databases;‘
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linfan             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

恢復linfan數據庫的doudou表

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> source tabledoudou-20180818.sql; 
mysql> show tables;
+------------------+
| Tables_in_linfan |
+------------------+
| doudou           |
| linfan           |
+------------------+
2 rows in set (0.00 sec)

模擬刪除整個數據庫

mysql> show tables;
+------------------+
| Tables_in_linfan |
+------------------+
| doudou           |
| linfan           |
+------------------+
2 rows in set (0.00 sec)

mysql> show database;
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 ‘database‘ at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linfan             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

恢復整個數據庫

[root@linfan ~]# ls
all-20180818.sql  anaconda-ks.cfg  data-linfan-20180818.sql  doudou.repo  lin  nfs.sh  tabledoudou-20180818.sql
[root@linfan ~]# mysql -uroot -p -h127.0.0.1 < all-20180818.sql
Enter password:
[root@linfan ~]# mysql -uroot -p -h127.0.0.1  -e ‘show databases;‘
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linfan             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

3.二進制格式mysql安裝

下載二進制格式的mysql軟件包

[root@linfan ~]# cd /usr/src/
[root@linfan src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  

創建用戶和組

[root@linfan ~]# groupadd -r mysql
[root@linfan ~]# useradd -M -s /sbin/nologin -g mysql mysql

解壓軟件至/usr/local/

[root@linfan src]# ls
apr-1.6.3          apr-util-1.6.1          debug    mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
apr-1.6.3.tar.bz2  apr-util-1.6.1.tar.bz2  kernels
[root@linfan src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@linfan src]# ls  /usr/local/
apache  apr-util  etc    include  lib64    mysql-5.7.22-linux-glibc2.12-x86_64  share
apr     bin       games  lib      libexec  sbin                                 src
[root@linfan src]# cd  /usr/local/ 
[root@linfan local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@linfan local]# ll
total 0
drwxr-xr-x. 13 root root 152 Aug 17 12:46 apache
drwxr-xr-x.  6 root root  58 Aug 17 12:35 apr
drwxr-xr-x.  5 root root  43 Aug 17 12:40 apr-util
drwxr-xr-x.  2 root root   6 Nov  5  2016 bin
drwxr-xr-x.  2 root root   6 Nov  5  2016 etc
drwxr-xr-x.  2 root root   6 Nov  5  2016 games
drwxr-xr-x.  2 root root   6 Nov  5  2016 include
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib64
drwxr-xr-x.  2 root root   6 Nov  5  2016 libexec
lrwxrwxrwx.  1 root root  36 Aug 17 13:54 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Aug 17 13:30 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root root   6 Nov  5  2016 sbin
drwxr-xr-x.  5 root root  49 Jul 11 15:44 share
drwxr-xr-x.  2 root root   6 Nov  5  2016 src

修改目錄/usr/locaal/mysql的屬主屬組

[root@linfan ~]# chown -R mysql.mysql /usr/local/mysql
[root@linfan ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Aug 17 13:54 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

添加環境變量

[root@linfan ~]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@linfan ~]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[root@linfan ~]# . /etc/profile.d/mysql.sh
[root@linfan ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

建立數據存放目錄

[root@linfan ~]# cd /usr/local/mysql
[root@linfan mysql]# mkdir /opt/data
[root@linfan mysql]# chown -R mysql.mysql /opt/data/
[root@linfan mysql]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql   6 Aug 17 14:05 data
drwxr-xr-x. 8 root  root  220 Jul 18 17:09 lin.d

初始化數據庫

[root@linfan mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-17T06:08:33.347313Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-17T06:08:33.873415Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-17T06:08:33.953310Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-17T06:08:34.016549Z 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: f8e46285-a1e3-11e8-b6bf-000c29c9d4ed.
2018-08-17T06:08:34.019542Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2018-08-17T06:08:34.023380Z 1 [Note] A temporary password is generated for root@localhost: B<HiGFoc.8yZ
//這個命令的最後會生成一個臨時密碼,此處密碼是B<HiGFoc.8yZ

生成配置文件

[root@linfan ~]# cat > /etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF

配置服務啟動腳本

[root@linfan ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@linfan ~]# sed -ri ‘s#^(basedir=).*#\1/usr/local/mysql#g‘ /etc/init.d/mysqld
[root@linfan ~]# sed -ri ‘s#^(datadir=).*#\1/opt/data#g‘ /etc/init.d/mysqld     

啟動mysql

[root@linfan ~]# service mysqld start
Starting MySQL.Logging to ‘/opt/data/linfan.err‘.
 SUCCESS!
[root@linfan ~]# ps -ef|grep mysql
root      52200      1  0 14:25 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     52378  52200  4 14:25 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=linfan.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      52408   2998  0 14:25 pts/1    00:00:00 grep --color=auto mysql
[root@linfan ~]# ss -antl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port
LISTEN     0      128                               *:22                                            *:*
LISTEN     0      100                       127.0.0.1:25                                            *:*
LISTEN     0      128                              :::80                                           :::*
LISTEN     0      128                              :::22                                           :::*
LISTEN     0      100                             ::1:25                                           :::*
LISTEN     0      80                               :::3306                                         :::*   

修改密碼
使用臨時密碼修改

[root@linfan ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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> set password = password(‘linfan123‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

mysql進階簡單解析