Mysql basic commands

Tags

, ,

mysql> show variables like ‘port’;
+—————+——-+
| Variable_name | Value |
+—————+——-+
| port | 3306 |
+—————+——-+
1 row in set (0.00 sec)

mysql> show variables like ‘socket’;
+—————+—————–+
| Variable_name | Value |
+—————+—————–+
| socket | /tmp/mysql.sock |
+—————+—————–+
1 row in set (0.00 sec)

mysql> show variables like ‘hostname’;
+—————+————-+
| Variable_name | Value |
+—————+————-+
| hostname | myhostname |
+—————+————-+
1 row in set (0.00 sec)

How to connect to ClearDB and import your local sql schema.
$ mysql -h us-cdbr-sl-dfw-01.cleardb.net -u abcdefghijkl -D remotedbname -p < C:\Apache24\htdocs\myWebApp\schema.sql

Rails common problems and its solutions

Tags

Problem:
Gem Load Error is: Could not find a JavaScript runtime.

$ rails console
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.15.1/lib/bundler/runtime.rb:85:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
Backtrace for gem load error is:
/usr/local/bundle/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
/usr/local/bundle/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/usr/local/bundle/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>'
/usr/local/bundle/gems/uglifier-3.2.0/lib/uglifier.rb:5:in `require'
/usr/local/bundle/gems/uglifier-3.2.0/lib/uglifier.rb:5:in `<top (required)>'

Solution A:
Edit the Gemfile and add/uncomment the line:
gem ‘therubyracer’, platforms: :ruby
Solution B:
sudo apt-get install nodejs

Docker: Basic Docker tutorial

Tags

Lets assume that you’ve created or downloaded some docker images. But in this tutorial, we are going to use the ubuntu-xenial image.

$ docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
ubuntu-xenial               latest              1d4b614305f6        23 minutes ago      859.8 MB
friendlyhello               latest              bf391a773695        18 hours ago        194.6 MB
python                      2.7-slim            1c7128a655f6        2 weeks ago         182.8 MB
mysql                       5.5                 c0fb485deb70        5 weeks ago         256 MB
truthadjustr/myng2          latest              f76b7a86e3b1        6 weeks ago         852.4 MB
hello-world                 latest              48b5124b2768        4 months ago        1.84 kB
ibmcom/cloudant-developer   latest              fb798da3ab5d        11 months ago       1.162 GB

Next, lets see if there are running container.

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES

We can see that there are no active running container. Let’s display all containers by running the command docker ps -a

## List all running/stopped containers
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
871e7dfd063c        ubuntu-xenial       "bash"              46 seconds ago      Exited (0) 6 seconds ago                       vigorous_johnson

Yay! there is one existing container available. Let’s use it.
But if ever there is no container found then we need to run the command docker run -it ubuntu-xenial bash first!

Moving forward, lets rename the existing container name.

## Rename from vigorous_johnson to ubuntu_xenial
$ docker rename 871e ubuntu_xenial

So renaming is done let’s verify it. It should be ubuntu_xenial now.

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
871e7dfd063c        ubuntu-xenial       "bash"              46 seconds ago      Exited (0) 6 seconds ago                       ubuntu_xenial

Next, we can see that the container(ubuntu_xenial) was exited then lets start it..

$ docker start ubuntu_xenial
ubuntu_xenial

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
871e7dfd063c        ubuntu-xenial       "bash"              46 seconds ago      Up to 5 seconds                                ubuntu_xenial

So the container ubuntu_xenial is started..lets execute it.

$ docker exec -it ubuntu_xenial bash

TAKE NOTE: if we run the image again it will create another container.

$ docker run -it ubuntu-xenial bash
## After running docker run run there are
## now two containers created from image ubuntu-xenial.
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
6409eda3b5d6        ubuntu-xenial       "bash"              20 minutes ago      Exited (0) 8 minutes ago                       agitated_shannon
871e7dfd063c        ubuntu-xenial       "bash"              22 minutes ago      Up 20 minutes                                  ubuntu_xenial

