备份Mysql

创建备份文件夹

1
mkdir -p /data/mysqlbackup/

创建备份的脚本

1
2
3
4
5
6
7
8
[root@prod-ghospital mysqlbackup]# cat backup.sh 
#!/bin/bash
mysqldump -upraise -pAf9Ed1Bb827b.4@9eB={9c4f --all-databases >/data/mysqlbackup/praise-`date "+%y-%m-%d"`.sql
mysqldump -umessage -p4666dd3a@79.d24=9cD{b382 --all-databases >/data/mysqlbackup/message-`date "+%y-%m-%d"`.sql
mysqldump -uvideo -pDd0{A9e2026c=94d13b@.62f --all-databases >/data/mysqlbackup/video-`date "+%y-%m-%d"`.sql
mysqldump --single-transaction -unethospital -p97@9EbeA7.=1Fb04{e87998d --all-databases >/data/mysqlbackup/nethospital-`date "+%y-%m-%d"`.sql
mysqldump -uhospunion -p76984a}74aE964e69b4Ce31,.ff{2f02 --all-databases >/data/mysqlbackup/hospunion-`date "+%y-%m-%d"`.sql
find /data/mysqlbackup -type f -name "*.sql" -mtime +7 -exec rm -rf {} \;

加入定时任务

1
2
[root@prod-ghospital mysqlbackup]# crontab -l
*/10 1 * * * /bin/sh /data/mysqlbackup/backup.sh

备份公众号数据库

1
mysqldump -ucdh -pCDH360lj#com2019 -h139.159.192.185 --all-databases >/home/mysql.sql

备份Postgresql

更改为postgres用户

1
su postgres

创建备份文件夹

1
2
bash-4.2$ cd /var/lib/pgsql/
bash-4.2$ mkdir -p postgres/backups

创建备份脚本

1
2
3
bash-4.2$ cat backup.sh 
pg_dumpall > ~/postgres/backups/pgbackup-`date "+%Y-%m-%d"`.bak
find ~/postgres/backups/ -type f -name "*.bak" -mtime +7 -exec rm -rf {} \;

加入定时任务

1
2
bash-4.2$ crontab -l
*/10 1 * * * /bin/sh ~/postgres/backups/backup.sh

备份sqlserver

linux备份

安装sqlcmd

1
2
3
4
5
curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/msprod.repo
sudo yum install mssql-tools unixODBC-devel
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

备份

1
2
3
4
5
6
7
8
9
#创建数据库备份文件夹 
mkdir -p /data/sqlserverbackup
#给mussql授予/data/sqlserverbackup文件夹的权限
chown -R mssql:mssql /data/sqlserverbackup
#登录数据库
sqlcmd -S 127.0.0.1 -U sa
#备份数据库到指定路径
1> backup database appauth to disk='/data/sqlserverbackup/appauth.bak'
2> go

windows备份

windows任务计划定时备份sqlserver数据库

在E盘下新建文件夹DBbackup,在DBbackup下创建一个sql的备份脚本,文件命名为dbback.sql

sql脚本如下:(DBname就是你所需要备份的数据库名)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GO

DECLARE

@backuptime varchar(20)

DECLARE

@filename varchar(100)

select @backuptime=(convert(varchar(8),getdate(),112)+replace(convert(varchar(5),getdate(),114),':',' '))

select @filename='E:\DBbackup\db_'+@backuptime+'.bak'

backup database DBname to disk=@filename

写一个批处理文件执行sql语句:

例如:backup_database.bat

1
.bat文件内容为:sqlcmd -S . -i E:\DBbackup\dbback.sql

.bat文件内的语句可以在cmd控制台执行测试是否正确;

可以使用winrar加入环境变量进行压缩

1
2
3
4
5
set date_str=%date:~,4%%date:~5,2%%date:~8,2%
set time_hh=%time:~0,2%
if /i %time_hh% LSS 10 (set time_hh=0%time:~1,1%)
set data_time_str=%date:~,4%%date:~5,2%%date:~8,2%_%time_hh%%time:~3,2%%time:~6,2%
rar a db_%data_time_str%.rar -m5 -s -r *.bak

在windows-控制面板-管理工具下,打开任务计划,创建基本任务,输入任务名称描述、设置备份时间、选择启动程序。需要注意的是在起始于选项里输入程序执行路径,完成。

bat

删除大于7天的备份

1
2
3
4
5
6
7
8
9
10
delete.bat

@echo off&setlocal enabledelayedexpansion
pushd E:\DBbackup
set n=0
for /f "delims=" %%a in ('dir /a-d-h /b /o-d shop_lepeng*.rar') do (
if !n! geq 7 del "%%~a"
set /a n+=1
)
popd

备份mongodb

备份命令格式

1
mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件

无密码备份

1
2
3
4
5
6
7
8
9
10
[root@database data]# mongodump -h 192.168.50.51 -d B2BMall -o /data/mongodbbackup/mongodbbackup-`date "+%Y-%m-%d"`
2020-12-23T15:01:09.747+0800 writing B2BMall.VLoginToken to
2020-12-23T15:01:09.747+0800 writing B2BMall.SmallProgramUserAccount to
2020-12-23T15:01:09.748+0800 writing B2BMall.VLoginAccount to
2020-12-23T15:01:09.759+0800 done dumping B2BMall.VLoginToken (3324 documents)
2020-12-23T15:01:09.759+0800 done dumping B2BMall.SmallProgramUserAccount (18 documents)
2020-12-23T15:01:09.761+0800 done dumping B2BMall.VLoginAccount (0 documents)
[root@database mongodbbackup]# ll mongodbbackup-2020-12-23/B2BMall/
SmallProgramUserAccount.bson VLoginAccount.bson VLoginToken.bson
SmallProgramUserAccount.metadata.json VLoginAccount.metadata.json VLoginToken.metadata.json

恢复Mysql

安装mysql

1
2
3
4
5
6
7
wget https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
#如不能使用使用yum-config-manager请使用yum -y install yum-utils
yum install mysql-community-server
yum -y install mysql-devel

启动mysql

1
[root@dev-database ~]# systemctl start mysqld

查看临时密码

1
2
[root@dev-database ~]# grep "temporary password" /var/log/mysqld.log
2020-10-27T08:30:11.011852Z 1 [Note] A temporary password is generated for root@localhost: iEOu+Tz47m-)

登录mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@dev-database ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.32

Copyright (c) 2000, 2020, 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> ALTER USER USER() IDENTIFIED BY 'Liangjian@360';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by 'Liangjian@360';
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

创建用户

1
CREATE USER 'hospitalTest'@'%' IDENTIFIED BY 'Liangjian123360@8899';

授权

1
GRANT ALL ON *.* TO 'hospitalTest'@'%';

恢复数据库

1
2
3
4
mysql -uhospitalTest -pLiangjian123360@8899
CREATE DATABASE hospunion;
use hospunion;
source /home/hospunion-20-10-27.sql
恢复单个库

方法一:

从全备中直接导入单个库

格式:mysql -u用户 -p密码 单个数据库名 –one-database < 全备的sql文件

1
mysql -uroot -p'nmk@0718'  test  -o  < databases-2022-07-08.sql

方法二:

从全备中直接导出库数据到新的sql文件

格式:sed -n ‘/^– Current Database: 表名/,/^– Current Database: `/p’ 全备sql文件 > 新sql文件

1
sed -n '/^-- Current Database: `test`/,/^-- Current Database: `/p' databases-2022-07-08.sql > test.sql

将提取出的新的sql数据导入数据库

1
mysql -uroot -p'nmk@0718'  < test.sql
恢复单个表

方法一:

从全备份中导出该表的建表语句到新的sql文件中

格式:sed -e’/./{H;$!d;}’ -e ‘x;/CREATE TABLE 表名/!d;q’ 全备sql文件 > 新sql文件

1
sed -e'/./{H;$!d;}' -e 'x;/CREATE TABLE `test`/!d;q' databases-2022-07-08.sql > test.sql

从全备份中导出该表的insert into语句追加到上一个sql文件中

格式:grep -i ‘INSERT INTO 表名‘ 全备sql文件 >> 含建表语句的sql文件

1
grep -i 'INSERT INTO `test`' databases-2022-07-08.sql >> test.sql

导入到对应的库中

格式:
use 对应的库名;
source 导出的sql文件;

1
2
use test;
source /root/test.sql;

mysql问题

ERROR 1805 (HY000): Column count of mysql.user is wrong. Expected 45, found 43. The table is probabl

1
2
mysql> create user 'turn123'@'localhost' identified by 'abc123'; 
ERROR 1805 (HY000): Column count of mysql.user is wrong. Expected 45, found 43. The table is probably corrupted

解决

1
2
/usr/bin/mysql_upgrade -uroot -p -S /var/lib/mysql/mysql.sock
不能本地登录使用/usr/bin/mysql_upgrade -S /var/lib/mysql/mysql.sock

恢复Postgresql

安装postgresql

1
2
3
4
5
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql12-server
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl enable postgresql-12
systemctl start postgresql-12

切换至postgres用户

1
su postgres

连接数据库

1
psql

修改密码

1
2
postgres=# ALTER ROLE postgres WITH PASSWORD 'Liangjian@360';
ALTER ROLE

将认证方式修改为密码认证,打开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/var/lib/pgsql/12/data/pg_hba.conf

更改为以下配置
# TYPE DATABASE USER ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication all peer
#host replication all 127.0.0.1/32 ident
#host replication all ::1/128 ident
host all all 0.0.0.0/0 md5

开放远程连接

1
2
3
4
/var/lib/pgsql/12/data/postgresql.conf 

加入以下配置
listen_addresses = '*'

重载配置

1
2
3
4
5
6

postgres=# SELECT pg_reload_conf();
pg_reload_conf
----------------
t
(1 row)

连接数据库

1
psql -U postgres -W

创建数据库

1
createdb dbname

删除数据库

1
dropdb dbname

恢复数据库

1
psql -U postgres -f pgbackup-2020-10-27.bak

数据库恢复完,postgres远程连接密码会默认变为生产数据库密码,需要手动进行修改才可远程连接,本地连接密码不变

1
alter user postgres with password 'Liangjian@360';

恢复sqlserver

下载sql server的源,便于通过yum命令来安装

1
2
3
4
5
6
7
8
9
10
11
[root@dev-database home]# curl https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo > /etc/yum.repos.d/mssql-server.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 232 100 232 0 0 153 0 0:00:01 0:00:01 --:--:-- 153
[root@dev-database home]# cat /etc/yum.repos.d/mssql-server.repo
[packages-microsoft-com-mssql-server-2017]
name=packages-microsoft-com-mssql-server-2017
baseurl=https://packages.microsoft.com/rhel/7/mssql-server-2017/
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc

安装

1
yum install -y mssql-server

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@dev-database home]# /opt/mssql/bin/mssql-conf setup
Choose an edition of SQL Server:
1) Evaluation (free, no production use rights, 180-day limit)
2) Developer (free, no production use rights)
3) Express (free)
4) Web (PAID)
5) Standard (PAID)
6) Enterprise (PAID)
7) Enterprise Core (PAID)
8) I bought a license through a retail sales channel and have a product key to enter.

