jenssegers/laravel-mongodb Basic Usage on cloud9 with php7 ①

updated 2018/1/5

Installation

monteecristoo.hatenablog.com

monteecristoo.hatenablog.com

 

Laravel Configuration

@config\app.php

add the service provider

Jenssegers\Mongodb\MongodbServiceProvider::class,

@config/database.php

'mongodb' => [

            'driver'   => 'mongodb',

            'host'     => env('DB_HOST', 'localhost'),

            'port'     => env(27017),

            'database' => env('DB_DATABASE'),

           //  'username' => env('DB_USERNAME'),

           // 'password' => env('DB_PASSWORD'),

            'options'  => [

                'database' => 'admin' // sets the authentication database required by mongo 3

            ] 

        ],

参考:MongoDB \ Driver \ Exception \ AuthenticationException (11) Authentication failed.

github.com

 

Making Laravel Eloquent Model 

php artisan make:model testMongo

@app\testMongo.php

  namespace App;

  use Illuminate/Database/Eloquent/Model;

  use Moloquent;

 

  class testMongo extends Moloquent

  {

    protected $connection = 'mongodb',  // if use multiple dbs

    protected $collection = 'testMongo'  // define the collection

  }

参考:

github.com

 

Making Laravel Migration file

php artisan make:migration create_testMongo_table

or

php artisan make:model testMongo -m

 

@database/migrations/timestamp_create_testMongo_table.php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

 

class CreateTestMongoTable extends Migration

{

  public function up()

  {

    Schema::connection('mongodb')->create('testMongo', function(Blueprint table) {

      $table->increments('id');

      $table->timestamps();

      $table->text('body');

    )};

  }

 

  public function down()

  {

    Schema::connection('mongodb')->drop('testMongo');

  }

}

 * using multiple databases

* DON'T use dropIfExists

参考:

github.com

 

./mongod mongoDBを起動

mongo mongoシェルを起動

env('DB_DATABASE') は c9 なので

mongo shell 上で

> use c9

> db.testMongo.insertOne(

  {

    body: "test mongo operation",

    id: 1

  }

)

参考:

MongoDB CRUD Operations — MongoDB Manual 3.6

 

Laravel tinker で確認

php artisan tinker 

>> $test  = App\testMongo::get()

 

 

PHP のシステムファイルが破損した場合 Unable to load dynamic library

composer update or composer require xxx

phpunit/phpunit 6.1.4 requires ext-dom * -> the requested PHP extension dom is missing from your system.

→ sudo apt-get install php7.1-xml 

 

composer update or composer require xxx

  error- Unable to load dynamic library ....

と表示され、phpinfo から extension_dir を確認し、該当ディレクトリにエラーが表示されているモジュールの xxx.so があるにも関わらずエラーが発生する場合。

 

Safe solution is to reinstall all php7 related packages.

You can do it by simply entering this command to console:

sudo apt-get install --reinstall `dpkg -l | grep 'ii  php7' | awk '{ printf($2" "); next}'`

This solution will keep your configuration unchanged

 

参考:

stackoverflow.com

 

php.ini を新しく作成するか聞かれるが keep current verion に。

 

cloud9 の場合、

Apache & PHP タブを RUN / STOP  で

error- Unable to load dynamic library ....

エラーを確認できる。

jenssegers/laravel-mongodb installation PHP7.1 on cloud9

updated: 2018/9/25

jenssegers/laravel-mongodb installation PHP7.1 on cloud9

updated: 2017/12/14

下記に従ってmongoDBをインストール。

monteecristoo.hatenablog.com

  • composer require jenssegers/mongodb