So, it is recommended to just re-use one container otherwise there is a reason to do it.

Common Docker commands

Tags

docker build -t {IMAGENAME} .

$ docker build -t skillsoftror_mysql . 

docker run –name {CUSTOMNAME} -p 3000:3000 –link {CONTAINER}:{IMAGE} {IMAGE} rails server –port 3000 –binding 0.0.0.0

$ docker run \
       --name railsapp \
       -p 3001:3001 \
       --link mysql57:mysql \
       skillsoftror_mysql \
       rails server --port 3001 --binding 0.0.0.0 

$ docker stop railsapp3310 && docker rm -f railsapp

Running like these is wrong!!
$ docker run \
–name mysql55_2 \
–net=host \
-p 3310:3310 \
-e MYSQL_ROOT_PASSWORD=webdevel \
-d mysql:5.5


How to run mysql (version 5.5/5.7) container

$ docker run \
       --name mysql57 \
       -p 3310:3306 \
       -e MYSQL_ROOT_PASSWORD=webdevel \
       -d mysql:5.7

$ docker run \
       --name mysql55 \
       -p 3310:3306 \
       -e MYSQL_ROOT_PASSWORD=webdevel \
       -d mysql:5.5

$ docker exec -it mysql55 bash

How to run phpmyadmin in Docker
First, we need to run the mysql container like this:

$ docker run \
         --name mysql57 \
         -p 3310:3306 \
         -e MYSQL_ROOT_PASSWORD=webdevel \
         -d mysql:5.7

Once the mysql container is started, we’re going to install the phpmyadmin and link it to him.

$ docker run \
         --name phpmyadmin57 \
         -d \
         --link mysql57:db \
         -p 8080:80 \
         phpmyadmin/phpmyadmin

Open the phpmyadmin in your browser enter your DOCKER MACHINE IP + MYSQL_PORT e.g, http://192.168.59.103:8080. Tadangg! it works!


How to run cloudant-developer

STEP1
$ docker run \
       --privileged \
       --detach \
       --volume cloudant:/srv \
       --name cloudant-developer \
       --publish 8000:80 \
       --hostname cloudant.dev \
       ibmcom/cloudant-developer
STEP2
$ docker start cloudant-developer

Open the cloudant dashboard in your browser enter your DOCKER MACHINE IP + CLOUDANT_PORT e.g, http://192.168.59.103:8000. Tadangg! it works!


How to run mongodb container

$ docker run -d --name mongo -p 27017:27017 mongo:3.2.8

How to run redis container

$ docker run -d --name redis -p 6379:6379 redis

How to run postgres container using bitnami

$ docker run \
         --name pgrorblogv1 \
         -v /c/postgresql:/bitnami \
         bitnami/postgresql:latest

How to run postgres container using official postgre

# Using official postgres
$ docker run \
         --name postgresdb \
         -e POSTGRES_PASSWORD=webdevel \
         -p 5432:5432 \
         -d postgres:latest
# Once the postgres is running, lets try it.
$ docker exec -it postgresdb bash
# default user is postgres instead of root. For more info: psql --help
$ psql -U postgres 
# Try to execute postgres command
postgres=# SELECT version();
 PostgreSQL 9.6.3 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)
postgres=# /q   (to exit from cli or press CTRL+d)
<hr>
<strong>Postgres with persistent data</strong>
$ docker run \
         --name postgresdb \
         -d \
         -p 8432:5432 \
         -e POSTGRES_PASSWORD=webdevel  \
         -v /c/postgresdata:/var/lib/postgresql/data postgres:latest \
         

How to run the pgAdmin
First, install the postgres server if it doesn’t exist yet.

$ docker run \
         --name pg9 \
         -e POSTGRES_USER=bob \
         -e POSTGRES_PASSWORD=webdevel \
         -p 5432:5432 \
         -d postgres:9

Once the postgres is install successfully then lets install the pgAdmin.

