Howto: Connect MySQL server using C program API under Linux or UNIX

Howto: Connect MySQL server using C program API under Linux or UNIX


Bagaimana cara menulis program C untuk koneksi ke server database MySQL? How do I write a C program to connect MySQL database server?
database MySQL tidak mendukung program C API seperti PHP atau perl . MySQL database does support C program API just like PHP or perl .
C kode API didistribusikan dengan MySQL. The C API code is distributed with MySQL. Hal ini termasuk dalam perpustakaan mysqlclient dan memungkinkan C program untuk mengakses database. It is included in the mysqlclient library and allows C programs to access a database.
Banyak dari klien dalam distribusi source MySQL ditulis dalam C. Jika Anda mencari contoh-contoh yang menunjukkan bagaimana menggunakan API C, lihatlah di klien ini. 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. Anda dapat menemukan ini pada direktori klien dalam distribusi source MySQL. You can find these in the clients directory in the MySQL source distribution.

Persyaratan Requirements

Pastikan Anda telah menginstal lingkungan pengembangan seperti gcc, pengembangan paket mysql dll Berikut ini adalah daftar merangkum daftar paket untuk mengkompilasi program: 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: program klien MySQL dan shared library mysql : MySQL client programs and shared library
  • mysqlclient: Backlevel MySQL shared libraries (libs lama) mysqlclient : Backlevel MySQL shared libraries (old libs)
  • mysql-devel: File untuk pengembangan aplikasi MySQL (harus ada) mysql-devel : Files for development of MySQL applications (a must have)
  • mysql-server: MySQL server sendiri mysql-server : Mysql server itself
  • gcc, membuat dan lain pembangunan libs: GNU C compiler gcc, make and other development libs : GNU C compiler

Contoh Program C Sample C Program

Mengikuti instruksi harus bekerja pada setiap distro Linux atau komputer UNIX. Following instructions should work on any Linux distro or UNIX computer. Berikut adalah program kecil yang menghubungkan ke mysql dan daftar tabel dari database mysql server (. link download ): Here is the small program that connects to mysql server and list tables from mysql database.( download link ):
/ * Sederhana program C yang terhubung ke server Database MySQL * / /* Simple C program that connects to MySQL Database server*/ 
 # Include  #include  
 # Include  #include  
 main () { main () { 
    MYSQL * conn; MYSQL * conn ; 
    * MYSQL_RES res; MYSQL_RES * res ; 
    MYSQL_ROW baris; MYSQL_ROW row ; 

    Char * server = "localhost"; char * server = "localhost" ; 
    Char * user = "root"; char * user = "root" ; 
    char * password = "PASSWORD"; / * set me * pertama / char * password = "PASSWORD" ; /* set me first */ 
    Char * database = "mysql"; char * database = "mysql" ; 

    conn = mysql_init (NULL); conn = mysql_init ( NULL ); 

    / * Hubungkan ke database * / /* Connect to database */ 
    if (server! mysql_real_connect (conn,, if (! mysql_real_connect ( conn , server , 
          user, password, database, 0, NULL, 0)) { user , password , database , 0 , NULL , 0 )) { 
       fprintf (stderr, "% s \ n", mysql_error (conn)); fprintf ( stderr , "%s \n " , mysql_error ( conn )); 
       exit (1); exit ( 1 ); 
    } } 

    / * Kirim * query SQL / /* send SQL query */ 
    if (mysql_query (conn, "tabel menunjukkan")) { if ( mysql_query ( conn , "show tables" )) { 
       fprintf (stderr, "% s \ n", mysql_error (conn)); fprintf ( stderr , "%s \n " , mysql_error ( conn )); 
       exit (1); exit ( 1 ); 
    } } 

    res = mysql_use_result (conn); res = mysql_use_result ( conn ); 

    / * Nama tabel output * / /* output table name */ 
    printf ("Tabel pada database MySQL mysql: \ n"); printf ( "MySQL Tables in mysql database: \n " ); 
    while ((baris = mysql_fetch_row (res))! = NULL) while (( row = mysql_fetch_row ( res )) != NULL ) 
       printf ("% s \ n", baris [0]); printf ( "%s \n " , row [ 0 ]); 

    / * Tutup koneksi * / /* close connection */ 
    mysql_free_result (res); mysql_free_result ( res ); 
    mysql_close (conn); mysql_close ( conn ); 
 } } 
 

Bagaimana cara mengkompilasi dan link program terhadap libs MySQL? How do I compile and link program against MySQL libs?

MySQL dilengkapi dengan script khusus yang disebut mysql_config. MySQL comes with a special script called mysql_config. Ini memberikan Anda informasi yang berguna untuk mengkompilasi Anda klien MySQL dan menghubungkannya ke server database MySQL. It provides you with useful information for compiling your MySQL client and connecting it to MySQL database server. Anda perlu menggunakan berikut dua pilihan. You need to use following two options.
Pass - libs pilihan - Perpustakaan dan opsi yang diperlukan untuk menghubungkan dengan perpustakaan klien MySQL. Pass --libs option - Libraries and options required to link with the MySQL client library.
$ mysql_config --libs
Output: Output:
-L/usr/lib64/mysql-Lmysqlclient-Lz-lcrypt-lnsl-lm -L/usr/lib64-lssl-lcrypto -L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto 
Pass - CFLAGS opsi - Kompilator flag untuk menemukan menyertakan file dan flag-flag compiler kritis dan mendefinisikan digunakan ketika kompilasi perpustakaan libmysqlclient. 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: Output:
-I/usr/include/mysql-G-pipe-m64-D_GNU_SOURCE-D_FILE_OFFSET_BITS = 64-D_LARGEFILE_SOURCE-fno-ketat-aliasing -I/usr/include/mysql -g -pipe -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing 
Anda harus lulus di atas pilihan untuk gcc compiler yaitu GNU C. You need to pass above option to GNU C compiler ie gcc. Jadi untuk mengkompilasi program di atas, masukkan: So to compile above program, enter:
$ gcc -o output-file $(mysql_config --cflags) mysql-c-api.c $(mysql_config --libs)
Sekarang melaksanakan program: Now execute program:
$ ./output-file
Output: Output:
Tabel MySQL di database mysql: MySQL Tables in mysql database:
columns_priv columns_priv
db db
func func
help_category help_category
help_keyword help_keyword
help_relation help_relation
help_topic help_topic
tuan rumah host
tables_priv tables_priv
time_zone time_zone
time_zone_leap_second time_zone_leap_second
time_zone_name time_zone_name
time_zone_transition time_zone_transition
time_zone_transition_type time_zone_transition_type
pengguna user 



