[Redis] Add 1 more port for Redis

  • Create a new redis .conf file
cp /etc/redis.conf /etc/redis_6479.conf
  • Edit /etc/redis_6479.conf
#modify pidfile
#pidfile /var/run/redis/redis.pid
pidfile /var/run/redis/redis_6479.pid

...
#modify port
#port 6379
port 6479

...
#modify logfile
#logfile /var/log/redis/redis.log
logfile /var/log/redis/redis_6479.log
  • Copy init script
cp /etc/init.d/redis /etc/init.d/redis_6479
  • and edit init script
...
pidfile="/var/run/redis/redis_6479.pid"
REDIS_CONFIG="/etc/redis_6479.conf"
  • Start 1 more redis with port 6479
sudo /etc/init.d/redis_6479 start

# or check status, restart
sudo /etc/init.d/redis_6479 status
sudo /etc/init.d/redis_6479 restart
  • Check port is running of redis:
ps aux |grep redis

grep-redis-port

Use “alias” in Linux

In file “~/.bash_profile“, we can use alias to define keyword shorter to run app faster 🙂

Example: In server has been installed redis. To run redis-server we must run command “/opt/redis/latest/bin/redis-server”. We can use alias in “~/.bash_profile“:

alias redis-server="/opt/redis/latest/bin/redis-server"

And with redis-cli we must run command: “/opt/redis/latest/bin/redis-cli”. We can use alias:

alias redis-cli="/opt/redis/latest/bin/redis-cli"

And now, we can type redis-server or redis-cli in terminal instead of long command: “/opt/redis/latest/bin/redis-server” and “/opt/redis/latest/bin/redis-cli”.

 

When we added alias to “~/.bash_profile“, we can reload “~/.bash_profile” by the way:

. ~/.bash_profile

 

Change the Command-Line Prompt Colour in the Ubuntu/Linux Terminal

First, open file bashrc

gedit ~/.bashrc

Find line has text: if [ “$color_prompt” = yes ]; then

Copy context in if clause and paste to else clause

Save, close and reopen terminal. 🙂

Optional information:

Black 0;30 – Dark Gray 1;30 – Blue 0;34 – Light Blue 1;34 – Green 0;32 – Light Green1;32 – Cyan 0;36 – Light Cyan 1;36 – Red 0;31 – Light Red 1;31 – Purple 0;35 – Light Purple 1;35 – Brown 0;33 – Yellow 1;33 – Light Gray 0;37 – White 1;37

 

[Linux] Using the Vim editor

Commands that switch the editor to insert mode

  • a will append: it moves the cursor one position to the right before switching to insert mode
  • i will insert
  • o will insert a blank line under the current cursor position and move the cursor to that line.
  • Esc key switches back to command mode.

Pressing the Esc key switches back to command mode. If you’re not sure what mode you’re in because you use a really old version of vi that doesn’t display an “INSERT” message, type Esc and you’ll be sure to return to command mode. It is possible that the system gives a little alert when you are already in command mode when hitting Esc, by beeping or giving a visual bell (a flash on the screen). This is normal behavior.

Basic operations

