文章851
标签121
分类10

使用 MongoDB 命令连接远程服务器的 MongoDB 数据库

MongoDB 是一个高性能、无模式的 NoSQL 数据库,广泛应用于现代应用开发。在许多情况下,您可能需要连接到远程服务器上的 MongoDB 数据库。本文将介绍如何使用 MongoDB 命令行工具连接远程 MongoDB 数据库的步骤。

1. 安装 MongoDB 客户端

在连接远程 MongoDB 数据库之前,确保您的系统上已安装 MongoDB 客户端。可以从 MongoDB 官方网站 下载并安装适合您操作系统的版本。

2. 获取连接信息

在连接到远程 MongoDB 数据库之前,您需要获取以下信息:

  • 远程服务器的 IP 地址 或主机名
  • MongoDB 服务端口(默认是 27017)
  • 数据库名称
  • 用户名和密码(如果启用了身份验证)

3. 使用 mongo 命令连接

MongoDB 提供了 mongo 命令行工具来连接数据库。基本的连接命令格式如下:

mongo <host>:<port>/<database> -u <username> -p <password>

示例

假设您要连接到远程 MongoDB 数据库,以下是示例信息:

  • 远程服务器 IP:192.168.1.100
  • 端口:27017
  • 数据库名称:mydatabase
  • 用户名:myuser
  • 密码:mypassword

您可以使用以下命令连接到数据库:

mongo 192.168.1.100:27017/mydatabase -u myuser -p mypassword

4. 连接到带身份验证的 MongoDB

如果 MongoDB 启用了身份验证,您可以在连接命令中指定用户名和密码,如上所示。

4.1 使用 --authenticationDatabase

如果用户的凭据存储在不同的数据库中,您还需要指定 --authenticationDatabase 选项。例如,如果用户在 admin 数据库中,命令如下:

mongo 192.168.1.100:27017/mydatabase -u myuser -p mypassword --authenticationDatabase admin

5. 连接成功后的操作

连接成功后,您将进入 MongoDB Shell,您可以执行各种命令,例如:

  • 列出数据库:

    show dbs
  • 切换到指定数据库:

    use mydatabase
  • 查询集合中的数据:

    db.mycollection.find()

6. 常见问题

6.1 无法连接到远程 MongoDB

  • 防火墙问题:确保远程服务器的防火墙允许访问 MongoDB 的端口(默认是 27017)。
  • MongoDB 配置:检查 MongoDB 配置文件(mongod.conf)中的 bindIp 设置,确保它允许外部连接(例如设置为 0.0.0.0 或指定的 IP 地址)。

6.2 身份验证失败

  • 确保您使用的用户名和密码是正确的,并且用户具有访问指定数据库的权限。

PHP 的命令行模式

PHP 不仅是一种用于 Web 开发的服务器端语言,还可以在命令行模式下运行。这种模式使得 PHP 成为一种强大的脚本语言,能够执行各种任务,如系统管理、批处理、文件操作等。本文将深入探讨 PHP 的命令行模式,包括其特点、使用方法以及常见应用场景。

1. 什么是 PHP 命令行模式?

PHP 的命令行模式(CLI,Command Line Interface)允许用户在命令行终端中直接运行 PHP 脚本。当 PHP 以命令行模式执行时,可以接收命令行参数并执行相应的操作。这种模式通常用于自动化任务、批处理脚本和系统管理。

2. 启动 PHP 命令行模式

在命令行中执行 PHP 脚本的基本语法如下:

php [options] [filename] [args]

示例

假设有一个名为 script.php 的 PHP 文件,可以使用以下命令运行:

php script.php

3. PHP 命令行选项

在命令行模式下,PHP 提供了一些有用的选项,可以通过 php -h 查看所有可用选项。

常见选项

  • -f:指定要执行的 PHP 文件。

    php -f script.php
  • -r:直接执行 PHP 代码,而不需要创建文件。

    php -r 'echo "Hello, World!\n";'
  • -a:进入交互模式,允许您逐行输入 PHP 代码。

    php -a
  • -d:在执行时设置 PHP 配置选项。

    php -d memory_limit=512M script.php

