CentOS下的C串連MySQL資料庫

來源:互聯網
上載者:User

1、修改密碼

因為預設是沒有密碼的為了安全先修改密碼,方法是:

先停止mysql

/etc/init.d/mysql stop

--  安全模式啟動
[root@mysql var]#mysqld_safe --skip-grant-tables &
[1] 10912
[root@mysql var]# 110407 17:39:28 mysqld_safe Logging to '/usr/local/mysql/var//mysql.chinascopefinanical.com.err'.
110407 17:39:29 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var/

[root@mysql var]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.1.41-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Database changed

mysql> update user set password=password("yourpasswd) where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

 


完成上面的操作之後,/etc/init.d/mysqld restart重啟mysql

 


完成上面的操作之後密碼就設定好了。

 


2、安裝支援庫

 


root

鍵入yum search mysql

出現

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.ta139.com
 * extras: mirrors.ta139.com
 * updates: mirrors.ta139.com
================================ Matched: mysql ================================
mod_auth_mysql.i386 : 使用 MySQL 資料庫的 Apache 全球資訊網伺服器的基礎驗證。
qt-MySQL.i386 : 用於 Qt 的 SQL 類別的 MySQL 驅動程式。
MySQL-python.i386 : 一個到 MySQL 的介面
apr-util-mysql.i386 : APR utility library MySQL DBD driver
bytefx-data-mysql.i386 : MySQL database connectivity for Mono
freeradius-mysql.i386 : freeradius 的 MySQL 綁定
freeradius2-mysql.i386 : MySQL support for freeradius
libdbi-dbd-mysql.i386 : libdbi 的 MySQL 外掛程式
mysql.i386 : MySQL client programs and shared libraries
mysql-bench.i386 : MySQL benchmark scripts and data
mysql-connector-odbc.i386 : MySQL 的 ODBC 驅動程式。
mysql-devel.i386 : Files for development of MySQL applications

mysql-server.i386 : The MySQL server and related files
mysql-test.i386 : The test suite distributed with MySQL
pdns-backend-mysql.i386 : MySQL backend for pdns
perl-DBD-MySQL.i386 : perl 的 MySQL 介面
perl-DBD-Pg.i386 : Perl 的 PostgresSQL 介面
php-mysql.i386 : 一個用於使用 MySQL 資料庫的 PHP 程式的模組。
php-pdo.i386 : A database access abstraction module for PHP applications
php-pear-MDB2-Driver-mysql.noarch : MySQL MDB2 driver
php53-mysql.i386 : A module for PHP applications that use MySQL databases
php53-pdo.i386 : A database access abstraction module for PHP applications
qt4-mysql.i386 : MySQL drivers for Qt's SQL classes
rsyslog.i386 : Enhanced system logging and kernel message trapping daemon
rsyslog-mysql.i386 : MySQL support for rsyslog


unixODBC.i386 : 一個用於 Linux 的完整的 ODBC 驅動程式管理器。

紅色部分就是我們要的:
yum install mysql-devel-i386 開始安裝

3、編寫程式mysql.c


#include <stdlib.h>
#include <stdio.h>
#include <mysql/mysql.h>
MYSQL *conn_ptr;

int main(void){
 
 conn_ptr = mysql_init(NULL);
 if (!conn_ptr)
 {
  fprintf(stderr,"mysql_init failed!\n");
  return (-1);
 }
 
 conn_ptr = mysql_real_connect(conn_ptr,"127.0.0.1","root","","mysql",3306,0,NULL);
 if (conn_ptr)
 {
  printf ("connection succeed!\n");
 }
 else{
  printf ("connection failed!\n");
  return (-2);
 }
 
 mysql_close(conn_ptr);
 printf ("connection close!\n");
 
 return 0;

}

4、編譯器
gcc -o mydbcon -I /usr/include/mysql/ -L /usr/lib/mysql -lmysqlclient mysql.c

5、執行程式
./mydbcon

connection succeed!
connection close!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.