Note: This image won’t work 😦

$ docker run \
         --name pgadmin4 \
         --link pg9:postgres \
         -p 5050:5050 \
         -d chorss/docker-pgadmin4

Instead use this image and it works!

$ docker run \
         --name pgadmin4 \
         --link pg9:postgres \
         -p 5050:5050 \
         -d fenglc/pgadmin4

As we can see, we’re going to link our created postgres container(pg9) to the pgAdmin container(pgadmin4).
Use the default administrator account to log in:
user: pgadmin4@pgadmin.org
password: admin
Access the website at http://localhost:5050 or http://docker-host-ip:5050 in your browser.
Next, create new SERVER. Input the following details:
Host: {docker-host-ip}
Username: bob
Password: webdevel
Port: 5432
And then saved. Tadangg! It should works!

Sources:
https://hub.docker.com/r/fenglc/pgadmin4/
https://hub.docker.com/r/chorss/docker-pgadmin4/


How to run Debian container

$ docker run --name debianjessie -it debian:jessie bash

Apache: Bad Request: Your browser sent a request that this server could not understand.

Tags

If you’ve encountered an error that says: Bad Request: Your browser sent a request that this server could not understand.
Size of a request header field exceeds server limit.

Solution:
Edit the httpd.conf, then add the line LimitRequestFieldSize {bytes}.
$ vi /etc/httpd/conf/httpd.conf

ServerRoot “/etc/httpd”
LimitRequestFieldSize 16384


Then restart the server…
$ sudo /etc/init.d/httpd restart

OpenShift: Error 503 – Service Temporarily Unavailable

Tags

When you try to execute the git push command and the deployment status is SUCCESS. However the open shift return an error 503 – Service Temporarily Unavailable

artheman@localmachine /home/mywebappblog (master) $ git push origin master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 341 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: CLIENT_MESSAGE: Stopping Node.js application...
remote: CLIENT_RESULT: Node.js application stopped.
remote: Building git ref 'master', commit 63efa24
remote: CLIENT_RESULT: Node.js modules installed.
remote: Preparing build for deployment
remote: Deployment id is 291b0274
remote: Activating deployment
remote: CLIENT_MESSAGE: Starting Node.js application...
remote: CLIENT_RESULT: Node.js application started.
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://123456789@mywebappblog.rhcloud.com/~/git/mywebappblog.git/
   7783eb5..63efa24  master -> master

When accessing the website https://mywebappblog-myaccount.rhcloud.com I got this error Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

Solution:
In Express.JS, to fix it make sure that your app properly listen to the correct Port number and IP address. Use process.env.OPENSHIFT_NODEJS_PORT and process.env.OPENSHIFT_NODEJS_IP

/**
 * Get port from environment and store in Express.
 */
var port = process.env.NODE_PORT || process.env.OPENSHIFT_NODEJS_PORT || 4010;
port = normalizePort(port);
var ip = process.env.NODE_IP || process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
app.set('port', port);

var server = http.createServer(app);
server.listen(port, ip); 
server.on('error', onError);
server.on('listening', onListening);
...
...

Note: If using the old node.js version use process.env.NODE_IP & process.env.NODE_PORT otherwise use the process.env.OPENSHIFT_NODEJS_PORT & process.env.OPENSHIFT_NODEJS_IP variables.
That’s it.. it should be working now.

OpenShift: Install rhc client tools on Windows

Tags

Step1. Install Ruby
Step2. Install Git
Step3. Install rhc – a Ruby gem

Step1. Install Ruby
* Download Ruby installer for windows at https://rubyinstaller.org/

Step2. Install Git
* Download the Git for windows and install it to your local machine – https://git-for-windows.github.io/

Step3. Install rhc – a Ruby gem

