วันพฤหัสบดีที่ 27 มกราคม พ.ศ. 2554

Tuning Server Parameters

Tuning Server Parameters

You can determine the default buffer sizes used by the mysqld server using this command:

shell> mysqld --verbose --help

This command produces a list of all mysqld options and configurable system variables. The output includes the default variable values and looks something like this:

help                              TRUE
abort-slave-event-count 0
allow-suspicious-udfs FALSE
auto-increment-increment 1
auto-increment-offset 1
automatic-sp-privileges TRUE
basedir /home/jon/bin/mysql-5.0/
bdb FALSE
bind-address (No default value)
character-set-client-handshake TRUE
character-set-filesystem binary
character-set-server latin1
character-sets-dir /home/jon/bin/mysql-5.0/share/mysql/charsets/
chroot (No default value)
collation-server latin1_swedish_ci
completion-type 0
concurrent-insert 1
console FALSE
datadir /home/jon/bin/mysql-5.0/var/
default-character-set latin1
default-collation latin1_swedish_ci
default-time-zone (No default value)
disconnect-slave-event-count 0
enable-locking FALSE
enable-pstack FALSE
engine-condition-pushdown FALSE
external-locking FALSE
federated TRUE
gdb FALSE
large-pages FALSE
init-connect (No default value)
init-file (No default value)
init-slave (No default value)
innodb TRUE
innodb_checksums TRUE
innodb_data_home_dir (No default value)
innodb_adaptive_hash_index TRUE
innodb_doublewrite TRUE
innodb_fast_shutdown 1
innodb_file_per_table FALSE
innodb_flush_log_at_trx_commit 1
innodb_flush_method (No default value)
innodb_locks_unsafe_for_binlog FALSE
innodb_log_arch_dir (No default value)
innodb_log_group_home_dir (No default value)
innodb_max_dirty_pages_pct 90
innodb_max_purge_lag 0
innodb_rollback_on_timeout FALSE
innodb_status_file FALSE
innodb_support_xa TRUE
innodb_table_locks TRUE
isam FALSE
language /home/jon/bin/mysql-5.0/share/mysql/english/
lc-time-names en_US
local-infile TRUE
log (No default value)
log-bin (No default value)
log-bin-index (No default value)
log-bin-trust-function-creators FALSE
log-bin-trust-routine-creators FALSE
log-error
log-isam myisam.log
log-queries-not-using-indexes FALSE
log-short-format FALSE
log-slave-updates FALSE
log-slow-admin-statements FALSE
log-slow-queries (No default value)
log-tc tc.log
log-tc-size 24576
log-update (No default value)
log-warnings 1
low-priority-updates FALSE
master-connect-retry 60
master-host (No default value)
master-info-file master.info
master-password (No default value)
master-port 3306
master-retry-count 86400
master-ssl FALSE
master-ssl-ca (No default value)
master-ssl-capath (No default value)
master-ssl-cert (No default value)
master-ssl-cipher (No default value)
master-ssl-key (No default value)
master-user test
max-binlog-dump-events 0
memlock FALSE
merge TRUE
myisam-recover OFF
ndbcluster FALSE
new FALSE
old-passwords FALSE
old-style-user-limits FALSE
pid-file /home/jon/bin/mysql-5.0/var/tonfisk.pid
port 3306
port-open-timeout 0
relay-log (No default value)
relay-log-index (No default value)
relay-log-info-file relay-log.info
replicate-same-server-id FALSE
report-host (No default value)
report-password (No default value)
report-port 3306
report-user (No default value)
rpl-recovery-rank 0
safe-user-create FALSE
secure-auth FALSE
secure-file-priv (No default value)
server-id 0
show-slave-auth-info FALSE
skip-grant-tables FALSE
skip-slave-start FALSE
slave-load-tmpdir /tmp/
socket /tmp/mysql.sock
sporadic-binlog-dump-fail FALSE
sql-mode OFF
symbolic-links TRUE
sysdate-is-now FALSE
tc-heuristic-recover (No default value)
temp-pool TRUE
timed_mutexes FALSE
tmpdir (No default value)
use-symbolic-links TRUE
verbose TRUE
warnings 1
back_log 50
binlog_cache_size 32768
bulk_insert_buffer_size 8388608
connect_timeout 10
date_format (No default value)
datetime_format (No default value)
default_week_format 0
delayed_insert_limit 100
delayed_insert_timeout 300
delayed_queue_size 1000
div_precision_increment 4
expire_logs_days 0
flush_time 0
ft_max_word_len 84
ft_min_word_len 4
ft_query_expansion_limit 20
ft_stopword_file (No default value)
group_concat_max_len 1024
innodb_additional_mem_pool_size 1048576
innodb_autoextend_increment 8
innodb_buffer_pool_awe_mem_mb 0
innodb_buffer_pool_size 8388608
innodb_commit_concurrency 0
innodb_concurrency_tickets 500
innodb_file_io_threads 4
innodb_force_recovery 0
innodb_lock_wait_timeout 50
innodb_log_buffer_size 1048576
innodb_log_file_size 5242880
innodb_log_files_in_group 2
innodb_mirrored_log_groups 1
innodb_open_files 300
innodb_sync_spin_loops 20
innodb_thread_concurrency 8
innodb_thread_sleep_delay 10000
interactive_timeout 28800
join_buffer_size 131072
keep_files_on_create FALSE
key_buffer_size 8384512
key_cache_age_threshold 300
key_cache_block_size 1024
key_cache_division_limit 100
long_query_time 10
lower_case_table_names 0
max_allowed_packet 1048576
max_binlog_cache_size 18446744073709547520
max_binlog_size 1073741824
max_connect_errors 10
max_connections 100
max_delayed_threads 20
max_error_count 64
max_heap_table_size 16777216
max_join_size 18446744073709551615
max_length_for_sort_data 1024
max_prepared_stmt_count 16382
max_relay_log_size 0
max_seeks_for_key 18446744073709551615
max_sort_length 1024
max_sp_recursion_depth 0
max_tmp_tables 32
max_user_connections 0
max_write_lock_count 18446744073709551615
multi_range_count 256
myisam_block_size 1024
myisam_data_pointer_size 6
myisam_max_extra_sort_file_size 2147483648
myisam_max_sort_file_size 9223372036853727232
myisam_repair_threads 1
myisam_sort_buffer_size 8388608
myisam_stats_method nulls_unequal
net_buffer_length 16384
net_read_timeout 30
net_retry_count 10
net_write_timeout 60
open_files_limit 0
optimizer_prune_level 1
optimizer_search_depth 62
plugin_dir
preload_buffer_size 32768
query_alloc_block_size 8192
query_cache_limit 1048576
query_cache_min_res_unit 4096
query_cache_size 0
query_cache_type 1
query_cache_wlock_invalidate FALSE
query_prealloc_size 8192
range_alloc_block_size 4096
read_buffer_size 131072
read_only FALSE
read_rnd_buffer_size 262144
record_buffer 131072
relay_log_purge TRUE
relay_log_space_limit 0
slave_compressed_protocol FALSE
slave_net_timeout 3600
slave_transaction_retries 10
slow_launch_time 2
sort_buffer_size 2097144
sync-binlog 0
sync-frm TRUE
table_cache 64
table_lock_wait_timeout 50
thread_cache_size 0
thread_concurrency 10
thread_stack 262144
time_format (No default value)
tmp_table_size 33554432
transaction_alloc_block_size 8192
transaction_prealloc_size 4096
updatable_views_with_limit 1
wait_timeout 28800