4. 处理命令行参数

在命令行模式下,您可以使用 $argv$argc 数组来处理命令行参数。

  • $argv:一个数组,包含所有传递给脚本的参数。
  • $argc:一个整数,表示参数的数量。

示例代码

以下示例展示如何访问和使用命令行参数:

<?php
if ($argc > 1) {
    echo "脚本名称: " . $argv[0] . "\n";
    echo "传递的参数:\n";
    for ($i = 1; $i < $argc; $i++) {
        echo "参数 $i: " . $argv[$i] . "\n";
    }
} else {
    echo "没有传递任何参数。\n";
}
?>

运行示例

假设将上述代码保存为 params.php,并使用以下命令运行:

php params.php hello world

输出结果

脚本名称: params.php
传递的参数:
参数 1: hello
参数 2: world

PHP 中的钩子:register_shutdown_function

在 PHP 中,register_shutdown_function 是一个非常有用的钩子函数,用于注册一个函数在脚本执行结束时自动调用。无论是正常结束还是由于错误或异常而终止,注册的函数都会被执行。这使得 register_shutdown_function 成为进行清理工作、记录日志或释放资源的理想选择。

1. 基本用法

1.1 注册钩子函数

使用 register_shutdown_function 可以轻松注册一个在脚本结束时运行的函数。以下是基本的示例:

<?php
function shutdownFunction() {
    echo "脚本执行结束,执行清理工作。\n";
}

register_shutdown_function('shutdownFunction');

// 模拟一些代码
echo "正在执行代码...\n";
?>

1.2 输出结果

当执行上述代码时,输出将是:

正在执行代码...
脚本执行结束,执行清理工作。

2. 错误处理

register_shutdown_function 还可以用于处理错误和异常。在脚本发生致命错误时,注册的函数也会被调用。这使得开发者能够捕获错误信息并进行处理。

示例:捕获错误信息

<?php
function shutdownFunction() {
    $error = error_get_last();
    if ($error) {
        echo "脚本出现错误:{$error['message']}\n";
    } else {
        echo "脚本正常结束。\n";
    }
}

register_shutdown_function('shutdownFunction');

// 模拟错误
echo "正在执行代码...\n";
undefinedFunction(); // 触发致命错误
?>

输出结果

当执行上述代码时,输出将是:

正在执行代码...
脚本出现错误:Call to undefined function undefinedFunction()

3. 使用场景

3.1 清理资源

在脚本结束时释放占用的资源,例如关闭数据库连接或文件句柄。

3.2 记录日志

在脚本执行结束时记录日志信息,方便后期调试和审计。

3.3 处理错误

捕获脚本中的致命错误并进行处理,如发送错误报告或执行备用逻辑。

4. 注意事项

  • 执行顺序:如果注册多个钩子函数,register_shutdown_function 将按照注册的顺序执行。
  • 不适用于输出缓冲:如果使用了输出缓冲(ob_start()),在钩子函数中可能无法正确输出内容。
  • PHP 版本:确保使用的 PHP 版本支持该函数。

CentOS 安装与卸载docker

一、 安装Docker环境

1.使用官方安装脚本自动安装

安装命令如下:

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

也可以使用国内daocloud一键安装命令:

curl -sSL https://get.daocloud.io/docker | sh

2.使用Docker仓库进行安装

  • 下载关于Docker的依赖环境

    yum -y install yum-utils device-mapper-persistent-data lvm2
  • 设置下载Docker的镜像源

    • 使用官方源(国内较慢)

      yum-config-manager  --add-repo https://download.docker.com/linux/centos/docker-ce.repo
      • 国内的源地址

        • 阿里云

          yum-config-manager  --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        • 清华大学源

          yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
      • 安装Docker

        yum makacache fast
        yum -y install docker-ce

    二、清理卸载Docker环境

1.杀死所有运行容器

docker kill $(docker ps -a -q)

2.删除所有容器

docker rm $(docker ps -a -q)

