Linux(八)数据库备份及nginx配置使用

数据库

## 备份

mysqldump

- 锁表
- 备份特别慢,适用于数据量较小
- 不可以做增量备份
- 单线程

```SHEL
-A, --all-databases 所有的库
-B 指定备份的库
—F 备份前刷新日志
--flush-privileges 刷新授权表
-p 密码
-u 用户
-P 端口
触发器
存储过程和存储函数
## 备份
mysqldump -uroot -S /mydata/mysql/mysql.sock -A -p > mysql.sql
## 恢复一
直接在数据库里面source mysql.sql文件
## 恢复二
mysql -uroot -p < mysql.sql
```

## xtrabackup

- 多进程
- 支持增量备份
- 锁行

```SHELL
安装
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm # 安装yum仓库
yum install -y percona-xtrabackup-24
--target-dir=name # 指定备份生成的目录
--backup 备份
--prepare 准备
--databases=name filtering by list of databases.
--databases-file=name 配置文件
```

### 创建一个用户

```SHELL
mysql> create user 'backup'@'localhost' identified by 'backup';
Query OK, 0 rows affected (0.00 sec)

mysql> grant reload,lock tables,process,replication client on *.* to 'backup'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

```

## 备份

```SHELL
xtrabackup --backup --target-dir=/mydata/backup/base -ubackup -pbackup --socket=/mydata/mysql/mysql.sock
### 看得如下信息,代表成功
xtrabackup: Transaction log of lsn (5480853) to (5480862) was copied.
190902 09:25:09 completed OK!
```

## 恢复

#### 准备文件

```SHELL
xtrabackup --prepare --target-dir=/mydata/backup/base
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 5482536
190902 09:28:12 completed OK!
```

#### 恢复文件

```SHELL
cd /mydata/backup/base
cp -rf hello /mydata/mysql/
chown mysql.mysql /mydata/mysql/hello/ -R
```

#### 恢复全部文件

```SHELL
xtrabackup --copy-back --target-dir=/mydata/backup/base
chown mysql.mysql * -R
```

### 增量备份

```shell
xtrabackup --backup --target-dir=/mydata/backup/t1 --incremental-basedir=/mydata/backup/base -uroot -p --socket=/mydata/mysql/mysql.sock
--incremental-dir 全备的路径
xtrabackup --backup --target-dir=/mydata/backup/t2 --incremental-basedir=/mydata/backup/t1 -uroot -p --socket=/mydata/mysql/mysql.sock
--incremental-basedir应该是上一次的增量备份目录
xtrabackup --prepare --apply-log-only --target-dir=/mydata/backup/base
```

### 恢复(只能全部删除以后再恢复)

```shell
xtrabackup --prepare --apply-log-only --target-dir=/mydata/backup/base
xtrabackup --prepare --apply-log-only --target-dir=/mydata/backup/base --incremental-dir=/mydata/backup/t1
xtrabackup --prepare --apply-log-only --target-dir=/mydata/backup/base --incremental-dir=/mydata/backup/t2
xtrabackup --copy-back --target-dir=/mydata/backup/base
chown mysql.mysql * -R
systemctl restart mysqld
```

# nginx

web服务 apache iis

django web框架

lvs 负载均衡 章文嵩博士

vue 尤雨溪

Tengine

F5 硬件负载

A10

## 安装

```SHELL
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1
yum install gcc zlib2-devel pcre-devel openssl-devel
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module
make && make install
```

## 目录结构

```shell
[root@localhost nginx]#ls
conf html logs sbin
conf 配置文件
html 存放静态文件 index.html 是默认的欢迎页面
logs 日志目录
sbin 二进制文件
启动以后会生成一个主进程,根据配置文件的选项来生成子进程(工作进程),主进程不负责处理用户的请求,用来转发用户的请求,真正负责处理用户请求的是子进程
```

## 命令格式

```shell
./sbin/nginx -h
nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

iptables -F
./sbin/nginx

Options:
-?,-h : this help
-v : show version and exit 显示版本号
-V : show version and configure options then exit 显示版本+编译时选项
-t : test configuration and exit 测试配置文件
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /opt/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
```

## 配置文件

```shell
#user nobody; 使用哪个用户来启动子进程
worker_processes 1; #工作进程的个数,配置成cpu的核心数-1或者-2
# cpu亲缘性绑定,让nginx的子进程工作在哪个核心上

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
#use [epoll|select|poll];
worker_connections 102400; # 每一个子进程可以处理的连接数
}


http {
include mime.types; #导入
default_type application/octet-stream; # 默认的请求方式
# 定义日志格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#定义日志并定义日志格式
#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65; # 保持长连接的超时时间

#gzip on;

server {
listen 80; #监听端口
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html; # 指定静态文件地址
index index.html index.htm; # 指定默认的index页面
}
# 错误页面 找不到页面
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
# 错误页面 服务端错误
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

```

## 404页面

```SHELL
error_page 404 /404.html;
```

## root和alias的区别

```shell
location /img {
root /data/img;
}
root /data/img 里面必须有/img
location /img {
alias /data/img;
}
alias /data/img 里面不需要有 /img
```

## 域名

```shell
server_name ms.s22.com
```

## 多域名访问

```shell
server {
listen 80;
server_name www.taobao.com taobao.com;
location / {
root /data/taobao;
index index.html;
}

}
server {
listen 80;
server_name www.jd.com jd.com;
location / {
root /data/jd;
index index.html;
}
}
```

### 默认server

```shell
listen 80 default_server;
```

posted @ 2021-05-10 11:07  Full-Stack~  阅读(159)  评论(0)    收藏  举报