For a mysqld server that is currently running, you can see the current values of its system variables by connecting to it and issuing this statement:

mysql> SHOW VARIABLES;

You can also see some statistical and status indicators for a running server by issuing this statement:

mysql> SHOW STATUS;

System variable and status information also can be obtained using mysqladmin:

shell> mysqladmin variables
shell> mysqladmin extended-status

For a full description of all system and status variables, see Section 5.1.3, “Server System Variables”, and Section 5.1.5, “Server Status Variables”.

MySQL uses algorithms that are very scalable, so you can usually run with very little memory. However, normally you get better performance by giving MySQL more memory.

When tuning a MySQL server, the two most important variables to configure are key_buffer_size and table_cache. You should first feel confident that you have these set appropriately before trying to change any other variables.

The following examples indicate some typical variable values for different runtime configurations.

  • If you have at least 256MB of memory and many tables and want maximum performance with a moderate number of clients, you should use something like this:

    shell> mysqld_safe --key_buffer_size=64M --table_cache=256 \
    --sort_buffer_size=4M --read_buffer_size=1M &
  • If you have only 128MB of memory and only a few tables, but you still do a lot of sorting, you can use something like this:

    shell> mysqld_safe --key_buffer_size=16M --sort_buffer_size=1M

    If there are very many simultaneous connections, swapping problems may occur unless mysqld has been configured to use very little memory for each connection. mysqld performs better if you have enough memory for all connections.

  • With little memory and lots of connections, use something like this:

    shell> mysqld_safe --key_buffer_size=512K --sort_buffer_size=100K \
    --read_buffer_size=100K &

    Or even this:

    shell> mysqld_safe --key_buffer_size=512K --sort_buffer_size=16K \
    --table_cache=32 --read_buffer_size=8K \
    --net_buffer_length=1K &

