drinke9 发布的文章

MongoDB 权限配置

MongoDB 默认没有管理员账号,如果需要开启权限必须先添加管理员账号。

  • 切换到 admin (Collection-集合)
> use admin
switched to db admin
> show collections
system.version
  • 创建超级管理员用户
> db.createUser({user:'root',pwd:'root',roles:["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
> show users
{
    "_id" : "admin.root",
    "user" : "root",
    "db" : "admin",
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
}
##  官方例子
use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
## 官方例子
use reporting
db.createUser(
  {
    user: "reportsUser",
    pwd: "12345678",
    roles: [
       { role: "read", db: "reporting" },
       { role: "read", db: "products" },
       { role: "read", db: "sales" },
       { role: "readWrite", db: "accounts" }
    ]
  }
)
  • 切到所在集合,创建用户
>use mycollections
>db.createUser(
  {
    user: "reportsUser",
    pwd: "12345678",
    roles: [
       { role: "read", db: "reporting" },
       { role: "read", db: "products" },
       { role: "read", db: "sales" },
       { role: "readWrite", db: "accounts" }
    ]
  }
)

MongoDB 开启Auth认证

修改配置文件mongod.conf

security:
  authorization: enabled
  #authorization: disabled

## 网络上很多资料是以下配置,
auth=true

Enable Access Control

MongoDB does not enable access control by default. You can enable authorization using the --auth or the security.authorization setting. Enabling internal authentication also enables client authorization.

官方配置文档

Laravel中使用MongoDB

安装扩展包composer require jenssegers/mongodb.
修改config/database.php配置文件

'mongodb' => [
       'driver'   => 'mongodb',
       'host'     => env('MONGO_DB_HOST', 'localhost'),
       'port'     => env('MONGO_DB_PORT', 27017),
       'database' => env('MONGO_DB_DATABASE'),
       'username' => env('MONGO_DB_USERNAME'),
       'password' => env('MONGO_DB_PASSWORD'),
       'options'  => [
           'database' => env('MONGO_DB_DATABASE') // sets the authentication database required by mongo 3
      ]
],

MongoDB是强类型语言,PHP中对MongoDB操作时要注意类型问题!

以下两个查询会得到不一样的结果:

> db.users.find({user_id:1})
> db.users.find({user_id:"1"})

Reference

  1. https://blog.csdn.net/kk185800961/article/details/45619863
  2. https://www.jianshu.com/p/f5afc6488f9e
  3. https://docs.mongodb.com/manual/core/authorization/

使用-h 命令查看具体参数

[email protected]:~/gogs$ ./gogs -h
NAME:
   Gogs - A painless self-hosted Git service

USAGE:
   gogs [global options] command [command options] [arguments...]

VERSION:
   0.11.43.0330

COMMANDS:
     web      Start web server
     serv     This command should only be called by SSH shell
     hook     Delegate commands to corresponding Git hooks
     cert     Generate self-signed certificate
     admin    Perform admin operations on command line
     import   Import portable data as local Gogs data
     backup   Backup files and database
     restore  Restore files and database from backup
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h     show help
   --version, -v  print the version

- 阅读剩余部分 -

需要配置两处地方!

  • nginx.conf
#在 http 块中 添加以下配置
fastcgi_intercept_errors on;
  • your.custom.conf
#在server块中添加
#有两种指定方式
error_page 404 /404.html; #当前配置默认路径是从nginx安装路径;
# OR 指定特定路由地址
# error_page 404 https://happyhack.cn/404.html;
#如需自定义路径,添加一个location块
location /404.html{
    root /data/wwwroot/errorpages;
}

也可以指定其他状态码到同一个页面。使用正则同样可以!

error_page 404 = 401 /your/errorpage.html;

直奔主题!

使用技巧

递增和递减

要代替以下实现:

$article = Article::find($article_id);
$article->read_count++;
$article->save();

你可以这样做:

$article = Article::find($article_id);
$article->increment('read_count');

以下这些方法也可以实现:

Article::find($article_id)->increment('read_count');
Article::find($article_id)->increment('read_count', 10); // +10
Product::find($produce_id)->decrement('stock'); // -1

模型的 boot() 方法

在一个 Eloquent 模型中,有个神奇的地方,叫 boot(),在那里,你可以覆盖默认的行为:

class User extends Model
{
    public static function boot()
    {
        parent::boot();
        static::updating(function($model)
        {
            // 写点日志啥的
            // 覆盖一些属性,类似这样 $model->something = transform($something);
        });
    }
}

- 阅读剩余部分 -