http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html

Reset Your Forgotten Ubuntu Password / Reset Password Ubuntu

Reset Your Forgotten Ubuntu Password / Reset Password Ubuntu

If you’ve ever forgotten your password, you aren’t alone… it’s probably one of the most common tech support problems I’ve encountered over the years. Luckily if you are using Ubuntu they made it incredibly easy to reset your password.
Jika anda mempunyai masalah lupa password, anda tidak sendiri…itu mungkin salah satu hal umum tentang masalah teknis yang saya temui saat ini. Untungnya dengan ubuntu mereka membuat anda mudah mereset password anda.
image32
All it takes is adjusting the boot parameters slightly and typing a command or two, but we’ll walk you through it.
Yang diperlukan hanyalah menyesuaikan parameter boot sedikit dan mengetik satu atau dua perintah, tapi kami akan memandu Anda melalui itu.
Reset Your Ubuntu Password
Reset Password Ubuntu Anda

Reboot your computer, and then as soon as you see the GRUB Loading screen, make sure to hit the ESC key so that you can get to the menu.
Reboot komputer Anda, dan kemudian segera setelah Anda melihat layar GRUB Loading, pastikan untuk menekan tombol ESC sehingga Anda bisa mendapatkan menu sbb
image33
Root Shell – Easy Method
Root Shell – Metode Mudah

If you have the option, you can choose the “recovery mode” item on the menu, usually found right below your default kernel option.
Jika Anda memiliki pilihan, Anda dapat memilih item “recovery mode” pada menu, biasanya ditemukan tepat di bawah opsi kernel default Anda.
image[3]
Then choose “Drop to root shell prompt” from this menu.
Lalu pilih “Drop to root shell prompt” dari menu ini.
image[6]
This should give you a root shell prompt.
Ini akan memberikan Anda sebuah prompt shell root.
Alternate Root Shell Method
If you don’t have the recovery mode option, this is the alternate way to manually edit the grub options to allow for a root shell.
First you’ll want to make sure to choose the regular boot kernel that you use (typically just the default one), and then use the “e” key to choose to edit that boot option.
Alternatif Metode Root Shell
Jika Anda tidak memiliki opsi recovery mode, ini adalah cara alternatif untuk mengedit pilihan grub secara manual untuk memungkinkan shell root.
Pertama Anda akan ingin memastikan untuk memilih boot kernel yang biasa Anda gunakan (default biasanya hanya satu), dan kemudian menggunakan tombol “e” untuk memilih untuk mengedit opsi boot.
image34
Now just hit the down arrow key over to the “kernel” option, and then use the “e” key to switch to edit mode for the kernel option.
Sekarang hanya menekan tombol panah bawah ke pilihan “kernel”, dan kemudian menggunakan tombol “e” untuk beralih ke mode edit untuk opsi kernel.
image35
You’ll first be presented with a screen that looks very similar to this one:
Anda akan terlebih dahulu disajikan dengan layar yang terlihat sangat mirip dengan yang satu ini:
image36
You’ll want to remove the “ro quiet splash” part with the backspace key, and then add this onto the end:
Anda akan menghapus bagian “ro quiet splash” dengan tombol backspace, dan kemudian menambahkan di bagian akhir:
rw init=/bin/bash
image37
Once you hit enter after adjusting the kernel line, you’ll need to use the B key to choose to boot with that option.
Setelah Anda tekan enter setelah baris kernel disesuaikan, Anda harus menggunakan tombol B untuk memilih boot dengan opsi tsb.
image38
At this point the system should boot up very quickly to a command prompt.
Pada poin ini sistem harus boot dengan sangat cepat ke command prompt.
Changing the Actual Password
You can use the following command to reset your password:
Mengubah Password Aktual

Anda dapat menggunakan perintah berikut untuk mereset password Anda:
passwd
For example my username being geek I used this command:
Misalnya username saya adalah geek saya menggunakan perintah ini:
passwd geek
image39
After changing your password, use the following commands to reboot your system. (The sync command makes sure to write out data to the disk before rebooting)
Setelah mengubah password Anda, gunakan perintah berikut untuk me-reboot sistem anda. (perintah sync memastikan untuk menuliskan data ke disk sebelum reboot)
sync
reboot –f
I found that the –f parameter was necessary to get the reboot command to work for some reason. You could always hardware reset instead, but make sure to use the sync command first.
Saya menemukan parameter-f yang diperlukan untuk mendapatkan perintah reboot untuk bekerja karena alasan tertentu. Anda selalu bisa mengatur ulang perangkat keras bukan, tapi pastikan untuk menggunakan perintah sync pertama.
And now you should be able to login without any issues.
Dan sekarang Anda bisa login tanpa masalah apapun.

Ref : http://dodiesatriani.wordpress.com/2009/11/05/reset-your-forgotten-ubuntu-password-reset-password-ubuntu/