Tuesday, May 26, 2015

Ubuntu 14.04 - Mysql Galera Cluster for Wordpress

This cluster has 2 nodes run in multi master mode

Installing cluster
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://mirror3.layerjet.com/mariadb/repo/5.5/ubuntu trusty main'
sudo apt-get update -y ; sudo apt-get install -y galera mariadb-galera-server rsync

Configuring cluster
On each of the host in the cluster add this configuration
vi /etc/mysql/conf.d/galera.cnf
[mysqld]
#mysql settings
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_size=0
query_cache_type=0
bind-address=0.0.0.0
#galera settings
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_name="my_wsrep_cluster"
wsrep_cluster_address="gcomm://192.20.3.31,192.20.3.32"
wsrep_sst_method=rsync

Stop MariaDB instance in all of the Galera hosts
sudo service mysql stop

Init the Galera cluster on my 1st node by start MariaDB
sudo service mysql start --wsrep-new-cluster

Just start MariaDB on my 2nd node
sudo service mysql start

To prevent startup error on 2nd node we need to copy /etc/mysql/debian.cnf content from node1 to node2 and then restart MariaDB on 2nd node

Confirm cluster status has started
mysql -u root -p -e 'SELECT VARIABLE_VALUE as "cluster size" FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE VARIABLE_NAME="wsrep_cluster_size"'
Enter password: 
+--------------+
| cluster size |
+--------------+
| 2            |
+--------------+
From now on the data will sync between both hosts. To test it try create one database from 1st node then go to 2nd node and see it is there.
On 1st node
mysql -u root -p
create database HelloWorld;

On 2nd node
mysql -u root -p
show databases;
if Helloworld database was there. That's kool. We're ready to go.

Grant Remote privilege to remote user on DBcluster
On one of the DB host (node1 or node2)
mysql -u root -p
create user 'handsome'@'%' identified by handsome_password;
grant all privileges on Database_name . * to  'handsome'@'%';
flush privileges;
show grants for 'handsome'@'%';

Note: 
Replace handsome with your username or root
Replace Database_name with your DB name or * to grant privileges on all Database

DNS setting
On my premise DNS server I add a Round Robin DNS record point to 2 Galera host
dbcluster -> 192.20.3.31
dbcluster -> 192.20.3.32

On my PHP app server I can connect to the dbcluster with this command
mysql -u root -p -h dbcluster 

If it not work, you could have a look on MariaDB log

Reference link:
https://www.linode.com/docs/databases/mariadb/clustering-with-mariadb-and-galera

Thursday, May 21, 2015

Tại sao đuổi học?

Ngay từ thuở còn cắp sách đến trường tôi chứng kiến không biết bao nhiêu bạn bè của mình bị đuổi học vì những lý do như đánh nhau, quay cóp, uống bia rượu, đua xe... nói chung là những lý do mà người ta quy cho là phạm tội hay lỗi trầm trọng mà qua đó sẽ dẫn đến việc cho thôi học có thời hạn hay vô thời hạn. Mà việc đuổi học này không phải chỉ riêng ở Việt Nam mà nước Tây nước Tàu nào cũng có. Tôi còn chứng kiến trước khi cho thôi học nhà trường còn bắt cả phụ huynh lên làm bản kiểm điểm đọc trước toàn trường, thật không thể nào tưởng tượng nổi.

Người ta có rất nhiều lý do để giải thích cho việc đuổi học. Nhưng quan điểm của tôi là dựa trên chức năng của nhà trường là giáo dục học sinh, trường không dạy nổi, không quản lý nổi có nghĩa là không thực hiện nổi chức năng của mình như vậy người ra quyết định đuổi học nên thôi giữ chức vụ hay đóng cửa trường vì không có khả năng dạy dỗ nổi học sinh do mình nhận vào.