$ gem install rhc
Successfully installed net-ssh-4.1.0
Successfully installed net-scp-1.2.1
Successfully installed net-ssh-gateway-2.0.0
Successfully installed net-ssh-multi-1.2.1
The `minitar` executable is no longer bundled with `minitar`. If you are
expecting this executable, make sure you also install `minitar-cli`.
Successfully installed minitar-0.6.1
Successfully installed powerbar-1.0.18
Successfully installed minitar-cli-0.6.1
'archive-tar-minitar' has been deprecated; just install 'minitar'.
Successfully installed archive-tar-minitar-0.6.1
Successfully installed highline-1.6.21
Successfully installed commander-4.2.1
Successfully installed httpclient-2.6.0.1
Successfully installed open4-1.3.4
===========================================================================
If this is your first time installing the RHC tools, please run 'rhc setup'
===========================================================================
Successfully installed rhc-1.38.7
13 gems installed

Once you installed the rhc gem, you’ll change directory to your root app then execute this command:

$ rhc setup
OpenShift Client Tools (RHC) Setup Wizard
This wizard will help you upload your SSH keys, set your application namespace, and check that other programs like Git are properly installed.
If you have your own OpenShift server, you can specify it now. Just hit enter to use the server for OpenShift Online: openshift.redhat.com.

Enter the server hostname: |openshift.redhat.com|
You can add more servers later using 'rhc server'.
Login to openshift.redhat.com: myemail@gmail.com
Password: **********

OpenShift can create and store a token on disk which allows to you to access the server without using your password. The key is stored in your home directory
and should be kept secret.  You can delete the key at any time by running 'rhc logout'.
Generate a token now? (yes|no) yes
Generating an authorization token for this client ... lasts about 1 month

Saving configuration to C:\Users\ADMIN\.openshift\express.conf ... done

No SSH keys were found. We will generate a pair of keys for you.

    Created: C:/Users/ADMIN/.ssh/id_rsa.pub

Your public SSH key must be uploaded to the OpenShift server to access code.  Upload now? (yes|no) no

You can upload your public SSH key at a later time using the 'rhc sshkey' command

In order to fully interact with OpenShift you will need to install and configure a git client if you have not already done so. Documentation for installing
other tools you will need for OpenShift can be found at https://www.openshift.com/developers/install-the-client-tools

We recommend these free applications:

  * Git for Windows - a basic git command line and GUI client http://msysgit.github.io/
  * TortoiseGit - git client that integrates into the file explorer http://code.google.com/p/tortoisegit/
 Checking common problems

Your private SSH key file should be set as readable only to yourself.  Please run 'chmod 600 C:\Users\ADMIN\.ssh\id_rsa'

Checking for a domain ... sudogem
Checking for applications ... found 1

  nodeexpressjsblogv1 http://nodeexpressjsblogv1-sudogem.rhcloud.com/

  You are using 1 of 3 total gears
  The following gear sizes are available to you: small

Your client tools are now configured.

That’s it!!

References:
https://developers.openshift.com/getting-started/windows.html

Rails: Omniauth-oauth2 gem for Linkedin, Twitter, Facebook is not working

Tags

, ,

If you’re using a Devise gem and the latest Omniauth Oauth2(1.4.0) for authentication chances are you may encounter an errors below:

Facebook:
ERROR — omniauth: (facebook) Authentication failure! invalid_credentials: >OAuth2::Error, : {“error”:{“message”:”Error validating verification code. Please make sure your >redirect_uri is identical to the one you used in the OAuth dialog request”,”type”:”OAuthException”,”code”:100,”fbtrace_id”:”XXXXXXXXXX”}}

LinkedIn:
ERROR — omniauth: (linkedin) Authentication failure! invalid_credentials: >OAuth2::Error, invalid_request: missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired {“error_description”:”missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired”,”error”:”invalid_request”}

Google
ERROR — omniauth: (google_oauth2) Authentication failure! invalid_credentials: >OAuth2::Error, redirect_uri_mismatch: { “error” : “redirect_uri_mismatch” }

Solution:
We need to update the Gemfile and add this line:
gem ‘omniauth-oauth2’, ‘~> 1.3.1’.
Then run the command: bundle update