→ error:
  Problem 1
    - jenssegers/mongodb v3.0.0 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1, 1.0.2].
    - jenssegers/mongodb v3.0.1 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1, 1.0.2].
    - jenssegers/mongodb v3.0.2 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1, 1.0.2].
    - mongodb/mongodb 1.0.2 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.1 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.0 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - Installation request for jenssegers/mongodb ^3.0 -> satisfiable by jenssegers/mongodb[v3.0.0, v3.0.1, v3.0.2].

  To enable extensions, verify that they are enabled in those .ini files:
    - /etc/php5/cli/php.ini
    - /etc/php5/cli/conf.d/05-opcache.ini
    - /etc/php5/cli/conf.d/10-mysqlnd.ini
    - /etc/php5/cli/conf.d/10-pdo.ini
    - /etc/php5/cli/conf.d/20-curl.ini
    - /etc/php5/cli/conf.d/20-gd.ini
    - /etc/php5/cli/conf.d/20-json.ini
    - /etc/php5/cli/conf.d/20-mcrypt.ini
    - /etc/php5/cli/conf.d/20-mongo.ini
    - /etc/php5/cli/conf.d/20-mysql.ini
    - /etc/php5/cli/conf.d/20-mysqli.ini
    - /etc/php5/cli/conf.d/20-pdo_mysql.ini
    - /etc/php5/cli/conf.d/20-pdo_pgsql.ini
    - /etc/php5/cli/conf.d/20-pgsql.ini
    - /etc/php5/cli/conf.d/20-readline.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

sudo pecl mongodb 

→ Parse error: syntax error, unexpected 'new' (T_NEW) in /usr/share/php/PEAR/Frontend.php on line 91

 

sudo apt-get install php7.1-xml

→ sudo pecl mongodb 

→ Package mongodb Version 1.3.4 does not have REST dependency information available install failed

 

Solution: 

mongoDB をアンインストールして先に sudo pecl mongodb で mongoDB PHP ドライバーを先にインストールしてから mongoDB をインストールする。

ここでmongoDBインストール時、dependencies の conflict が起こったら sudo apt-get install -y mongodb-org* だけでなく正規のインストール手続きを踏む。

 

  • composer require jenssegers/mongodb
→ error:
  Problem 1
    - jenssegers/mongodb v3.0.0 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1, 1.0.2].
    - jenssegers/mongodb v3.0.1 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1, 1.0.2].
    - jenssegers/mongodb v3.0.2 requires mongodb/mongodb ^1.0.0 -> satisfiable by mongodb/mongodb[1.0.0, 1.0.1, 1.0.2].
    - mongodb/mongodb 1.0.2 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.1 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - mongodb/mongodb 1.0.0 requires ext-mongodb ^1.1.0 -> the requested PHP extension mongodb is missing from your system.
    - Installation request for jenssegers/mongodb ^3.0 -> satisfiable by jenssegers/mongodb[v3.0.0, v3.0.1, v3.0.2].

  To enable extensions, verify that they are enabled in those .ini files:
    - /etc/php5/cli/php.ini
    - /etc/php5/cli/conf.d/05-opcache.ini
    - /etc/php5/cli/conf.d/10-mysqlnd.ini
    - /etc/php5/cli/conf.d/10-pdo.ini
    - /etc/php5/cli/conf.d/20-curl.ini
    - /etc/php5/cli/conf.d/20-gd.ini
    - /etc/php5/cli/conf.d/20-json.ini
    - /etc/php5/cli/conf.d/20-mcrypt.ini
    - /etc/php5/cli/conf.d/20-mongo.ini
    - /etc/php5/cli/conf.d/20-mysql.ini
    - /etc/php5/cli/conf.d/20-mysqli.ini
    - /etc/php5/cli/conf.d/20-pdo_mysql.ini
    - /etc/php5/cli/conf.d/20-pdo_pgsql.ini
    - /etc/php5/cli/conf.d/20-pgsql.ini
    - /etc/php5/cli/conf.d/20-readline.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

php -m で php にインストールされているモジュールを確認

おそらく mongodb が抜けている。(ただしmongodコマンドでmongoDBは起動できる) 

この場合、

sudo apt-get install php7.1-mongodb で extension_dir に mongodb.so が作成される。

 

→ composer require jenssegers/mongodb

→ Installed successfully

 

 

 

@workspace/php.ini

以下を加筆

    extensoin=mongodb.so

 

→ composer require jenssegers/mongodb

 

extention の問題関連

参考:

monteecristoo.hatenablog.com

 

f:id:monteecristoo:20171214235649p:plain