Những lý do như làm ảnh hưởng đến học sinh khác là hoàn toàn không chấp nhận, nếu sợ ảnh hưởng đến học sinh khác thì tại sao nhận học sinh vào học, nhận học sinh vào học nghĩa là anh phải chịu trách nhiệm quản lý dạy dỗ và giáo dục người ta, nếu lỡ người ta vi phạm anh phải xem đó là lỗi của mình và có cách khắc phục hậu quả không đằng này lại đuổi học như vậy là từ chối trách nhiệm của mình. Nếu nói là làm ảnh hưởng đến học sinh khác thì chẳng khác nào coi người bị đuổi như một phần tử bệnh dịch nguy hiểm cần được loại trừ? Bạn phạm một lỗi và bạn trở thành một loại dịch bệnh nguy hiểm.
Có lí do đuổi học nào là muốn tốt cho học sinh bị đuổi học không? Tôi thấy đa phần là do dự luận và do ảnh hưởng đến tên tuổi của trường hay của ông/bà nào đó cho nên phải đuổi? Choáng!
Nhà trường có thể có cả tỷ lý do để chứng tỏ quyền uy đuổi học của mình nhưng nếu giáo dục thật sự xuất phát từ tình thương thì không có lý do nào có thể chấp nhận được và nó cũng đã sai chức năng và mục đích giáo dục. Nhưng việc đuổi học vẫn cứ xảy ra, nhiều người vẫn cứ hả hê ... thế thì người bị đuổi học họ sẽ cảm thấy thế nào? Họ sẽ trở thành người như thế nào? Những người khác sẽ cư xử với họ ra sao? Liệu có nơi nào để họ có thể tiếp tục được học hay ko? Học sao mà xa xỉ quá vậy.

Tôi chứng kiến bạn bè của mình thuở học chung học rất giỏi nhưng họ phạm sai lầm và bị đuổi học ở lứa tuổi thành niên và thế là sau 10-15 năm gặp lại tôi nhìn thấy họ mà rưng nước mắt, họ cũng mặc cảm ko thèm nói chuyện với mình. Có ai không phạm sai lầm, thế sao họ lại bị vứt ra khỏi xã hội như vậy, trẻ con hay người lớn ai cũng có thiện tâm trong lòng mình ai cũng có phút lầm đường lạc lối và hoàn toàn có thể quay trở lại con đường tốt của mình. Thế mà nền giáo dục này, xã hội này lại vứt bỏ họ và chà đạp họ.

Nếu người bị đuổi học là con cái của mình thì mình sẽ thế nào? Vì nó làm mất mặt cả dòng họ nên đuổi nó ra khỏi nhà chăng làm như vậy gia đình cũng chẳng khác gì nhà trường cũng phủi bỏ trách nhiệm giáo dục của mình chỉ khác ở chỗ là lý do, xây thì khó phá bỏ thì dễ lắm. Trường với gia đình đổ lỗi cho nhau rồi đổ lên đầu đứa nhỏ. Nơi nương tựa của người Việt mình là gia đình thế nên phải tìm cách giúp con em mình vượt qua được lỗi lầm, không đi học nghĩa là một cơ hội tốt vì có thêm thời gian để rèn luyện chấn chỉnh bản thân của mình thành một con người tốt bằng một cách thức khác chứ không phải cứ đi tới trường là con đường duy nhất. Trường đời rộng và dài lắm ai ơi, mỗi người lại mỗi ngã đâu như trường Việt thì học 16 năm trời cũng chữ thầy trả thầy chưa chắc đã bằng một người chuyên tâm học hành say mê làm việc yêu thích trong vòng vài năm là cũng đạt được thành tựu. Những người tu hành hay khổ luyện họ cũng có chỗ của họ đâu đó chứ đâu nhất thiết phải ở trường.
Vì vậy đừng đuổi học nữa! Trường học đừng chứng tỏ sự yếu kém và bất lực của mình nữa.

Ubuntu 14.04 - GlusterFS for Wordpress

Here's my setup:  Client -> Varnish cache -> 2 x Worpdress -> Memcached -> 2 x MariaDB

I need to sync Wordpress folder on both Machine, just have a look on NFS, CEPH, GlusterFS and I go with GlusterFS
sudo apt-get -y install glusterfs-server

If you don't have a on premise DNS server, we can add the server name in to hosts files
192.20.1.1    srvr-phpnode1.mydomain.vn    srvr-phpnode1    
192.20.1.2    srvr-phpnode2.mydomain.vn    srvr-phpnode2

Create a directory for Gluster Volume on both hosts
mkdir /gluster-volume

Check for other peer status
sudo gluster peer probe srvr-phpnode2
peer probe: success

sudo gluster peer status
Number of Peers: 1

Hostname: srvr-phpnode2
Port: 24007
Uuid: 225698cb-a84f-4b5b-8537-27732d752aba
State: Peer in Cluster (Connected)

Glusterfs distributed vs Replica mode (GlusterFS docs):
Distributed mean data distributes across the available bricks in a volume: write 100 files, on average, 50 on one server, and 50 on another. This is faster than a “replicated” volume, but isn’t as popular since it doesn’t give you two of the most sought after features of Gluster — multiple copies of the data, and automatic failover if something goes wrong

Create wwwVol volume on host srvr-phpnode1
sudo gluster volume create wwwVol replica 2 transport tcp srvr-phpnode1:/gluster-volume srvr-phpnode2:/gluster-volume force

(GlusterFS docs): we tell it to make the volume a replica volume, and to keep a copy of the data on at least 2 bricks at any given time. Since we only have two bricks total, this means each server will house a copy of the data. Lastly, we specify which nodes to use, and which bricks on those nodes. The order here is important when you have more bricks

sudo gluster volume start wwwVol
sudo gluster volume info

Volume Name: wwwVol
Type: Replicate
Volume ID: 8c1430d4-8b25-4967-9a46-39287cdedbb2
Status: Started
Number of Bricks: 1 x 2 = 2
Transport-type: tcp
Bricks:
Brick1: srvr-phpnode1:/gluster-volume
Brick2: srvr-phpnode2:/gluster-volume

We need /var/www will be on GlusterFS so we will mount wwwVol to /var/www on both hosts
On both hosts
mkdir /var/www

On phpnode1
vi /etc/fstab
srvr-phpnode1:/wwwVol /var/www glusterfs defaults,_netdev 0 0

mount -a 

On phpnode2
vi /etc/fstab
srvr-phpnode2:/wwwVol /var/www glusterfs defaults,_netdev 0 0
mount -a 

Other GlusterFS command
gluster volume stop VOLNAME
gluster volume delete VOLNAME

gluster volume remove-brick VOLNAME wnode1:/www
http://www.gluster.org/community/documentation/index.php/Gluster_3.1:_Deleting_Volumes
http://www.gluster.org/community/documentation/index.php/Basic_Gluster_Troubleshooting

Ubuntu 14.04 - Memcached server for Wordpress

First thing first: memcache vs memcached

According to http://blackbe.lt/php-memcache-vs-memcached/
PHP has two separate module implementations wrapping the memcached (as in memcache daemon) server.

The memcache module utilizes this daemon directly, whereas the memcached module wraps the libMemcached client library and contains some added bonuses.
Memcached module will be faster than memcache module => I'll go with Memcached module

Install Memcached daemon on my dedicate Memcached host

sudo apt-get install memcached libmemcached-tools

My memcache server listen all of its address. So edit /etc/memcached.conf
-m 20000      #amount of RAM in MB
-p 11211      #listen port 11211
-l 0.0.0.0    #listen on all interfaces

Install memcached client
In other Wordpress PHP server we need to install memcahed client and server (optional):
sudo apt-get install php5-memcached memcached libmemcached-tools php-pear
sudo mkdir /etc/php5/conf.d
ln -s /etc/php5/mods-available/memcached.ini /etc/php5/conf.d/memcached.ini

I need to store Php sessions in Memcached because store in memory will be faster than disk 
Default is session.save_handler = files
Add to /etc/php5/conf.d/memcached.ini
[Session]
session.save_handler = memcached
session.save_path = "tcp://localhost:11211"

Wordpress and Memcached
Finally edit Wordpress wp-config file to add list of Memcached server for Wordpress Total cache plugin
global $memcached_servers;
$memcached_servers = array('default' => array('192.1.3.30:11211','192.1.1.11:11211','192.1.1.12:11211'));

Now your Wordpress can Memcached it.