centos c 连接mysql_CentOS下的C连接Mysql数据库

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

#include

#include

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值