If you are performing GROUP BY or ORDER BY operations on tables that are much larger than your available memory, you should increase the value of read_rnd_buffer_size to speed up the reading of rows following sorting operations.

You can make use of the example option files included with your MySQL distribution; see Section 4.2.3.3.2, “Preconfigured Option Files”.

If you specify an option on the command line for mysqld or mysqld_safe, it remains in effect only for that invocation of the server. To use the option every time the server runs, put it in an option file.

To see the effects of a parameter change, do something like this:

shell> mysqld --key_buffer_size=32M --verbose --help

The variable values are listed near the end of the output. Make sure that the --verbose and --help options are last. Otherwise, the effect of any options listed after them on the command line are not reflected in the output.

วันพุธที่ 26 มกราคม พ.ศ. 2554

mysqlisacheck VS mysqlcheck

ข้อแตกต่างระหว่างคำสั่งทั้งสอง mysqlcheck คำสั่งนี้สามารถใช้งานขณะที่ Service mysqld ทำงานอยู่ได้ ส่วน mysqlisacheck จำเป็นต้องหยุด Service mysqld ก่อนเพราะเป็นการทำในระดับ file

วันอังคารที่ 25 มกราคม พ.ศ. 2554

MYSQL CHECK

กรณีที่ต้องการ ตรวจสอบ Mysql และให้ทำการ Auto repair กรณี Tables Crashed

#mysqlcheck --all-databases --extended --medium-check --auto-repair --optimize --verbose -uMyAccount -pMyPASSWORD -h 127.0.0.1

Table is marked as crashed and should be repaire

MySQL อาจเกิดปัญหา ทำให้เกิด Error ทำนองนี้

"Table xxx is marked as crashed and should be repaired"

ให้ทำดังนี้

1. ไปยังพื้นที่จัดเก็บ Database File คือ /var/lib/mysql

2. แล้วเข้าไปใน Directory ของ Database นั้นๆ เช่น xxx เป็นต้น

3. ใช้คำสั่ง myisamchk xxx.MYI เพื่อตรวจสอบ (xxx คือชื่อ Database ตัวที่มีปัญหา)

4. หากพบปัญหา ลองใช้คำสั่ง myisamchk -r xxx.MYI เพื่อพยายาม recovery เป็นอันดับแรก[http://dev.mysql.com/doc/refman/5.1/en/myisamchk.html ]

วันศุกร์ที่ 21 มกราคม พ.ศ. 2554

Test zimbra

Turns out that running zmlocalconfig |grep zmprov showed the problem. The results were:

zimbra_zmprov_default_soap_server = localhost
zimbra_zmprov_default_to_ldap = TRUE

on the working server and:

zimbra_zmprov_default_soap_server = localhost
zimbra_zmprov_default_to_ldap = false

on the server with the error. Solution was to either change localhost to a mailbox server OR change default_to_ldap to TRUE.



zmlocalconfig -e zimbra_zmprov_default_soap_server=ns.mm.com
zmlocalconfig -e zimbra_zmprov_default_to_ldap = TRUE

zmlocalconfig -e zimbra_server_hostname = ns.mm.com


zmlocalconfig -e zimbra_zmprov_default_soap_server = localhost
zmlocalconfig -e zimbra_zmprov_default_to_ldap=false
zmlocalconfig -e ldap_port=389
zmlocalconfig -e ldap_url=ns.mm.com

วันอาทิตย์ที่ 16 มกราคม พ.ศ. 2554

เข้า ssh ไม่ต้องใส่ password โดยใช้ key

มันคือการทำ ให้เวลาเราเข้าที่ host นึงแล้วพอเรา ssh ไปอีกทีสามารถเข้าได้เลยโดยไม่ต้องใส่ password วิธีการคือเราต้องไปสร้าง key ก่อน แล้วเอามาใส่ อธิบายก็ งงแน่ๆ เพราะผมก็งง
มายกตัวอย่างให้เห็นกันดีกว่า

Client จะ เป็นเครื่องที่เราใช้เป็น Client พอ Login เข้าเครื่องนี้แล้วอยากไป เครื่อง Server สามารถเข้า Server ได้เลยโดยไม่ต้องใส่ Password
Server เครื่อง Server ที่เราอยากเข้ามาโดยไม่ต้องใส่ Password

ขั้นตอน การทำ key นั้นไม่ยุ่งยากครับ เริ่มก็ ต้องเข้าใจนะครับว่าตัวไหน Client ตัวไหน Server จะได้ไม่ งง
ผมจะใช้ตัวย่อ C = Client และ S = Server นะครับ (C$ = command line บนเครื่อง Client , S$ = command line บนเครื่อง Server)

Code:

C$ ssh-keygen -t dsa

การส ร้าง key มีทั้ง dsa , rsa เปลี่ยนเอาตามที่ชอบ ความแตกต่าง ลองหาอ่านดูใน net ครับผมก็หาผ่านๆ ไม่แน่ใจว่าอันไหนดีกว่า แต่เห็นว่าเวลาเรา ssh default มันจะสร้าง key นี้ เลยใช้อันนี้นะครับ

Quote
Generating public/private dsa key pair.
Enter file in which to save the key (/home/bobby/.ssh/id_dsa): Enter ผ่านไปเลย
Enter passphrase (empty for no passphrase): Enter ผ่านไปเลย
Enter same passphrase again: Enter ผ่านไปเลย
Your identification has been saved in /home/bobby/.ssh/id_dsa.
Your public key has been saved in /home/bobby/.ssh/id_dsa.pub.
The key fingerprint is:
de:59:05:fe:aa:6e:15:73:2a:c7:53:0c:c1:ae:e7:92 bobby@Client
The key's randomart image is:
+--[ DSA 1024]----+
| .o. |
| .o. |
| ..o. |
| +o+ |
| S o.*. |
| . .ooB. |
| . oB.. |
| E.. |
| oo. |
+-----------------+
Enter รั่วไป 3 อันก็จะจบการสร้าง key โดยเราจะได้ไฟล์ /home/bobby/.ssh/id_dsa.pub ซึ่งเป็น key ของเครื่อง Client

Code:

C$ cd /home/bobby/.ssh
C$ ls -la

เข้าไปอยู่ใน path ที่เก็บ key เพราะเราจะต้อง copy key ไปยัง Server

Quote
-rw------- 1 bobby bobby 672 May 3 02:14 id_dsa
-rw-r--r-- 1 bobby bobby 612 May 3 02:14 id_dsa.pub
-rw-r--r-- 1 bobby bobby 394 May 3 02:07 known_hosts
รายการใน folder .ssh

Code:

C$ sftp bobby@Server

เตรียมส่ง key ไปยัง Server ผ่าน sftp (หรือจะใช้ ftp ธรรมดาก็ได้แล้วแต่ความถนัด)

Quote
Connecting to Server...
The authenticity of host 'Server (Server)' can't be established.
DSA key fingerprint is 5c:7e:65:eb:cc:6c:1e:24:49:16:b0:6f:d7:82:f7:72.
Are you sure you want to continue connecting (yes/no)? yes หากเข้ามาครั้งแรกก็ให้ตอบ yes
Warning: Permanently added 'Server' (DSA) to the list of known hosts.
Password: ใส่ password

Code:

sftp> put id_dsa.pub

ส่ง key จากเครื่อง Client ไปยัง Server

Quote
Uploading id_dsa.pub to /usr/home/bobby/id_dsa.pub
id_dsa.pub 100% 612 0.6KB/s 00:00

Code:

sftp> exit



ขั้นต่อไปเราก็ไปทำบน เครื่อง Server
Code:

S$ ssh bobby@Client

ถาม password ก็ใส่ไปเรียบร้อย ก็ exit ออกมา เป็นการทำเพื่อให้ระบบสร้าง path,file ต่างๆ เกี่ยวกับ ssh
ถ้าเคย ssh ออกจาก Server ไปที่อื่นๆ ไม่จำเป็นต้องทำหัวข้อนี้นะครับ

Code:

S$ mv id_dsa.pub .ssh/authorized_keys
S$ chmod 755 .ssh/authorized_keys

id_dsa.pub คือ key ที่เราส่ง sftp มามันก็จะอยู่ใน home เราก็ move ไปให้ .ssh/

เสร็จแล้วครับแค่นี้ เวลาเรา
Code:

C$ ssh bobby@Server

ก็จะไม่ถาม Password แล้วครับ

***การทำแบบนี้เวลาจะใช้งานเราต้องเข้า User บนเครื่อง Client ที่สร้าง key และ ssh ไปยังเครื่อง Server ที่ user ที่เอา key ไปใส่

ตัวอย่างเพื่ออธิบายเพิ่มเติม
เราใช้ bob@Client และอยากเข้า bobby@Server ก็สร้าง key ที่ bob@Client แล้ว ส่งไฟล์ key ไปเก็บไว้ใน path ของ bobby@Server

rsync

แอด user ที่จะใช้ในการซิงค์ที่นี้ใช้ apache
# add apache
# passwd apache

เปลี่ยน user มาเป็น apache
# su - apache
แล้วทำการ gent key
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/apache/.ssh/id_rsa):
Created directory '/home/apache/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

# cd .ssh
ก๊อป key มาใช้
# cp id_rsa.pup local.key

ทำการส่งค่าไฟล์ local.key ไปยังเครื่องแบคอัพ ด้วยการ sftp
# sftp apache@192.168.201.103

# The authenticity of host '192.168.201.103 (192.168.201.103)' can't be established.
DSA key fingerprint is 27:bd:d3:a3:e3:63:ba:01:43:ad:f1:03:2a:07:41:75.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.201.103' (DSA) to the list of known hosts.
apache@192.168.201.103's password: <ใส่พาสเวิร์ดไป >
sftp> put local.key
Uploading local.key to /home/apache/local.key
local.key 100% 412 0.4KB/s 00:00
sftp> bye


#### ที่เครื่อง backup ### ( 192.168.201.103 )
อันนี้เป็น CentOS ใช้ yum
# yum install rsync แล้วก็รอไป
adduser ที่จะใช้ให้เหมือนกัน
# adduser apache
# passwd apache เปลี่ยนพาสดังที่เราเซ็ต

แล้วเปลียน user
# su - apache

แล้วทำการ ssh ไปยังเครืองอื่นที่ไม่ใช่เครื่องต้นแบบ เพอให้มันสร้าง ไดเรคโทรี่ .ssh อัตโนมัติ
$ ssh apache@192.168.201.105
apache@192.168.201.105's password: < ใส่พาสเวิร์ด >
$ exit เสร็จแล้วออกมา
แล้วทำาการ ก๊อปคีย์ที่ได้เก็บไว้เป็นตัวออเท็น
$ cat local.key >> .ssh/authorized_keys
$ chmod 755 .ssh/authorized_keys

