顯示具有 CentOS 標籤的文章。 顯示所有文章
顯示具有 CentOS 標籤的文章。 顯示所有文章

2015年8月25日 星期二

vim 常用功能

引用自:夕口技術錄

如何叫它(vi)幫您自動對應出 ( ) 、{}、[]
也就是停在 ( 上括號時,
要自動讓遊標幫您指到 ) 下括號的地方…
當當…
就是用 Shift + 5 => % 就可以辦到啦…
以下是另外的技巧補充哦:
w       游標跳到下個word的開頭
e       游標跳到下個word的字尾
b       游標回到上個word的開頭
$       移到游標所在行的行尾
^       移到該行第一個非空白的字元
0       移到該行的開頭位置
#|      移到該行的第 # 個位置   例:  5| 12|
f char      尋找(FIND) 本行中下一個 char 字元
t char      將游標移到本行中下一個 char 字元之前
F char      尋找本行中前一個char所在的位置
T char      移到本行中前一個 char 字元之後
;       重覆上一個 f, t, F, 或 T 的動作
,       反向重覆上一個 f, t, F, 或 T 的動作
%       尋找有無對應的 () {} 或 []
H    移到游標到畫面的最上方 (HIGHEST)
M    移到游標到畫面的中間 (MIDDLE)
L    移到游標到畫面的最下方
m char     (MARK) 把這個地方標示成 char
‘ char     (quote character) 跳到被標為 char的那一行
"         (按兩次’) 回到剛才的位置
G      GO to the last line in the file
#G      GO to line #.  (e.g., 3G , 5G , 175G )
G      (GO)  跳到檔案的最後一行.
#G      (GO to line #). 跳到第 # 行  (例: 3G , 5G , 124G )
{       (左大括號) 跳到上一段的開頭
}       (右大括號) 跳到下一段的的開頭.
(       (左小括號) 移到這個句子的開頭
)       (右小括號) 移到下一個句子的開頭
[[     跳往上一個函式
]]     跳往下一個函式
/string      向後尋找 string 這個字串
?string      往前尋找 string 這個字串

2015年6月3日 星期三

C program to connect MySQL database server

Forward From : http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html
From my mailbag:
How do I write a C program to connect MySQL database server?
MySQL database does support C program API just like PHP or perl.
The C API code is distributed with MySQL. It is included in the mysqlclient library and allows C programs to access a database.
Many of the clients in the MySQL source distribution are written in C. If you are looking for examples that demonstrate how to use the C API, take a look at these clients. You can find these in the clients directory in the MySQL source distribution.

Requirements

Make sure you have development environment installed such as gcc, mysql development package etc. Following is the list summarize the list of packages to compile program:
  • mysql: MySQL client programs and shared library
  • mysqlclient: Backlevel MySQL shared libraries (old libs)
  • mysql-devel: Files for development of MySQL applications (a must have)
  • mysql-server: Mysql server itself
  • gcc, make and other development libs: GNU C compiler

Sample C Program

Following instructions should work on any Linux distro or UNIX computer. Here is the small program that connects to mysql server and list tables from mysql database.(download link):
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
main() { MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char *server = "localhost"; char *user = "root"; char *password = "PASSWORD"; /* set me first */ char *database = "mysql"; conn = mysql_init(NULL); /* Connect to database */ if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } /* send SQL query */ if (mysql_query(conn, "show tables")) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } res = mysql_use_result(conn); /* output table name */ printf("MySQL Tables in mysql database:\n"); while ((row = mysql_fetch_row(res)) != NULL) printf("%s \n", row[0]); /* close connection */ mysql_free_result(res); mysql_close(conn); }

How do I compile and link program against MySQL libs?

MySQL comes with a special script called mysql_config. It provides you with useful information for compiling your MySQL client and connecting it to MySQL database server. You need to use following two options.
Pass --libs option - Libraries and options required to link with the MySQL client library.
$ mysql_config --libs
Output:
-L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto
Pass --cflags option - Compiler flags to find include files and critical compiler flags and defines used when compiling the libmysqlclient library.
$ mysql_config --cflags
Output:
-I/usr/include/mysql -g -pipe -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing
You need to pass above option to GNU C compiler i.e. gcc. So to compile above program, enter:
gcc -o output-file $(mysql_config --cflags) mysql-c-api.c $(mysql_config --libs)
Now execute program:
$ ./output-file
Output:
MySQL Tables in mysql database:
columns_priv
db
func
help_category
help_keyword
help_relation
help_topic
host
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user 

2014年12月4日 星期四

修改 Service 開機關機順序

  開啟/etc/init.d下的Script ,找到下面這句:

  #chkconfig: 2345 64 36

2345是啟動的環境
64是啟動的順序
36是關機的順序

改變後執行下面的命令就完成了:

        #chkconfig --del servicename

        #chkconfig --add servicename

        #chkconfig servicename on

CentOS 新增硬碟搬移 /var 目錄


本來以為搬移/var只是檔案複製一下,沒想到很多麻煩就從建立新硬碟開始吧:

A.#ls /dev/sd*
   查一下新硬碟代號

B.#fdisk -l /dev/sdc
  確定容量大小

C.#fdisk /dev/sdc
   
    進入fdisk設定磁區


  1.   新增分割區,輸入 n 按 Enter。
  2. 選擇要建立 extended 還是 primary partition,因為我的硬碟全部只要一個分割區,所以我選 primary,輸入 p 按 Enter。
  3. 選擇 Partition number,primary 分割區最多可以有四個,隨便選都可以,不過建議選 1,免得以後看起來很奇怪,輸入 1 按 Enter。
  4. 輸入開始的 cylinder,用預設值就可以了,直接按 Enter。
  5. 輸入結束的 cylinder,若是要用最大的容量,就直接按 Enter,若是要指定分割區的大小,就用 +size{K,M,G} 的形式指定,例如指定為 100G 的大小就輸入 +100G 再按 Enter。
  6. 最後將分割表寫入硬碟,輸入 w 再按 Enter。

D.#fdisk -l /dev/sdc

    確認分割區是否正確

E.#mkfs -t ext4 /dev/sdc1

   格式化分割區

F.#blkid

   查詢硬碟 UUID

G:#vi /etc/fstab

   加入啟動的UUID硬碟LABEL,如下
 
    UUID=146d3bb3-e351-45c8-ac84-413241313 /var          ext4    defaults        0


H:#init 1

   進入  Single Mode

I:#mv var va2

   將原來var變更名稱

J:#mkdir /var

   建立新的/var

K:#mount /var
   掛載新的var

L:#cp -ax /var2/* /var
   複製/var所有檔案

M:#restorecon -vr /var
   修復Security Label

尤其是最後一步,非常重要~








2013年9月4日 星期三

CentOS 6 關閉 IPv6

如果使用預設值網路環境中卻沒有IPv6的設備,Log會出現大量的info
因此關閉IPv6就是必然了。

動作如下:

1.編輯 /etc/sysconfig/network
   增加或修改
   NETWORKING_IPV6=no

2.編輯 /etc/modprobe.d/ipv6.conf
  增加或修改
  alias net-pf-10 off

  alias ipv6 off

3.編輯 /etc/modprobe.d/blacklist.conf
   增加或修改
   blacklist net-pf-10
   blacklist ipv6

4.執行關閉 ipv6 iptable 功能

#service ip6tables stop

#chkconfig ip6tables off

重啟network 後就完成設定,如果設定未完成會出現以下訊息:

FATAL: Module off not found