• Ubuntu上安装 MongoDB,并与php 的链接测试

    MongoDB是一个可扩展、高性能的下一代数据库。MongoDB中的数据以文档形式存储,这样就能在单个数据对象中表示复杂的关系。文档可能由 以下几 部分组成:独立的基本类型属性、“内嵌文档”或文档数组。

    这样的灵活性让开发者能以一种易于管理且灵活的方式来对大量的问题进行建模,不必将数据打散到不同的数据表中。在数据不宜被构造成单独文档的情况 下,MongoDB有“DBRef”的概念,这是从文档的一个属性指向另一个文档的指针。

    从MongoDB数据库中获取和查询数据是十分灵活的——可以基于主文档、文档中的任意属性、任意内嵌文档、数组中的任意文档来动态地查询文档。可 以通过 “点”符号来访问内嵌文档。

    由于Ubuntu 10.04 已经带了MongoDB 1.2.2的源,因此可以直接使用apt-get安装:

    sudo apt-get install mongodb

    在安装MongoDB的时候,系统会安装MongoDB的很多依赖包,安装完成之后,运行MongoDB,会提示:

    mongod: error while loading shared libraries: libmozjs.so: cannot open shared object file: No such file or directory

    解决方法是:

    sudo apt-get install xulrunner-dev

    找到libmozjs.so文件:

    find /usr/lib | grep libmozjs

    可以看到:

    /usr/lib/xulrunner-devel-1.9.2.8/sdk/lib/libmozjs.so

    /usr/lib/xulrunner-1.9.2.8/libmozjs.so

    /usr/lib/firefox-3.6.8/libmozjs.so

    给库做个链接:

    sudo ln -s /usr/lib/xulrunner-1.9.2.8/libmozjs.so /usr/lib

    重启mongodb的服务:

    sudo service mongodb start

    可以确认一下服务是否启动:

    ps aux | grep mongod

    pymongo安装

    由于pymongo采用的是easy_install的模式进行安装的,所以需要先安装setuptools:

    sudo apt-get install python-setuptools

    安装好之后,可以执行安装:

    sudo easy_install pymongo

    注意事项:

    只要 sudo apt-get install mongodb 应该就会装好。

    但是安装得朋友一定都会发现,装起来无法启动无法使用。

    在查找了一些资料后发现:

    这主要的问题在于少了 /usr/lib/libmozjs.so 这个软件库的文件。

    只要你有装 Firefox 或是 另外安装 xulrunner 就应该会有,不过路径是错得。

    所以就要这样:sudo apt-get install xulrunner-1.9.2

    安装好了以后,建立连结。

    ln -s /usr/lib/xulrunner-1.9.2.6/libmozjs.so /usr/lib/libmozjs.so

    然后重新启动 mongodb 试试看,应该就会看到 mongodb 正确启动。

    安装php连接mongo的驱动


    可以到这个链接下载驱动http://github.com/mongodb/mongo-php-driver/downloads

    注意php的版本匹配问题,否则启动apache报错。

    mongoDB 服务端的默认连接端口是 27017

    <?php
    //多个服务器
    $conn = new Mongo(“mongodb://localhost:27017”);
    //选择数据库blog
    $db = $conn->blog;
    //制定结果集(表名:users)
    $collection = $db->users;
    //列出数据库列表
    $dbs = $conn->listDBs();
    echo “<pre>”;
    print_r($dbs);
    echo “</pre>”;
    //新增
    $user = array(‘name’ => ‘caleng’, ’email’ => ‘admin@admin.com’);
    $collection->insert($user);
    //修改
    $newdata = array(‘$set’ => array(“email” => “test@test.com”));
    $collection->update(array(“name” => “caleng”), $newdata);
    //查找
    $cursor = $collection->find();
    echo “<pre>”;
    print_r($cursor);
    echo “</pre>”;
    //查找一条
    $user = $collection->findOne(array(‘name’ => ‘caleng’), array(’email’));
    echo “<pre>”;
    print_r($user);
    echo “</pre>”;
    //删除
    $collection->remove(array(‘name’=>’caleng’), array(“justOne” => true));
    ?>
  • 一个PHP读取MSN用户信息的类

    <?php
    $msn = new myMSN(“adminer@phper.cc“, “123”);
    // MSNv9
    class myMSN {
    private $server = “messenger.hotmail.com”;
    private $port = 1863;
    private $nexus = “https://nexus.passport.com/rdr/pprdr.asp“;
    private $sshLogin = “login.live.com/login2.srf”; //loginnet.passport.com/login2.srf
    private $getCode = null;
    private $_ip = null;
    private $_port = null;
    private $connect = null;
    private $trID = 1;
    private $maxMessage = 4096;
    private $userName = null;
    private $passWord = null;
    private $debug = true;

    function myMSN($userName=””, $passWord=””) {
    if (!empty($userName) && !empty($passWord)) {
    $this->userName = $userName;
    //$this->passWord = urlencode($passWord);
    $this->passWord = $passWord;
    $this->startTalk();
    }
    }
    function put($data) {
    if ($this->isConnect()) {
    fputs($this->connect, $data);
    $this->trID++;
    if ($this->debug)
    print(“<div style=’color:green;font-size:13px;’>>>>{$data}</div>”);
    }
    }
    function get() {
    if ($data = @fgets($this->connect, $this->maxMessage)) {
    if ($this->debug)
    print(“<div style=’color:red;font-size:13px;’><<<{$data}</div>”);
    return $data;
    } else {
    return false;
    }
    }
    function isConnect() {
    if (!is_null($this->connect))
    return true;
    else
    return false;
    }
    function close() {
    @fclose($this->connect);
    }
    function startTalk() {
    if ($this->connect = fsockopen($this->server, $this->port, $errno, $errstr, 2))
    $this->verTalk();
    }
    function verTalk() // MSN 协议协商 {
    $this->put(“VER {$this->trID} MSNP9 CVR0 rn”);
    $data = $this->get();
    //echo $data;
    if (false !== strripos($data, “VER”))
    $this->envTalk();
    }
    function envTalk() // 环境协商 {
    $this->put(“CVR {$this->trID} 0x0409 winnt 5.0 i386 MSNMSGR 7.0.0816 MSMSGS {$this->userName} rn”);
    $data = $this->get();
    //echo $data;
    if (false !== strripos($data, “CVR”))
    $this->reqTalk();
    }
    function reqTalk() // 请求确认 {
    $this->put(“USR {$this->trID} TWN I {$this->userName} rn”);
    $data = $this->get(); // XFR 3 NS 207.46.107.41:1863 0 65.54.239.210:1863 XFR 3 NS 207.46.107.25:1863 U D
    //echo $data;
    if (false !== strripos($data, “XFR”)) {
    list(, , , $serv) = explode(” “, $data); // 分析服务器
    list($ip, $port) = explode(“:”, $serv); // 分析IP和端口
    $this->_ip = $ip;
    $this->_port = $port;
    $this->reLink($ip, $port);
    } else {
    //echo $data; // USR 3 TWN S ct=1205292058,rver=5.0.3270.0,wp=FS_40SEC_0_COMPACT,lc=1033,id=507,ru=http:%2F%2Fmessenger.msn.com,tw=0,kpp=1,kv=4,ver=2.1.6000.1,rn=1lgjBfIL,tpf=b0735e3a873dfb5e75054465196398e0
    list(, , , , $this->getCode) = explode(” “, trim($data));
    //echo $data;
    if (empty($this->sshLogin))
    $this->reLoginTalk(); // 重新获取登陆服务器地址
    else
    $this->getLoginCode($this->sshLogin);
    }
    }
    function reLink($server, $port) // 重置连接 {
    $this->connect = null;
    $this->server = $server;
    $this->port = $port;
    $this->trID = 1;
    $this->startTalk();
    }
    function reLoginTalk() // 重新获取服务器地址 {
    $ch = curl_init($this->nexus);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $header = curl_exec($ch);
    //print_r($header);
    curl_close($ch);
    preg_match (‘/DALogin=(.*?),/’, $header, $out); // 捕捉服务器登陆匹配
    //print_r($out);
    if (isset($out[1])) {
    $this->getLoginCode($out[1]);
    }
    else {
    //return false;
    exit(“无法捕捉到登陆服务器的URL”);
    }
    }
    function getLoginCode($slogin) // 获取登陆代码 {
    //echo($this->getCode);
    if (!is_null($this->getCode)) {
    $ch = curl_init(“https://” . $slogin);
    $loginInfo = array(
    “Authorization: Passport1.4 rgVerb=GET,OrgURL=http%3A%2F%2Fmessenger%2Emsn%2Ecom,sign-in=” . $this->userName . “,pwd=” . $this->passWord . “,” . $this->getCode,
    “Host: login.passport.com”
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $loginInfo);
    //print_r($loginInfo);
    //$this->getCode = null;
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $header = curl_exec($ch);
    //print_r($header);
    preg_match (“/from-PP='(.*?)’/”, $header, $out);
    //print_r($out);
    if (isset($out[1])) {
    $this->loginAction($out[1]);
    } else {
    //return false;
    exit(“无法捕捉到登陆代码的信息”);
    }
    } else {
    return false;
    }
    }
    function loginAction($loginCode) // 登陆工作 {
    $this->put(“USR {$this->trID} TWN S {$loginCode} rn”); // USR |trID| SSO S |t=code|
    $data = $this->get();
    //echo $data;
    //print_r($data);
    //$this->put(“SYN {$this->trID} 0 rn”);
    //$this->put(“CHG {$this->trID} NLN rn”);
    //print_r($this->get());
    }
    }
    ?>