### ตัว server ต้นแบบ ###
กลับมาที่ฝั่ง server ต้นแบบ ลอง ssh ไปยังตัวแบคอัพ มันจะไม่ถามพาสเวิร์ด
$ ssh apache@192.168.201.103
[apache@streaming01 ~]$ เรียบร้อยเข้าได้เลย

เมื่อเข้าได้แล้วต่อไปก็ใส่คำสั่ง rsync เทสได้เลยว่าได้หรือเปล่า
# rsync [ออฟชั่น] [ ต้นแบบ] [ตัวแบคอัพ]
ลองเทสเป็น
# rsync -avz ssh /DATA/ apache@192.168.201.103:/home/BACKUP/
= ทำการซิงค์ที่เครื่องต้นแบบภายใต้พาร์ท /DATA ไปยังเครื่องแบคอัพ ip 192.168.201.103 ในพาร์ท /home/BACKUP/
ลองรันดู ถ้าทำงานได้จริงก็ผ่านแล้วครับเอาไปใส่ใน crontab เลย ทำวันละครั้งก็ได้ครับ
# crontab -e
โค๊ด:
0 0 * * * rsync -avz --delete ssh /DATA/ apache@192.168.201.103:/home/BACKUP/
ใน การใช้งานจริงจะใส่ --delete เป็นออฟชั่นเพิ่มไปอีกเพื่อให้เวลาซิงค์หากตัวต้นแบบมีการลบไฟล์ไหนไปแล้ว ปลายทางที่แบคอัพก็ลบตามไปด้วย

อีกอย่างที่ควรระวังคือการใส่พาร์ทระหว่าง /DATA หรือ /DATA/ ได้ผลต่างกันนะครับระวังให้ดี
ถ้าใส่เป็น rsync -avz --delete ssh /DATA apache@192.168.201.103:/home/BACKUP/
= เวลามันซิ้งค์ไปที่ปลายทางมันจะสร้างไดเรคโทรี่ /DATA ภายใต้ /home/BACKUP/
แต่ถ้าใส่เป็น rsync -avz --delete ssh /DATA/ apache@192.168.201.103:/home/BACKUP/
= เวลาซิงค์มันจะเอาโฟลเดอร์ย่อย ไปไว้ใน /home/BACKUP/ เลย
ซึ่ง หากไม่มีการเปลี่ยนรูปแบบคำสั่งอาจจะไม่มีผลอะไรเท่าไหร่ แต่ถ้าวันไหนเปลียนรูปแบบคำสั่งจาก มี / ไปเป็น ไม่มี / แล้วเราใส่ออฟชั่น --delete แล้ว
มันจะมองว่าปลายทางไม่เหมือนเดิมมันก็ลบทิ้งเกลี้ยงเลยแล้วทับด้วย data ใหม่หรือซับย่อยของโฟลเดอร์ใหม่เลย อันนี้ต้องระวัง