follow up:

monteecristoo.hatenablog.com

raspberry pi Basic use

1. Installation

オフィシャルからnobes をDL

 

raspberry pi をインストールする SDカードを SDFormatter でフォーマット ** この時ボリュームラベル名に RASPBERRYPIを設定(しないとError resizing existing FAT partition)

 

DL した nobes の zip を解凍し中身を SDカードに移す ** nobes の親フォルダはいらない中身だけ

 

raspberry pi に SDカードを挿して電源ON → 自動でインストールが始まる

 

2. 初めにやること

インターネットに接続

sudo apt-get update これからインストールするパッケージの最新のリストをGET

 

sudo apt-get upgrade インストール済みのパッケージをアップデート(時間がかかる)

 

3. VNC viewer、teraterm で接続

raspberry pi のデスクトップのラズベリーアイコンから

ログインパスワードの変更

ラズベリーパイの設定>システム

パスワードの変更

ラズベリーパイの設定>>インターフェースを開き

SSHVNC を有効にする

OK=>再起動

クライアント側のデバイスにrealVNCviewer、teraterm をインストール

環境:windows10

 

@teraterm

ホスト: 192.168.100.115

ユーザー名: raspberry pi 起動時のユーザー名

パスフレーズ: 2で設定し直したログインパス

 

realVNCviewer で接続を試みると下記のエラー

Unable to connect to VNC Server using your chosen security setting. Either upgrade VNC Server to a more recent version from RealVNC, or select a weaker level of encryption

クライアントのビュワー側の

file >> properties >> general >> security >> encryption で

prefer on に設定変更で接続が可能になる

mongoDB installation on a cloud9 workspace

updated: 2020/1/24

 

環境:PHP7 Laravel5.4

mongoDB 公式Doc の Install MongoDB Community Edition on Ubuntu に従う。

1

Import the public key used by the package management system.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

 

2

Create a list file for MongoDB.

Ubuntu 14.04

echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

3

Reload local package database.

sudo apt-get update

4

Install the MongoDB packages.

sudo apt-get install -y mongodb-org

 

手順①、②をしないとversion 2.6.12 がインストールされる。

 

 

5

Making mongod command with own configuration to force no journal mode.

$ mkdir data

$ echo 'mongod --bind_ip=$IP --dbpath=data --nojournal "$@"' > mongod

$ chmod a+x mongod

 

/etc/mongod.conf を編集

  journal:
    enabled: false

  ref)

https://docs.mongodb.com/manual/reference/configuration-options/#storage.journal.enabled

 

6

Start mongod

$ ./mongod 

参考:

Install MongoDB Community Edition on Ubuntu — MongoDB Manual 3.4

community.c9.io

* mongoDB を apt-get install した時点で下記のエラーが発生する。

dpkg: error processing package mongodb-org (--configure):
dependency problems - leaving unconfigured
Processing triggers for ureadahead (0.100.0-16) ...
Errors were encountered while processing:
mongodb-org-server
mongodb-org
E: Sub-process /usr/bin/dpkg returned an error code (1)

touch /etc/init.d/mongod

 上記で解決。

参考:

stackoverflow.com

 

 

* この手順だと sudo service mongod start で service mongod unrecognized というエラーが発生する 

mongoDB の起動: mongod

mongoDB の停止: ctl + C

mongo shell の入り方: DB 起動後、mongo

 

 default の状態で configuration をいじっていないと mongod で起動後、root ディレクトリ/data/db にジャーナルファイル300MB程でストレージを埋める。

 root ディレクトリで

$  cd /data

$ du -h で確認

$ rm -rv db で生成されたジャーナルを削除

$ mkdir db で再度 mongod が可能な状態になる。

Laravel & redis 接続テスト

Installation

composer require predis/predis

 

@web.php

Route::get('/redis_test', function()

{

  print_r(app()->make('redis'));

}

 

// Illuminate\Redis\RedisManager Object ( [driver:protected] => predis [config:protected] => Array ( [default] => Array ( [host] => 127.0.0.1 [password] => [port] => 6379 [database] => 0 ) ) [connections:protected] => )