These are some popular vi commands:

  • n dd will delete n lines starting from the current cursor position.
  • n dw will delete n words at the right side of the cursor.
  • x will delete the character on which the cursor is positioned
  • :n moves to line n of the file.
  • :w will save (write) the file
  • :q will exit the editor.
  • :q! forces the exit when you want to quit a file containing unsaved changes.
  • :wq will save and exit
  • :w newfile will save the text to newfile.
  • :wq! overrides read-only permission (if you have the permission to override permissions, for instance when you are using the root account.
  • /astring will search the string in the file and position the cursor on the first match below its position.
  • / will perform the same search again, moving the cursor to the next match.
  • :1, $s/word/anotherword/g will replace word with anotherword throughout the file.
  • yy will copy a block of text.
  • n p will paste it n times.
  • :recover will recover a file after an unexpected interruption.

Refer: http://www.tldp.org/LDP/intro-linux/html/sect_06_02.html

[Ruby] Why should we use symbol for key in Hash

Using symbols not only saves time when doing comparisons, but also saves memory, because they are only stored once

1. Symbol and String

– Symbols in Ruby are basically “immutable strings” .. that means that they can not be changed, and it implies that the same symbol when referenced many times throughout your source code, is always stored as the same entity, e.g. has the same object id.

– Strings on the other hand are mutable, they can be changed anytime. This implies that Ruby needs to store each string you mention throughout your source code in it’s separate entity, e.g. if you have a string “name” multiple times mentioned in your source code, Ruby needs to store these all in separate String objects, because they might change later on (that’s the nature of a Ruby string).

2. Use in Hash

– If you use a string as a Hash key, Ruby needs to evaluate the string and look at it’s contents (and compute a hash function on that) and compare the result against the (hashed) values of the keys which are already stored in the Hash.

– If you use a symbol as a Hash key, it’s implicit that it’s immutable, so Ruby can basically just do a comparison of the (hash function of the) object-id against the (hashed) object-ids of keys which are already stored in the Hash. (much faster)

Downside: Each symbol consumes a slot in the Ruby interpreter’s symbol-table, which is never released. Symbols are never garbage-collected. So a corner-case is when you have a large number of symbols (e.g. auto-generated ones). In that case you should evaluate how this affects the size of your Ruby interpreter.

Notes:

If you do string comparisons, Ruby can compare symbols just by their object ids, without having to evaluate them. That’s much faster than comparing strings, which need to be evaluated.

If you access a hash, Ruby always applies a hash-function to compute a “hash-key” from whatever key you use. You can imagine something like an MD5-hash. And then Ruby compares those “hashed keys” against each other.

Refer: http://stackoverflow.com/posts/8189435/revisions

[Docker] Error: Can’t not access internet from container in Ubuntu 14.04

When I use Docker, I create container from image (Ex: Ubuntu:14.04) and ssh to this container by command:

docker run -it <image_id> bash

When I ssh to container, I used command ping google.com 

but I get error:  ping: unknown host google.com

To fix it, I have 2 solutions, but first, you must get DNS of your machine by command

nm-tool 

Result in my machine:

DNS: 192.168.30.29
DNS: 192.168.30.66

=> I’ll use DNS: 192.168.30.29

1. Solution 1

– Go to file sudo nano /etc/docker/daemon.json and add your DNS to this file

{
    "dns": ["192.168.30.29", "8.8.8.8", "8.8.4.4"]
}

Save it and restart docker by command: sudo service docker restart

2. Solution 2

solution 1- Go to file sudo nano /etc/default/docker, uncomment and add your DNS to this row:

DOCKER_OPTS="--dns 192.168.30.29 --dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true"

Save it and restart docker by command: sudo service docker restart

If when restart, you get error:

docker stop/waiting
start: Job failed to start

or error:

stop: Unknown instance: 
start: Job failed to start

– You shoud go to file sudo nano /etc/default/docker, and comment row

DOCKER_OPTS="--dns 192.168.30.29 --dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true"

and try again with solution 1.

==> After you try adding DNS successfully, you can ssh to container and run

ping google.com once again. And this is my result after try fixing with solution 1

root@2beb109bc7b3:~# ping google.com
PING google.com (113.171.239.123) 56(84) bytes of data.
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=1 ttl=56 time=1.06 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=2 ttl=56 time=1.51 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=3 ttl=56 time=2.32 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=4 ttl=56 time=1.19 ms
64 bytes from static.vnpt.vn (113.171.239.123): icmp_seq=5 ttl=56 time=1.55 ms
.....

That’s all. Thank you for reading. 🙂

[Docker] Error Cannot connect to the Docker daemon. Is the docker daemon running on this host?

In Ubuntu, when I install Docker successfully, I try running with command:

docker images #show all images in local

but it has error

Cannot connect to the Docker daemon. Is the docker daemon running on 
this host?

because it doesn’t have permission of root. I can run it by command

sudo docker images

Another way, we can config it to needn’t use sudo

  • Step 1: go to /var/run

    cd /var/run
  • Step 2: Add permission for file docker.sock
    sudo chmod -R 776 docker.sock
    

And now, we can use command docker images for shorter code

 

Error: AbstractController::DoubleRenderError when render in Rails

When work, I’ve had bug when use 2 render error 404 and 422. 

When I go to link: http://localhost:3000/products/cloths.js. Firstly, controller will understand params {name=cloths.js} and will select in database with name=cloths.js and not found any record with that name, then the website will use function render_404.

After that, website will check 422 error because the link has .js and use function render_422. At this time, this error will appear:

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like “redirect_to(…) and return”.)

To fix error, I use this code in function render after

self.response_body = nil

It will

# Clear the previous response body to avoid a DoubleRenderError
# when redirecting or rendering another view

Example for my problem:

In function 404, which render before:

def render_404
   if request.xhr? || request.format.json?
     render json: { meta: { message: "404 not found" } }, status: :not_found, layout: false
   else
     render file: path_to_error_file(404), status: :not_found, layout: false
   end
 end

And in function 422, which render after and has error  AbstractController::DoubleRenderError

def render_422
  self.response_body = nil
  if request.xhr? || request.format.json?
    render json: { meta: { message: "422 Error" } }, status: :422_error, layout: false
  else
   render file: path_to_error_file(422), status: :422_error, layout: false, 
      content_type: 'text/html'
  end
 end

Access remote server with local phpMyAdmin in Ubuntu

1

Default, the login page of phpmyadmin have only username & password. If you want to add some remote server you can follow by some steps:

  1. Find file “/etc/phpmyadmin/config.inc.php”
  2. Find row “/* Advance to next server for rest of config */” and add some configs below “$i++;”

/* Advance to next server for rest of config */
$i++;

/* some additional configs  */
$cfg[‘Servers’][$i][‘host’] = ‘52.69.150.47’;
$cfg[‘Servers’][$i][‘user’] = ‘mery_box’;
$cfg[‘Servers’][$i][‘password’] = ‘TewKszmceNQA’;
$cfg[‘Servers’][$i][‘auth_type’] = ‘config’;

And the result:

1