RSS구독하기:SUBSCRIBE TO RSS FEED
즐겨찾기추가:ADD FAVORITE
글쓰기:POST
관리자:ADMINISTRATOR
OS/Unix  2013/05/23 11:33
http://chxo.com/be2/20040511_5667.html



To set up a tunneled connection to a MySQL server at remotehost.com, you can issue the following two commands on any client:
ssh -fNg -L 3307:127.0.0.1:3306 myuser@remotehost.com
mysql -h 127.0.0.1 -P 3307 -u dbuser -p db
The first command tells ssh to log in to remotehost.com as myuser, go into the background (-f) and not execute any remote command (-N), and set up port-forwarding (-L localport:localhost:remoteport ). In this case, we forward port 3307 on localhost to port 3306 on remotehost.com.

The second command tells the local MySQL client to connect to localhost port 3307 (which is forwarded via ssh to remotehost.com:3306). The exchange of data between client and server is now sent over the encrypted ssh connection.

To make the same connection using PHP's built-in MySQL client:
<?php
 $smysql = mysql_connect( "127.0.0.1:3307", "dbuser", "PASS" );
 mysql_select_db( "db", $smysql );
?>

Server Configuration
Obviously, this approach requires that you have a shell account on the remote (database) server that you can log into in order to set up the forwarded port. And it requires a MySQL login that is allowed to connect to the database in question from host 127.0.0.1

On the database server, as root, create a user to handle the server side of all tunnels. This user will have no valid shell (might not work on all operating systems?)
useradd -s /bin/false myuser
mkdir /home/myuser/.ssh
touch /home/myuser/.ssh/authorized_keys
chown -R myuser:myuser /home/myuser/.ssh
chmod 755 /home/myuser/.ssh
chmod 600 /home/myuser/.ssh/authorized_keys
Connections via the tunnel will look like they are coming from 127.0.0.1, so you need to update the GRANT tables in the database:
USE mysql;
GRANT ALL ON db.* TO dbuser@127.0.0.1 IDENTIFIED BY 'PASS';
FLUSH PRIVILEGES;
Congrats, your server is ready to accept tunnels.

Client Configuration
Because you won't be around to type a password, you'll need to set up an RSA key pair for ssh on each client ( more details here ).

The first step is to set up an RSA key pair as root on each client, leaving all questions blank:
root@local# ssh-keygen -t rsa
 Generating public/private rsa key pair.
 Enter file in which to save the key (/var/root/.ssh/id_rsa):
 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:
 Your identification has been saved in /var/root/.ssh/id_rsa.
 Your public key has been saved in /var/root/.ssh/id_rsa.pub.
 The key fingerprint is:
 23:02:1b:f7:55:2b:35:cf:0c:5d:ad:21:c7:e1:78:ca root@myhost.local
root@local#
The next step is to append the rsa public key file to myuser's .ssh/authorized_keys on the server. First we copy the key, then we shell in and append it to the authoized list:
root@local# scp /var/root/.ssh/id_rsa.pub root@remotehost.com:/tmp/myhost.local_rsa.pub
root@local# ssh remotehost.com
...
root@remote# cat /tmp/myhost.local_rsa.pub >> /home/myuser/.ssh/authorized_keys
root@remote# exit

Now you can set up the connection on the client, without typing myuser's password:
root@local# ssh -fNg -L 3307:127.0.0.1:3306 myuser@remotehost.com


Making It A Daemon

A quick and dirty way to make sure the connection runs on startup and respawns on failure is to add it to /etc/inittab and have the init process (the, uh, kernel) keep it going.

Add the following to /etc/inittab on each client:
sm:345:respawn:/usr/bin/ssh -Ng -L 3307:127.0.0.1:3306 myuser@remotehost.com
And that should be all you need to do. Send init the HUP signal ( kill -HUP 1 ) to make it reload the configuration. To turn it off, comment out the line and HUP init again.
2013/05/23 11:33 2013/05/23 11:33
이 글에는 트랙백을 보낼 수 없습니다
MySQL에서 사용하는 log-bin이라는 옵션은 사용된 쿼리들이 로깅되는 파일이며 Innodb 혹은 Replication등에서 사용되곤 합니다.

