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!