本文将为您提供关于CentOS安装MongoDB的详细介绍,我们还将为您解释centos安装rpm软件包的命令的相关知识,同时,我们还将为您提供关于11-【MongoDB入门教程】MongoDB原子性
本文将为您提供关于CentOS 安装 MongoDB的详细介绍,我们还将为您解释centos 安装rpm软件包的命令的相关知识,同时,我们还将为您提供关于11-【MongoDB 入门教程】MongoDB 原子性和事务、ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接、CentOS 7.0 yum install 错误http://vault.centos.org/centos/7/os/Source/repodata/repomd.xml: [Errno 14]、CentOS 7设置开机启动服务,添加自定义系统服务 centos 7 关闭防火墙 centos 7.2 centos 7 64位下载的实用信息。
本文目录一览:- CentOS 安装 MongoDB(centos 安装rpm软件包的命令)
- 11-【MongoDB 入门教程】MongoDB 原子性和事务
- ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接
- CentOS 7.0 yum install 错误http://vault.centos.org/centos/7/os/Source/repodata/repomd.xml: [Errno 14]
- CentOS 7设置开机启动服务,添加自定义系统服务 centos 7 关闭防火墙 centos 7.2 centos 7 64位下载
CentOS 安装 MongoDB(centos 安装rpm软件包的命令)
以阿里云服务器,以 CentOS 系统为例
一、下载安装
(1)下载: 官网
方法一: 服务器上 wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-server-4.0.5-1.el7.x86_64.rpm
方法二: 客户端下载压缩包再上传至服务器
(2) 解压
1 $ tar xz -d mongodb-org-server-4.0.5-1.tag.xz
2 $ tar -vxf mongodb-org-server-4.0.5-1.tar
(3) 进入 命令行工具
$ cd mongodb-org-server-4.0.5-1/bin
(4) 创建软连接
1 ## 假定下载文件存放在/usr/local/目录下
2
3 $ ln -s /usr/local/mongodb-org-server-4.0.5-1/bin/mongod /usr/bin/mongod
4 $ ln -s /usr/local/mongodb-org-server-4.0.5-1/bin/mongo /usr/bin/mongo
(5) 配置(需要手动创建并设置数据存储和日志存放目录,否则因为找不到而报错)
1 $ vim /etc/mongod.conf
创建并打开 mongd.conf , 有些教程配置文件放在下载的 mongdb 目录下,但当设置为外部访问时可能会出现各种问题,这里索性在 /etc/ 下创建。创建后添加以下配置
1 ## 这里是部分基础配置,需要更多的可自行查询
2
3 #这是存储数据文件路径,自定义
4 dbpath=/usr/local/src/mongodb-v4.0.5/data/db
5
6 #这是日志存储文件,自定义
7 logpath=/usr/local/src/mongodb-v4.0.5/logs/mongo.log
8
9 #日志追加而不是覆盖
10 logappend=true
11
12 # 启用日志选项
13 journal=true
14
15 # 安静输出
16 quiet=true
17
18 # 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP, 若是0.0.0.0外网可访问
19 bind_ip=0.0.0.0
20
21 # 端口号
22 port=27017
23
24 # 以守护进程的方式运行MongoDB,创建服务器进程
25 fork=true
26
27 # 是否以安全认证方式运行,默认是不认证的非安全方式
28 #noauth = true
29 #auth = true
30
31 # 详细记录输出
32 #verbose = true
其他参数参考 https://blog.csdn.net/fdipzone/article/details/7442162
(6) 测试运行
1 $ mongod
2 $ mongo
启动 mongodb
进入 mongdb , 在后面输入 show dbs 可以显示当前的数据库 (admin/config/local)
二、客户端连接
(1) 客户端访问前需要将配置文件 bind_ip 设置为 0.0.0.0
(2) 将 27017 (使用的端口) 添加阿里云服务器实例的 “安全组规则” < 之前搭建 node 项目时有提过 >
(3) 服务器向外开放端口
$ iptables -I INPUT -p tcp --dport 27017 -j ACCEPT
$ service iptables save
$ service network restart
(4) 客户端浏览器访问 ip 地址 + 27017 (如 123.22.71.12:27017) ,显示以下表示连接成功, 此时可以使用可视化工具连接操作。
It looks like you are trying to access MongoDB over HTTP on the native driver port.
11-【MongoDB 入门教程】MongoDB 原子性和事务
在 MongoDB 中,写操作的原子性是在 document
级别上的,即使修改的是文档中的内嵌部分,写锁的级别也是 document
上。
当一个写操作要修改多个文档,每个文档的修改是原子性的。整个的写操作并不是原子性的,它可能和其他写操作产生交织。然而你可以使用 $isolated
隔离操作符来限制写操作,让它不与其他写操作交织。 不隔离性能更高,但是会产生数据的不确定性,隔离写操作,事务性更好。MongoDB 把这个级别完全由用户控制。
隔离写操作
MongoDB 使用 $isolated
操作符来隔离写操作。如果一个写操作要更新多个文档,它能防止其他进程与本次写操作交错。直到这个写操作完成,其他进程才能写。
但是,$isolated
算不上一个事务,如果在写的过程中发生错误,MongoDB 并不会回滚已经写的数据。$isolated
也不能在分片集群上工作。
特性:
- 隔离不支持分片集群
- 不支持 “all-or-nothing” 特性
- MongoDB2.2 版本后
$isolated
被替换成$atomic
类事务语法
MongoDB 并不支持关系型数据库中的那种事务特性,为了性能着想,它把这个特性交给程序员去实现。这就是 MongoDB 官方所讲的 Two Phase Commits 两阶段提交。这个技术虽然在一定程度上能保证数据最终的一致性,但是应用程序还是可能会读到提交或者回滚过程中的中间数据。对于这个技术如果有兴趣可以读一读原文。
#并发控制#
并发控制允许多个应用层程序同时访问数据库,而不引起数据不一致或冲突。
MongoDB 中提到两种技术来解决这个问题。第一种是唯一索引,第二种是叫 Update if Current
。
用唯一索引来防止多个进程重复插入或者更新导致的重复的值。 Update if Current
意思是说在更新数据的时候,在更新条件里给定一个期望的值(这个值是先查询出来的),用来防止在更新之前其他进程已经将此值更新。看一个例子:
var myDocument = db.products.findOne( { sku: "abc123" } );
if ( myDocument ) {
var oldQuantity = myDocument.quantity;
var oldReordered = myDocument.reordered;
var results = db.products.update(
{
_id: myDocument._id,
quantity: oldQuantity,
reordered: oldReordered
},
{
$inc: { quantity: 50 },
$set: { reordered: true }
}
)
if ( results.hasWriteError() ) {
print( "unexpected error updating document: " + tojson(results) );
}
else if ( results.nMatched === 0 ) {
print( "No matching document for " +
"{ _id: "+ myDocument._id.toString() +
", quantity: " + oldQuantity +
", reordered: " + oldReordered
+ " } "
);
}
}
同样的,在 findAndModify () 函数中:
db.people.findAndModify({
query: { name: "Andy" },
sort: { rating: 1 },
update: { $inc: { score: 1 } },
upsert: true
})
如果有多个进程同时调用此函数,这些进程都完成了查询阶段,如果 name
字段上没有唯一索引,upsert 阶段的操作,多个进程可能都会执行。导致写入重复的文档。
ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接
如何解决ArgumentException:MongoDB + Unity2D 中的无效关键字 ''mongodb+srv://test:test@HOST'' - Live MongoDB 未连接?
我试图在本地连接我的 Live MongoDB 数据库,但在我使用外部字符串而不是 localhost
时出错
我使用:
- MongoDB 数据库 v4.0.x(使用 mongoDB 的免费集群)
- Unity 2020.3.13
如果我使用 localhost,即使所有的插入、更新、删除查询都能正常工作,这段代码也能正常工作
using UnityEngine;
using UnityEngine.UI;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Collections;
using UnityEngine.Networking;
using MongoDB.Driver.Builders;
using UnityEngine.SceneManagement;
using System;
using Newtonsoft.Json ;
public class Login_Screen : MonoBehavIoUr {
public InputField usernameedit;
public InputField passwordone;
public Button loginButton;
void start(){
string connectionString = "mongodb://localhost:27017";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("admin");
var playercollection = database.GetCollection<BsonDocument>("users");
}
}
但是我用于 LIVE 数据库调用的相同内容,然后导致以下错误。甚至连不上。
string connectionString = "mongodb+srv://testUsername:testPassword@testClustor.mongodb.net?retryWrites=true&w=majority";
错误信息如下:
ArgumentException: Invalid keyword ''mongodb+srv://testusername:testpassword@xyz.xxx.mongodb.net/admin1?w''. MongoDB.Driver.MongoConnectionStringBuilder.set_Item (System.String keyword,System.Object value) (at <6da29fc855c44d33ad78b3e27475ff27>:0) System.Data.Common.DbConnectionStringBuilder.set_ConnectionString (System.String value) (at <290425a50ff84a639f8c060e2d4530f6>:0) MongoDB.Driver.MongoConnectionStringBuilder..ctor (System.String connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0) MongoDB.Driver.MongoClient.ParseConnectionString (System.String connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0) MongoDB.Driver.MongoClient..ctor (System.String connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0) Login_Screen.DoLogin () (at Assets/Script/Login_Screen.cs:61) UnityEngine.Events.InvokableCall.Invoke () (at <24599fe2776145d58ab771516c063d56>:0) UnityEngine.Events.UnityEvent.Invoke () (at <24599fe2776145d58ab771516c063d56>:0) UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:68) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:110) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler,UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target,UnityEngine.EventSystems.BaseEventData eventData,UnityEngine.EventSystems.ExecuteEvents+EventFunction1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:262) UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:385)
我真的浪费了 2 天的时间来寻找解决方案,请任何人帮助我。提前致谢
解决方法
我遇到了同样的问题。检查您的 db 和 db 用户的设置 - 确保它们可以从外部 IP 访问。
我像这样修改了我的连接字符串:
private readonly Dictionary<string,string> _config = new Dictionary<string,string>()
{
{"dbUser","xxxxxx"},{"dbName","YourDbName"},{"password","xxxxxxxxxxxxx"}
};
public IMongoDatabase CreateInstance()
{
var client = new MongoClient($"mongodb+srv://{_config["dbUser"]}:{HttpUtility.UrlEncode(_config["password"])}@pocdata.0u29g.mongodb.net/{HttpUtility.UrlEncode(_config["dbName"])}?retryWrites=true&w=majority");
return client.GetDatabase(_config["dbName"]);
}
我只是使用公共方法连接到数据库,但这不是强制性的 - 您可以直接替换值 - 只需对密码和数据库名称进行 url 编码
CentOS 7.0 yum install 错误http://vault.centos.org/centos/7/os/Source/repodata/repomd.xml: [Errno 14]
执行yum install vconfig时老是报错:
http://vault.centos.org/centos/7/os/Source/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
经过分析:
原因是标准的CentOS 7.0 Build1406里面很多的仓库已经被CentOS移除了,转到CentOS 7.1 Build1503和CentOS7.2 Build1503了。
因此需要手工更改仓库文件/etc/yum.d.repo/CentOS-Sources.repo,
更改之前,先备份下
#cp CentOS-Sources.repo CentOS-Sources.repo.bak
然后开始编辑CentOS-Sources.repo文件。
将文件中的“$releasever”全部替换成“7.2.1511”或者“7.1.1503”。
[base-source]
name=CentOS-7.2.1511 - Base Sources
baseurl=http://vault.centos.org/centos/7.2.1511/os/Source/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates-source]
name=CentOS-7.2.1511 - Updates Sources
baseurl=http://vault.centos.org/centos/7.2.1511/updates/Source/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras-source]
name=CentOS-7.2.1511 - Extras Sources
baseurl=http://vault.centos.org/centos/7.2.1511/extras/Source/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus-source]
name=CentOS-7.2.1511 - Plus Sources
baseurl=http://vault.centos.org/centos/7.2.1511/centosplus/Source/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
更改完成后执行
#yum clean
#yum update
CentOS 7设置开机启动服务,添加自定义系统服务 centos 7 关闭防火墙 centos 7.2 centos 7 64位下载
centos 7设置开机启动服务,添加自定义系统服务
- 建立服务文件
- 保存目录
- 设置开机自启动
- 其他命令
1.建立服务文件
文件路径
vim /usr/lib/systemd/<span>system</span>/nginx.service
服务文件内容
<span>[Unit]</span><span>Description=<span>nginx - high performance web server</span></span><span>After=<span>network.target remote-fs.target nss-lookup.target</span></span><span>[Service]</span><span>Type=<span>forking</span></span><span>ExecStart=<span>/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf</span></span><span>ExecReload=<span>/usr/local/nginx/sbin/nginx -s reload</span></span><span>ExecStop=<span>/usr/local/nginx/sbin/nginx -s stop</span></span><span>[Install]</span><span>WantedBy=<span>multi-user.target</span></span>
文件内容解释
[<span>Unit</span>]:服务的说明<span> Description:描述服务</span><span> After:描述服务类别</span><span> [Service]服务运行参数的设置</span><span> Type=forking是后台运行的形式</span><span> ExecStart为服务的具体运行命令</span><span> ExecReload为重启命令</span><span> ExecStop为停止命令</span><span> PrivateTmp=True表示给服务分配独立的临时空间</span><span> 注意:启动、重启、停止命令全部要求使用绝对路径</span><span> [Install]服务安装的相关设置,可设置为多用户</span>
2.保存目录
以754的权限保存在目录:
/usr/lib/systemd/<span>system</span>
3.设置开机自启动
任意目录下执行
systemctl enable nginx<span>.service</span>
4.其他命令
启动nginx服务
systemctl <span><span>start</span> nginx.service</span>
设置开机自启动
systemctl enable nginx<span>.service</span>
停止开机自启动
systemctl disable nginx<span>.service</span>
查看服务当前状态
systemctl status nginx<span>.service</span>
重新启动服务
systemctl restart nginx<span>.service</span>
查看所有已启动的服务
systemctl <span>list</span>-units --<span><span>type</span>=</span>service
以上就介绍了centos 7设置开机启动服务,添加自定义系统服务,包括了centos 7方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
关于CentOS 安装 MongoDB和centos 安装rpm软件包的命令的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于11-【MongoDB 入门教程】MongoDB 原子性和事务、ArgumentException:MongoDB + Unity2D 中的无效关键字 'mongodb+srv://test:test@HOST' - Live MongoDB 未连接、CentOS 7.0 yum install 错误http://vault.centos.org/centos/7/os/Source/repodata/repomd.xml: [Errno 14]、CentOS 7设置开机启动服务,添加自定义系统服务 centos 7 关闭防火墙 centos 7.2 centos 7 64位下载等相关内容,可以在本站寻找。
本文标签: