Monday 20 August 2012

HTML + Javascript

Vertical - content
http://baijs.nl/tinycarousel/
http://dev.css-zibaldone.com/onwebdev/post/jquery_vertical_content_slider.html

Thursday 16 August 2012

Replicate one database mysql in ubuntu

This tutorial will go through the setup of MySQL database replication. I will also talk about how to get everything working smoothly again after a server crash, or if you wish to switch databases. I will try to explain what is going on behind the scenes for every step (something I've found missing from other tutorials). This is written specifically for MySQL 5.0 on Centos 4, but should be very similar on other Linux distributions. It should also work this way with MySQL 4.x.

The theory

We have 2 servers, one of which is a Master and the other which is a Slave. We tell the Master that it should keep a log of every action performed on it. We tell the slave server that it should look at this log on the Master and whenever something new happens, it should do the same thing.
You should follow the instructions below with two console windows open - one for the Master and one for the Slave. Also note that I will capitalise the first letters of Master and Slave to indicate I am talking about the servers.

Configuring the Master

First of all, we need to create a user on the Master server that the Slave will connect as. I call mine 'slave_user'. Log into mysql as root and create the user:
mysql -u root -p (log into MySQL)
CODE:
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
Now, we should edit the my.cnf file (usually in /etc/my.cnf), in the [mysqld] section and tell MySQL that it's going to be a Master:
CODE:
log-bin = /home/mysql/logs/mysql-bin.log
binlog-do-db=my_database
server-id=1
The first line tells MySQL to start writing a log, and tells it where to write the log. Make sure this directory is empty of all replication logs, especially if you're starting again after replication has already been used.
The second line chooses the database to write the log for. You should change this to your database. The third line gives the server an ID (to distinguish it from the Slave).
You should also make sure skip-networking has not been enabled.
You should now restart the Master:
CODE:
/etc/rc.d/init.d/mysqld restart
(MySQL restart commands may vary)

Configuring the Slave

Again, we should change the /etc/my.cnf of the Slave server, in the [mysqld] section:
CODE:
server-id=2
master-host=128.0.0.1
master-connect-retry=60
master-user=slave_user
master-password=slave_password
replicate-do-db=my_database relay-log = /var/lib/mysql/slave-relay.log
relay-log-index = /var/lib/mysql/slave-relay-log.index
Line 1 gives the Slave its unique ID. Line 2, tells the Slave the I.P address of the Master server - so you need to change the I.P here.
The remaining lines set a retry limit, and tell the Slave the user, password and database it needs to replicate. We also tell the slave what to use as its relay log. It's best to set this directly, or MySQL will create the name from the hostname and should you change hostname, replication will fail.
You should also make sure skip-networking has not been enabled.
You should now restart the Slave:
CODE:
/etc/rc.d/init.d/mysqld restart

Getting the data onto the Slave

On the Master...

I'm assuming you have a live Master server, and an as yet empty Slave server. This stage depends on whether data is constantly being added to the Master. If so, we will have to prevent all database access on the Master so nothing can be added. This means your server will hang during the next step. If no data is being added to the server, you can skip this step. On the Master server, log into MySQL and do the following:
mysql -u root -p (log into MySQL)
CODE:
FLUSH TABLES WITH READ LOCK;
Now we will use mysqldump to get the data out. So, still on the Master server:
CODE:
mysqldump my_database -u root -p > /home/my_home_dir/database.sql;
gzip /home/my_home_dir/database.sql;
Make sure you change my_database to your database name, and my_home_dir to the name of your home directory (or another directory of your choosing). You wll now have a file called database.sql.gz in your home directory. This is a gziped copy of your database.

On the Slave...

Now we need to copy over the gzipped file. On the Slave run the following:
CODE:
scp root@128.0.0.1:/home/my_home_dir/database.sql.gz /home/my_home_dir/
Make sure 128.0.0.1 is the I.P of the Master. This will copy the file from the Master and put it in your home directory on the Slave. Now we just need to import into MySQL:
mysql -u root -p (log into MySQL)
CODE:
CREATE DATABASE `my_database`;
CODE:
gunzip /home/my_home_dir/database.sql.gz
mysql -u root -p my_database  </home/my_home_dir/database.sql

Ready to rumble...

On the Master...

Now we're ready to kick things off. We need to find the position the Master is at in the logs. So, log into MySQL and run the following:
mysql -u root -p (log into MySQL)
CODE:
SHOW MASTER STATUS;
This should give you an output along these lines:
CODE:
+---------------------+----------+-------------------------------+------------------+
| File                | Position | Binlog_Do_DB                  | Binlog_Ignore_DB |
+---------------------+----------+-------------------------------+------------------+
| mysql-bin.000001    | 21197930 | my_database,my_database       |                  |
+---------------------+----------+-------------------------------+------------------+
Keep that on-screen.

On the Slave...

Log into MySQL and do the following:
mysql -u root -p (log into MySQL)
CODE:
slave stop;
CHANGE MASTER TO MASTER_HOST='128.0.0.1', MASTER_USER='slave_user', MASTER_PASSWORD='slave_password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=21197930;
slave start;
First we stop the Slave. Then we tell it exactly where to look in the Master log file. We use the values for our previous SHOW MASTER STATUS; command on the Master. You should change 128.0.0.1 to the I.P of the Master, and change the user and password accordingly.
The Slave will now be waiting. So all that's left is to...

Back on the Master...

We shoud already be logged into MySQL, so all you have to do is:
CODE:
unlock tables;
To release the tables from lock. Note you only have to do this if you previously ran FLUSH TABLES WITH READ LOCK;

And the recovery part?

Several times it's happened to me that a server has crashed, or a hostname changed or whatever, and I've had a real trouble getting replication to work again. The solution has been to clear out the logs.

On the Slave

Clear out any replication logs from /var/lib/mysql or whever the logs are being stored, as stated in my.cnf. This usually does the trick:
CODE:
rm *relay*
rm master.info

On the Master

Again, get rid of the logs, as per where they are stored in my.cnf. For me it's the following:
CODE:
cd /home/mysql/logs/
rm -f *
This should give you a fresh start on things. You can now start again from the beginning...

Final Notes

My database doesn't use InnoDB tables - it's all MyISAM. However, the MySQL manual recommends adding this to my.cnf for InnoDB databases:
CODE:
innodb_flush_log_at_trx_commit=1
sync_binlog=1
See here for more info: http://dev.mysql.com/doc/refman/5.1/en/replication-howto-masterbaseconfig.html

Wednesday 15 August 2012

SQL command line

dump file sql
 mysqldump -umytable -p -hserver DatabseName TableName >  test.sql;
dump file gz
mysqldump -umytable -p -hserver DatabseName TableName | gzip >  test.gz;

unzip mysql dump
gunzip < filename.gz | mysql -uphuc.duong -p -hlocalhost DatabseName;

mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

Wednesday 8 August 2012

5 Vi du ve htaccess


Xin chào các bạn, nhất là những bạn đã đang và sẽ là một webmaster icon smile 5 ví dụ về Rewrite URL dùng htaccess thông dụng
Bạn là một WM ắt hẳn phải chú trọng đến việc quảng bá website để thu hút traffic. Có rất nhiều yếu tố để đạt được yêu cầu đó mà trong khuôn khổ bài viết hạn hẹp này Việt Coding không thể nói hết. Việt Coding chỉ nói đến việc “viết lại” (rewrite) đường dẫn web (url) của bạn cho đẹp, cho dễ nhớ, nói chung là thân thiện. Thân thiện với cả visitor và các search engine. Bạn nghĩ sao nếu phải gõ tay hoặc copy dòng địa chỉ này paste vào trình duyệt :
/index.php?option=com_content&view=article&id=25:bai-hc-t-cuc-sng&catid=1:news-tonghop&joscclean=1&comment_id=473#josc473
hay bạn chỉ muốn gõ đơn giản như vầy:
/article/25/bai-hoc-tu-cuoc-song/
Tôi có cần trả lời giùm bạn không nhỉ ? icon biggrin 5 ví dụ về Rewrite URL dùng htaccess thông dụng
Bài viết dưới đây sẽ hướng dẫn các bạn sử dụng sức mạnh của tập tin .htaccess, qua đó viết lại đường dẫn website của bạn một cách thân thiện (friendly url).
Hy vọng bạn chưa quên một bài viết khác của Việt Coding cũng nói về .htaccess : Bảo vệ băng thông webblog của bạn bằng htaccess ?
urllarge 5 ví dụ về Rewrite URL dùng htaccess thông dụng
URL Rewriting - Một kỹ thuật quan trọng giúp cho SEO - (Nguồn : Internet)
Đây là 5 ví dụ thông dụng nhất:
1. product.php?id=12 —> product-12.html
Đây là một ví dụ cực kỳ cơ bản của rewrite url, thường dùng để che dấu đuôi PHP:
RewriteEngine on
RewriteRule ^product-([0-9]+).html$ product.php?id=$1
2. product.php?id=12 —> product/ipod-nano/12.html
Một SEO chuyên nghiệp luôn biết khéo léo hiển thị từ khoá chính trên URL. Như trong ví dụ sau, URL hiển thị tên của sản phẩm chính:
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+).html$ product.php?id=$2
3. Non www URL —> www URL
Nếu bạn muốn khi visitor gõ vào trình duyệt vietcoding.com thì được chuyển thẳng vào www.vietcoding.com thì cấu hình như sau:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^vietcoding.com$
RewriteRule (.*) http://www.vietcoding.com/$1 [R=301,L]
4) vietcoding.com/user.php?username=xyz —> vietcoding.com/xyz
Nếu website của bạn có một lượng lớn thành viên (như mạng xã hội, diễn đàn), đây là một cách rất hay để rút gọn liên kết cho thành viên dễ nhớ
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Chuyển domain vào cấp thư mục:
Trong quá trình phát triển website, luôn luôn có sự thay đổi (về mã nguồn, cơ sở dữ liệu, cấu trúc site,…). Ví dụ vietcoding.com trước giờ vẫn chạy ở thư mục gốc (root), giả sử vì một lý do nào đó, Việt Coding cần dùng root cho việc khác và muốn chuyển webblog hiện tại vào thư mục /blog/ thì sao ?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^vietcoding.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.vietcoding.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
Thế là xong ! Từ nay mọi truy vấn đến vietcoding.com hoặc www.vietcoding.com đều được redirect vào vietcoding.com/blog
Tất nhiên, URL rewriting rất đa dạng và phong phú, tuỳ sự sáng tạo của từng webmaster mà sẽ cho ra những url độc đáo không đụng hàng, ví dụ như:
http://abc.com/Music/#List_Album2,-1,1
http://abc.com/Products/#IPhone|3GS|32G