3.删除所有镜像

docker rmi $(docker images -q)

4.停止docker服务

systemctl stop docker

5.删除存储目录

rm -rf /etc/docker
rm -rf /run/docker
rm -rf /var/lib/docker

6.卸载docker

  • 查看已安装的docker包
yum list installed | grep docker
  • 卸载相关包
yum remove docker*

`New` CentOS下使用certbot申请、部署Let's Encrypt免费SSL证书

网站使用 HTTPS 协议已是大势所趋,而要在 web 上使用 HTTPS 的话,我们首先需要获得一个 SSL 证书文件。本文介绍如何在 CentOS7 + Nginx 环境下,安装使用 Let’s Encrypt 免费 SSL 证书。

1. 准备工作

(1)首先安装Nginx服务器,并确保正常运行。

yum install nginx -y
systemctl start nginx  //启动Nginx
systemctl enable nginx  //设置Nginx开机自启动

(2)服务器要开放80端口以及443端口

firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload

2. 安装certbot工具

我们采用certbot脚本方式申请let’s Encrypt证书,依次执行如下命令安装该工具:

yum install -y epel-release
yum install -y certbot python2-certbot-nginx

3. 申请证书

接下来我们要使用certbot命令初次申请证书:

[root ~]# certbot certonly --nginx -d blog.top -d www.blog.top 

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Requesting a certificate for blog.top and www.blog.top

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/blog.top/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/blog.top/privkey.pem
   Your certificate will expire on 2025-01-17. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again. To non-interactively renew *all* of your
   certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

申请成功后,证书会保存在 /etc/letsencrypt/live/blog.top/ 下面:

[root@ ~]# ls /etc/letsencrypt/live/blog.top/
cert.pem  chain.pem  fullchain.pem  privkey.pem  README

使用如下命令可以查看证书的有效期 :

openssl x509 -noout -dates -in /etc/letsencrypt/live/blog.top/fullchain.pem

4. 证书更新

1) Let’s Encrypt 证书的有效期是 `90 天,需要长期使用的话,需要在失效前进行延长申请。我们可以执行如下命令去更新:

//更新证书
certbot renew --dry-run
 
//如果不需要返回的信息,可以用静默方式
certbot renew --quiet

2) 我们也可以将更新证书的脚本写到定时任务来自动完成,免得我们手动操作。首先执行如下命令开始编辑定时任务:

crontab -e

3)此时会进入 vi 的编辑界面让你编辑工作(每项工作都是一行)。我们在末尾添加如下一行内容,表示每月 1 号 5 时会执行执行一次更新,并重启 nginx 服务器:

00 05 01 * * /usr/bin/certbot renew --quiet && /bin/systemctl restart nginx

5. 配置Nginx

server
{
        listen 80;
        server_name www.blog.top;

        rewrite ^(.*)$ https://$host$request_uri;
}

server
{
        listen 80;
        server_name blog.top;

        return 301 https://www.blog.top$request_uri;
}

server {
        listen 443;

        server_name blog.top;

        ssl on;
        ssl_certificate /etc/letsencrypt/live/blog.top/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/blog.top/privkey.pem;

        return 301 https://www.blog.top$request_uri;
}

server {
        listen 443;

        #server_name www.blog.top;
        set $root /opt/webserver/wwww;
        root $root;

        ssl on;
        ssl_certificate /etc/letsencrypt/live/blog.top/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/blog.top/privkey.pem;

        location ~* /index.php/archives/(\d+)$ {
                return 301 https://$host/index.php/archives/$1.html;
        }
        location ~* /index.php/archives/(\d+)/$ {
                return 301 https://$host/index.php/archives/$1.html;
        }

        # 下面直接照写
        location / {
                try_files $uri $uri/ /$is_args$args;
        }

        location ~ \.php$ {

                root $root;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html{
        }


        location  ~ .*\.(jpg|ipa|jpeg|gif|png|ico|css|js|pdf|txt)
        {
                root $root;
                proxy_temp_path $root;
        }
}
">