Details about editions can be found at
https://go.microsoft.com/fwlink/?LinkId=852748&clcid=0x409

Use of PAID editions of this software requires separate licensing through a
Microsoft Volume Licensing program.
By choosing a PAID edition, you are verifying that you have the appropriate
number of licenses in place to install and run this software.

Enter your edition(1-8): 2
The license terms for this product can be found in
/usr/share/doc/mssql-server or downloaded from:
https://go.microsoft.com/fwlink/?LinkId=855862&clcid=0x409

The privacy statement can be viewed at:
https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409

Do you accept the license terms? [Yes/No]:
yes

Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
Configuring SQL Server...

ForceFlush is enabled for this instance.
ForceFlush feature is enabled for log durability.
Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service.
Setup has completed successfully. SQL Server is now starting.

查看状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@dev-database home]# systemctl status mssql-server
● mssql-server.service - Microsoft SQL Server Database Engine
Loaded: loaded (/usr/lib/systemd/system/mssql-server.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2021-01-31 16:30:10 CST; 28s ago
Docs: https://docs.microsoft.com/en-us/sql/linux
Main PID: 34727 (sqlservr)
CGroup: /system.slice/mssql-server.service
├─34727 /opt/mssql/bin/sqlservr
└─34747 /opt/mssql/bin/sqlservr

Jan 31 16:30:15 dev-database sqlservr[34727]: 2021-01-31 16:30:15.47 spid19s SQL Server is now ready for client connections. This is an informational message; no user action is required.
Jan 31 16:30:15 dev-database sqlservr[34727]: 2021-01-31 16:30:15.48 spid8s 0 transactions rolled back in database 'msdb' (4:0). This is an informational message only. No user action is required.
Jan 31 16:30:15 dev-database sqlservr[34727]: 2021-01-31 16:30:15.51 spid11s Polybase feature disabled.
Jan 31 16:30:15 dev-database sqlservr[34727]: 2021-01-31 16:30:15.51 spid11s Clearing tempdb database.
Jan 31 16:30:15 dev-database sqlservr[34727]: 2021-01-31 16:30:15.87 spid11s Starting up database 'tempdb'.
Jan 31 16:30:16 dev-database sqlservr[34727]: 2021-01-31 16:30:16.06 spid11s The tempdb database has 1 data file(s).
Jan 31 16:30:16 dev-database sqlservr[34727]: 2021-01-31 16:30:16.08 spid22s The Service Broker endpoint is in disabled or stopped state.
Jan 31 16:30:16 dev-database sqlservr[34727]: 2021-01-31 16:30:16.09 spid22s The Database Mirroring endpoint is in disabled or stopped state.
Jan 31 16:30:16 dev-database sqlservr[34727]: 2021-01-31 16:30:16.11 spid22s Service Broker manager has started.
Jan 31 16:30:16 dev-database sqlservr[34727]: 2021-01-31 16:30:16.11 spid8s Recovery is complete. This is an informational message only. No user action is required.

开机启动

1
sudo systemctl enable mssql-server

连接sqlserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
yum install -y mssql-tools unixODBC-devel

echo "export PATH=$PATH:/opt/mssql-tools/bin" >> /etc/profile

[root@dev-database home]# sqlcmd -S localhost -U sa
Password:
1> select db_name();
2> go

--------------------------------------------------------------------------------------------------------------------------------
master

(1 rows affected)
1>

还原数据库


恢复mongdb

配置系统yum源

  • 创建.repo文件,生成mongodb的源

    1
    vi /etc/yum.repos.d/mongodb-org-4.0.repo
  • 添加以下配置信息:

    1
    2
    3
    4
    5
    6
    [mongodb-org-4.0]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
    gpgcheck=0
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
  • 安装MongoDB

    1
    yum install -y mongodb-org
  • 启动MongoDB

    1
    systemctl start mongod.service
  • MongoDB默认端口是27017,查看是否开启

    1
    netstat -natp | grep 27017
  • 验证服务开启

    1
    mongo
  • 远程连接Mongodb

    1
    2
    3
    4
    5
    6
    7
    vi /etc/mongod.conf

    # network interfaces
    net:
    port: 27017
    bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
    #修改绑定ip默认127.0.0.1只允许本地连接, 所以修改为bindIp:0.0.0.0
  • 重启mongodb服务

    1
    service mongod restart

PostgreSQL 慢查询SQL语句跟踪

开启日志跟踪

进入postgresql的安装目录下修改配置文件

1
2
3
4
5
6
7
cd /var/lib/pgsql/12/data
vi postgresql.conf

logging_collector = on
log_destination = 'stderr'
log_directory = 'log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

默认的跟踪日志记录在 data/log 中,例如:/var/lib/pgsql/12/data/log

1
2
3
log_rotation_age = 1440    #minute,多长时间创建新的文件记录日志。0 表示禁扩展。
log_rotation_size = 10240 #kb,文件多大后创建新的文件记录日志。0 表示禁扩展。
log_truncate_on_rotation = on #可重用同名日志文件

需要跟踪SQL语句或者慢语句,得需要设置以下参数:

1
2
log_statement = all    #需设置跟踪所有语句,否则只能跟踪出错信息
log_min_duration_statement = 5000 #单位为毫秒,记录执行5秒及以上的SQL语句。

postgresql.conf参数 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
log_directory
默认: log_directory = 'log'
决定存放数据库运行日志文件的目录。可以是绝对路径,也可是相对路径(相对于数据库文件所在的路径)

log_filename:
默认: log_filename = 'postgresql-%a.log'
数据库运行日志文件的名称。
%Y、%m、%d、%H、%M和%S,分别表示年、月、日、小时、分和秒。
没有指定时间信息,系统会自动在log_filename值的末尾加上文件创建时间戳作为文件名

log_truncate_on_rotation
默认: log_truncate_on_rotation = on
系统在创建一个新的数据库运行日志文件时,如果发现存在一个同名的文件,当log_truncate_on_rotation的值是on时,系统覆盖这个同名文件。
当log_truncate_on_rotation的值是off时,系统将重用这个同名文件,在它的末尾添加新的日志信息。
要注意的是,只有在log_rotation_age非零时,系统才创建新的日志文件的情况下,才会覆盖同名的日志文件。

log_rotation_age
默认: log_rotation_age = 1d ,单位是分钟。
日志轮询时间
为0不是禁用该功能。

log_rotation_size
默认: log_rotation_size = 0 , 单位是KB。
日志轮询大小,当文件大小超过该值时进行切换。
如果一个日志文件写入的数据量超过log_rotation_size的值,数据库将创建一个新的日志文件。
为0表示禁用该功能。

logfilemode
默认: log_file_mode = 0600
创建日志文件的权限

log_min_duration_statement
默认: log_min_duration_statement = -1
只log执行时间大于设定值的语句,类似与慢查询
0表示log所有语句;-1表示不log任何语句。

log_duration
默认: log_duration = off
控制是否记录每个完成的SQL语句的执行时间。
对于使用扩展协议与数据库通信的客户端,会记载Parse、Bind和Execute的执行时间。

log_statement
默认: log_statement = 'none'
有效的取值是none、ddl、mod和all
控制记录哪种SQL语句的执行信息。
ddl包括所有数据定义语句,如CREATE、ALTER和DROP语句。
mod包括所有ddl语句和更新数据的语句,例如INSERT、UPDATE、DELETE、TRUNCATE、 COPY FROM、PREPARE和 EXECUTE。
All包括所有的语句。

当 log_statement=all 和 log_min_duration_statement 同时设置时,将跟踪所有语句,忽略log_min_duration_statement 设置。所以需按情况设置其中一个或两个值。

加载配置

1
2
select pg_reload_conf();
show log_min_duration_statement;

针对某个用户或者某数据库进行设置

1
alter database test set log_min_duration_statement=5000;

捕获正在查询的慢SQL

1
select * from pg_stat_activity where state<>'idle' and now()-query_start > interval '5 s' order by query_start ;

查看最大连接数

1
show max_connections;

查看连接数

1
select count(1) from pg_stat_activity;

参考链接:https://www.cnblogs.com/VicLiu/p/12017704.html
https://blog.csdn.net/liyingke112/article/details/84913510

pgsql断开数据库连接

1
2
3
4
5
6
7
8
9
10
11
select * from pg_stat_activity

SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
pid <> pg_backend_pid()
-- don't kill the connections to other databases
AND datname = 'databasename'

pgsql创建排序规则和字符分类为zh_CN.UTF-8的数据库

1
CREATE DATABASE ymall TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'zh_CN.UTF-8' LC_CTYPE 'zh_CN.UTF-8';