天天看点

服务器架构之性能扩展-第十章(11)

./mongoexport –d test(数据库) -c cl(集合) -o /tmp/cl.out  //导出记录,导出的记录和查询结果相同

<a href="http://blog.51cto.com/attachment/201209/003538643.png" target="_blank"></a>

./mongoimport –d test –c c1 /tmp/cl.out  //导入记录

<a href="http://blog.51cto.com/attachment/201209/003620412.png" target="_blank"></a>

./mongodump –d test –o /tmp/    //备份数据,如果不指定路径,则文件默认会在bin目录下建立dump目录存放备份,json后缀。

<a href="http://blog.51cto.com/attachment/201209/003635764.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201209/003649775.png" target="_blank"></a>

./mongorestore –d test /tmp/test/  //恢复备份

<a href="http://blog.51cto.com/attachment/201209/003700905.png" target="_blank"></a>

mongodb默认很多用户,只有开启认证才需要指定用户。

./mongod –auth –dbpath=/usr/local/mongodb/data  --logpath=/usr/local/mongodb/dblog &amp;

<a href="http://blog.51cto.com/attachment/201209/003710343.png" target="_blank"></a>

&gt;Use admin

&gt;db.addUser(“root”,”123”);   //添加用户

<a href="http://blog.51cto.com/attachment/201209/003724912.png" target="_blank"></a>

&gt;use test

&gt;db.addUser(“user1”,”123”);

&gt;db.auth(“user1”,”123”);   必须授权才能使用test

<a href="http://blog.51cto.com/attachment/201209/003745323.png" target="_blank"></a>

主从集群的方式是最基本的mongod集群方式,在最新版本中已经不是太推荐了。

测试:我们在同一台计算机上实现mongod的主从

./mongod –master –dbpath=/usr/local/mongodb/data1 –logpath=/usr/local/mongodb/dblog1 –fork –port 20001  //启动主的

<a href="http://blog.51cto.com/attachment/201209/003800433.png" target="_blank"></a>

./mongod –slave –source 127.0.0.1:20001 –dbpath=/usr/local/mongodb/data2 –logpath=/usr/local/mongodb/dblog2 –fork –port 20002   //从服务器启动

<a href="http://blog.51cto.com/attachment/201209/003817615.png" target="_blank"></a>

主服务器做增删,从服务器会自动跟进,从服务器插入不了数据,因为不是主,如增删可以用20001端口,查询可以使用20002端口。

<a href="http://blog.51cto.com/attachment/201209/003830770.png" target="_blank"></a>

mongodb在1.6以上的版本中,使用了自动切换和自动修复的副本集功能。他的主节点不固定,是更灵活的集群。

测试:同一台计算机上实现副本集

Mkdir –p /usr/local/mongodb/data1

Touch /usr/local/mongodb/log/dblog1

Touch /usr/local/mongodb/log/dblog2

创建key,用于私钥路径。

Mkdir /usr/local/mongodb/key

Ech “123456” &gt; key1

Ech “123456” &gt; key2

Chmod 600 /usr/local/mongodb/key/*

./mongod –replSet rs1 –keyFile /data/key/key1 –port 20001 –dbpath=/usr/local/mongodb/data1 –logpath=/usr/local/mongodb/log/dblog1 –fork //启动副本集

<a href="http://blog.51cto.com/attachment/201209/003843338.png" target="_blank"></a>

启动不了,就要看日志文件,发现密码短了

<a href="http://blog.51cto.com/attachment/201209/003858270.png" target="_blank"></a>

./mongo –port 20001 //启动字符界面

增加config_rs1规则然后,rs.initiate(config_rs1);初始化

<a href="http://blog.51cto.com/attachment/201209/003930968.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201209/004626961.png" target="_blank"></a>

根据priority进行分配主从,优先级越高,则为主,这时打开另一个终端,会发现一个为primary另个为secondary,并且从数据自动。当然从服务器需要执行“rs.slaveOK();”开启从数据库。

<a href="http://blog.51cto.com/attachment/201209/004716458.png" target="_blank"></a>

Rs.status();  //查看主从服务器状态,可以看到主从和当前主机状态

<a href="http://blog.51cto.com/attachment/201209/004641791.png" target="_blank"></a>

副本集集群方式之所以比主从改进的地方就是自动切换的功能。如副本集中有一个数据出现问题,都可以自动实现功能切换。

Cd zxf mongo-1.2.6.tgz

Cd mongo-1.2.6

/usr/local/php5/bin/phpize  //加载模块

./configure –with-php-config=/usr/local/php5/bin/php-config –enable-mongo

Make &amp;&amp; make install

Vi /etc/php.ini

Extension=mongo.so  //增加mongo扩展

/usr/local/apache/bin/apachectl restart

<a href="http://blog.51cto.com/attachment/201209/004734887.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201209/004748484.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201209/004803705.png" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201209/004822403.png" target="_blank"></a>

&lt;?php

$conn=new Mongo("mongodb://user1:123@localhost:27017/test")

$db=$conn-&gt;test; &gt;

本文转自zsaisai 51CTO博客,原文链接:http://blog.51cto.com/3402313/989253