하지만 이 파일의 문제는 무한정 늘어난다는 것입니다. relay-log-space-limit 같은 옵션이 있지만 이 옵션은 Replication에서 사용되는 relay-log-bin의 용량을 제한 하는 옵션이지 해결책이 되지 못합니다.

하지만 이 파일을 정리하는 방법이 있습니다. 우선 다음의 Query를 사용하는 것입니다.

PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY)

INTERVAL에서는 원하시는 로깅 시점을 기록하시면 됩니다. 위의 경우에는 30일 이전의 로깅 정보를 삭제하게 됩니다.

차례차례 해보도록 하겠습니다.

1. 현재의 하드디스크 용량 상태를 확인합니다.

[root@Theeye db]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              18G  1.9G   15G  12% /
/
dev/sda3              15G  803M   13G   6% /home
/
dev/sda2              97G   78G   15G  85% /usr/local/mysql/db

데이터베이스 공간이 85%나 사용되어 곧 꽉찰지도 모른다는 불안감에 휩싸이게 되었습니다.

2. log-bin 파일을 확인해 봅시다.

[root@Theeye db]# ls mysql-bin.*
mysql
-bin.000001  mysql-bin.000002  mysql-bin.000003 ......  mysql-bin.000073

파일이 엄청나게 많이 있습니다.

3. 다음 명령어를 수행합니다.

mysql -e "PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY)"

물론 mysql에 접속하셔서 위의 명령어를 입력하셔도 됩니다. Super권한을 가진 root계정으로 실행해 주세요.

4. log-bin 파일이 줄었나 확인해 봅시다.

[root@Theeye db]# ls mysql-bin.*
mysql
-bin.000041  mysql-bin.000042  mysql-bin.000043 ......  mysql-bin.000073

41번 이전의 파일들이 모두 삭제 되었습니다. 41번 부터가 최근 30일 이내의 로그정보가 남아있는 모양이군요.

5. 하드디스크 용량 상태를 확인해 봅시다.

[root@Theeye db]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              18G  1.9G   15G  12% /
/
dev/sda3              15G  803M   13G   6% /home
/
dev/sda2              97G   40G   53G  43% /usr/local/mysql/db

확실히 용량이 많이 확보가 되었군요^^

주기적인 실행이 필요하실 경우에는 위의 명령어를 crontab에 등록하시면 됩니다. 일주일에 한번정도의 실행이 적당하겠네요.




2013/05/23 10:41 2013/05/23 10:41
이 글에는 트랙백을 보낼 수 없습니다
Language/Python  2013/05/20 15:11
#!/usr/bin/python
# -*- coding: utf-8 -*-
########################################################################
import sys, time

def cli_progress_test(end_val, bar_length=20):
    for i in xrange(0, end_val):
        percent = float(i) / end_val
        hashes = '#' * int(round(percent * bar_length))
        spaces = ' ' * (bar_length - len(hashes))
        sys.stdout.write("\rPercent: [{0}] {1}%".format(hashes + spaces, int(round(percent * 100))))
        sys.stdout.flush()
        time.sleep(1)

cli_progress_test(50)

2013/05/20 15:11 2013/05/20 15:11
이 글에는 트랙백을 보낼 수 없습니다
CentOS 시간대 변경

# cp /etc/localtime /etc/localtime.ori <-- 원래 시간 정보를 복사해놓고...필요는 없는데...습관
# cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime  <-- 원하는 시간대의 정보를 복사해온다. 한국은 Seoul 이죠 (링크가능)
cp: overwrite `/etc/localtime'? Y
# date
Mon Nov 10 10:35:50 JST 2011   <--- 바꼇다
2013/05/20 15:07 2013/05/20 15:07
이 글에는 트랙백을 보낼 수 없습니다
import sys

reload(sys)
sys.setdefaultencoding("utf-8")
2012/11/13 12:42 2012/11/13 12:42
이 글에는 트랙백을 보낼 수 없습니다
kinuz:누즈의 웹 블로그
누즈의 웹 블로그
전체 (402)
Language (130)
Database (66)
OS (121)
Programs (51)
MyFavorite (10)
(0)
CaPoEiRa (2)
유용한 정보 (15)
server (1)
PDA (5)
여행 (0)
애자일 (1)
«   2013/05   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
  1. 2013/05 (4)
  2. 2012/11 (1)
  3. 2012/02 (2)
  4. 2012/01 (1)
  5. 2011/12 (3)