您的位置:首页 > 数据库 > MySQL

MySQL 连接数据库出现问题时的解决方案。

2010-01-02 22:34 1021 查看
yum 安装

#yum install mysql-server

$sudo /etc/init.d/mysqld start

$mysql -uroot

1、下载mysql的二进制包

首先到 http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-max-5.0.27-linux-i686-glibc23.tar.gz/from/pick 下载linux non RPM package --max

选择一个镜像下载(默认情况下下载到Desktop),文件名为mysql-max-5.0.27-linux-i686-glibc23.tar.gz

2、安装mysql

解压mysql文件包

$cd Desktop

$tar xzvf mysql-max-5.0.27-linux-i686-glibc23.tar.gz

解压得到文件夹mysql-.....

将该文件夹移到你想放的路径,如/usr/local

$mv mysql-.... /usr/local

如果你嫌名字太长,可以改名

$cd /usr/local

$mv mysql-..... mysql

下面进入mysql目录,进行配置

$cd mysql

$./configure --prefix=/usr/local //---/usr/local就是你想安装的路径

调用/sbin目录下的两个函数groupadd , useradd 添加用户组和用户名

$/sbin/groupadd mysql //add group

$/sbin/useradd -g mysql mysql //向 mysql 组中添加 mysql 用户

$scripts/mysql_install_db --user=mysql

此处显示错误信息:

Neither host 'zhu.sdut.edu' nor 'localhost' could be looked up with

./bin/resolveip

Please configure the 'hostname' command to return a correct hostname.

If you want to solve this at a later stage, restart this script with

the --force option

解决办法:

cd /etc

vim hosts

将::1改为127.0.0.1 (这里不是自己的IP,要不是这里出了错根本就不会在安装时耗时间)

[root@zhu etc]# cd /usr/local/mysql

[root@zhu mysql]# scripts/mysql_install_db --user=mysql

3、启动 登陆 mysql

$bin/mysqld_safe --user=mysql &

其中&是命令结束符,作用是将一个长时间运行的程序放入后台,我们再利用这个终端作其他事情

$bin/mysql -uroot (-p"PASSWORD") //登录

若显示 mysql> 则表示安装成功

4.查看mysql端口是否已运行

$netstat -nat

若显示 tcp 0 00.0.0.0 3306 0.0.0.0 * List 则表明mysql端口已打开

也可以使用查看端口进程命令

$ps -A

5、修改mysql的密码和设置环境变量

接下来说一下修改密码(添加密码),和设置环境变量

1)修改密码

a) 用mysql的命令mysqladmin修改密码:

$bin/mysqladmin -uroot password '新密码'

如果原来有密码,则用下面的语句:

$bin/mysqladmin -uroot -p原来密码 password '新密码'

b) 也可以进入Mysql后使用mysql命令修改密码

update user set password='123' where user='root' and host='localhost';

来添加或修改密码的

2)增加环境变量

在这里我们每次调用mysql的命令都需要找到其位置,为了在任何位置都能直接输入命令就可执行启动mysql或登录mysql

设置PATH的环境变量

[root@zhu local]# export PATH=$PATH:/usr/local/mysql/bin

查看是否将/home/zhu/bin:/usr/local/mysql/bin添加到环境变量中:

[root@zhu local]# echo $PATH

查看环境变量的值,可通过语句:

$echo $变量名

如这里查看PATH的值(变量前要加符号$)

$echo $PATH

6.linux 下 mysql 测试程序

mysql_init()

调用任何MySQL函数之前,需要在程序中调用该函数,它将初始化MySQL所需的某些全局变量。

mysql_real_connect()

尝试与运行在主机上的MySQL数据库引擎建立连接。在你能够执行需要有效MySQL连接句柄结构的任何其他API函数之前,mysql_real_connect()必须成功完成。

mysql_query()

执行指定为“以Null终结的字符串”的SQL查询。“以Null终结的字符串”的意思是字符串必须包含1条SQL语句,而且不应为语句添加终结分号(‘;’)或“/g”,而在mysql中每条语句都要有终结分号(‘;’)来表示一条语句的结束。

mysql_store_result()


于成功检索了数据的每个查询(SELECT、SHOW、DESCRIBE、EXPLAIN、CHECK
TABLE等),必须调用mysql_store_result()或mysql_use_result()。对于其他查询,不需要调用
mysql_store_result()或mysql_use_result(),但是如果在任何情况下均调用了
mysql_store_result(),它也不会导致任何伤害或性能降低

mysql_fetch_row()

检索结果集的下一行,如果是第一次检索则是检索第一行。当一个操作完成后保存该操作的结果,然后分行显示结果的内容,用的就是这个语句。

7. Linux下调试mysql:::

[root@zhu zhu]# gcc -g mysql.c -I /usr/local/mysql/include/ -L /usr/local/mysql/lib -lmysqlclient -o mysql -lz -lm

成功

因为mysql库文件不属于标准库,需要在编译时手工指定程序中使用的头文件

各参数意义:

-I:指定头文件 的位置/usr/local/mysql/include/

-L:指定附加库的位置/usr/local/mysql/lib

-lmysqlclient:链接mysql的客户端libmysqlclient.so文件,文件的前缀和后缀都是默认的。

-lz:优化编译

-lm:连接数据库

缺少选项-lm 时

# gcc -g mysql.c -I /usr/local/mysql/include/ -L /usr/local/mysql/lib -lmysqlclient -o mysql -lz

错误信息如下:

/usr/local/mysql/lib/libmysqlclient.a(libtaocrypt_la-dh.o):
In function `TaoCrypt::(anonymous
namespace)::DiscreteLogWorkFactor(unsigned int)':

dh.cpp:(.text+0x8c): undefined reference to `pow'

dh.cpp:(.text+0xa6): undefined reference to `log'

dh.cpp:(.text+0xae): undefined reference to `pow'

/usr/local/mysql/lib/libmyibtaocrypt_la-dh.o):
In function
`TaoCrypt::DH::GenerateKeyPsqlclient.a(lair(TaoCrypt::RandomNumberGenerator&,
unsigned char*, unsigned char*)':

dh.cpp:(.text+0x2d1): undefined reference to `pow'

dh.cpp:(.text+0x2ee): undefined reference to `log'

dh.cpp:(.text+0x2f6): undefined reference to `pow'

/usr/local/mysql/lib/libmysqlclient.a(libtaocrypt_la-dh.o):
In function
`TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&,
unsigned char*)':

dh.cpp:(.text+0x488): undefined reference to `pow'

dh.cpp:(.text+0x4a9): undefined reference to `log'

dh.cpp:(.text+0x4b1): undefined reference to `pow'

collect2: ld 返回 1

缺少选项-lz 时

# gcc -g mysql.c -I /usr/local/mysql/include/ -L /usr/local/mysql/lib -lmysqlclient -o mysql -lm

错误信息如下:

/usr/local/mysql/lib/libmysqlclient.a(my_compress.o): In function `my_compress_alloc':

my_compress.c:(.text+0xc5): undefined reference to `compress'

/usr/local/mysql/lib/libmysqlclient.a(my_compress.o): In function `my_uncompress':

my_compress.c:(.text+0x14c): undefined reference to `uncompress'

collect2: ld 返回 1

缺少选项-lmysqlclient 时

[root@zhu zhu]# gcc -g mysql.c -I /usr/local/mysql/include/ -L /usr/local/mysql/lib -o mysql -lz -lm

错误信息如下:

/tmp/ccO70ENe.o: In function `main':

/home/zhu/compress/mysql_linux_Num2/test.c:12: undefined reference to `mysql_init'

/home/zhu/compress/mysql_linux_Num2/test.c:14: undefined reference to `mysql_real_connect'

/home/zhu/compress/mysql_linux_Num2/test.c:16: undefined reference to `mysql_error'

/home/zhu/compress/mysql_linux_Num2/test.c:20: undefined reference to `mysql_query'

/home/zhu/compress/mysql_linux_Num2/test.c:22: undefined reference to `mysql_query'

/home/zhu/compress/mysql_linux_Num2/test.c:24: undefined reference to `mysql_real_connect'

/home/zhu/compress/mysql_linux_Num2/test.c:26: undefined reference to `mysql_error'

/home/zhu/compress/mysql_linux_Num2/test.c:33: undefined reference to `mysql_query'

/home/zhu/compress/mysql_linux_Num2/test.c:34: undefined reference to `mysql_query'

/home/zhu/compress/mysql_linux_Num2/test.c:35: undefined reference to `mysql_query'

/home/zhu/compress/mysql_linux_Num2/test.c:36: undefined reference to `mysql_query'

/home/zhu/compress/mysql_linux_Num2/test.c:37: undefined reference to `mysql_query'

/home/zhu/compress/mysql_linux_Num2/test.c:38: undefined reference to `mysql_store_result'

/home/zhu/compress/mysql_linux_Num2/test.c:39: undefined reference to `mysql_fetch_row'

/home/zhu/compress/mysql_linux_Num2/test.c:46: undefined reference to `mysql_query'

错误1:

程序中有游离的 ‘/200’

解决:

程序中有的标点符号是在中文件状态下输入的,将其改为在英文状态下输入

8. 操作实例

//mysql_linux_Num2

//mysql基本操作

//朱昌志

//2007-3-2

#include <stdio.h>

#include "/usr/local/mysql/include/my_global.h"

#include "/usr/local/mysql/include/mysql.h"

typedef struct _3D_POINT

{

float x,y,z;

}_3D_POINT;

int ReadData(int Point_Num,_3D_POINT point[111])

{

Point_Num=0;

int fscanf_ret=0;

float temppoint[3];

FILE *fp;

char * filename="/home/zhu/compress/mysql_linux_Num2/1.txt"; ///使用‘/‘而非‘//‘

if ((fp=fopen(filename,"r"))==NULL)

printf("can't open the file!");

else

{

while (!feof(fp))

{

fscanf_ret=fscanf(fp,"%f %f %f",&temppoint[0],&temppoint[1],&temppoint[2]);

if (fscanf_ret==0||fscanf_ret==EOF)

break;

point[Point_Num].x=temppoint[0];

point[Point_Num].y=temppoint[1];

point[Point_Num].z=temppoint[2];

Point_Num++;

}

}

return Point_Num;

}

void DatabaseOperation(int Point_Num,_3D_POINT point[111])

{

float a,b,c;

//char *sql=new char[50];

char sql[200];

int pointnum2=0;

MYSQL mysql;

MYSQL_ROW row;

MYSQL_RES *result;

mysql_init (&mysql);

//连接MYSQL数据库

if(!(mysql_real_connect(&mysql,"localhost","root",NULL,"mysql",0,NULL,0)))

{

fprintf(stderr, "Failed to connect to database: Error: %s/n",

mysql_error(&mysql));

}

//创建数据库example

mysql_query(&mysql,"create database example");

//创建新用户

mysql_query(&mysql,"grant all on example.* to zhu@'localhost' identified by '123'");

//连接example数据库

if(!(mysql_real_connect(&mysql,"localhost","zhu","123","example",0,NULL,0)))

{

fprintf(stderr, "Failed to connect to database: Error: %s/n",

mysql_error(&mysql));

}

else

{

//创建一个表并将点插入表中

int j=0;

mysql_query(&mysql,"create table point( point_x float /* unique*/ ,point_y float,point_z float)");

for (j;j<Point_Num;j++) //不能在‘for’ 循环内部初始化

{

a=point[j].x;

b=point[j].y;

c=point[j].z;

sprintf(sql,"insert into point values(%f,%f,%f)",a,b,c);

mysql_query(&mysql,sql);

}

//输出表中点的个数

mysql_query(&mysql,"select count(point_x) from point");

result=mysql_store_result(&mysql);

if((row=mysql_fetch_row(result)))

{

printf("排序前点的个数==>%s/n",row[0]); //因为row是MYSQL_ROW类型的,是字符型,所以此处是%s

}

//输出表中点的数据

mysql_query(&mysql,"select * from point");

result=mysql_store_result(&mysql);

printf("排序前表中的数据==>/n");

while((row=mysql_fetch_row(result)))

{

printf("%s %s %s/n",row[0],row[1],row[2]);

}

}

//按升序选择表中数据

mysql_query(&mysql,"select distinct * from point order by point_x asc");

result=mysql_store_result(&mysql);

//输出表中的数据

FILE *fp;

char *filename="/home/zhu/compress/mysql_linux_Num2/2.txt";

if ((fp=fopen(filename,"w"))==NULL)

{

printf("cant open the file!/n");

}

else

{

printf("/n排序后表中的数据==>/n");

while ((row=mysql_fetch_row(result)))

{

fprintf(fp,"%s %s %s/n",row[0],row[1],row[2]);

printf("%s %s %s/n",row[0],row[1],row[2]);

pointnum2++;

}

printf("/n排序后点的个数是==>%d/n",pointnum2);

}

//mysql_query(&mysql,"DAY()");

//result=mysql_store_result(&mysql);

//row=mysql_fetch_row(result);

//printf("%d",row[0]);

//创建视图

printf("/n输出新建视图myview中的内容==>/n");

mysql_query(&mysql,"create view myview (point_x,point_y,point_z)
as select point_x,point_y,point_z from point where
point_x>'31'");

result=mysql_store_result(&mysql);

mysql_query(&mysql,"select * from myview order by point_x");

result=mysql_store_result(&mysql);

while((row=mysql_fetch_row(result)))

{

printf("%s %s %s/n",row[0],row[1],row[2]);

}

//连接多个数据表

printf("/n连接point和temp_point两个表将他们的内容同时输出内容如下/n");

mysql_query(&mysql,"create table temp_point( point_x float /* unique*/ ,point_y float,point_z float)");

mysql_query(&mysql,"insert into point values(1.0,2.0,3.0)");

mysql_query(&mysql,"insert into point values(4.0,5.0,6.0)");

mysql_query(&mysql,"insert into point values(7.0,8.0,9.0)");

mysql_query(&mysql,"select * from point union select * from temp_point order by point_x");

result=mysql_store_result(&mysql);

while((row=mysql_fetch_row(result)))

{

printf("%s %s %s/n",row[0],row[1],row[2]);

}

//删除表中数据和表

mysql_query(&mysql,"delete from point");

mysql_query(&mysql,"drop table point");

mysql_query(&mysql,"drop database example");

}

int main()

{

int Point_Num=0;

_3D_POINT point[111];

Point_Num=ReadData(Point_Num,point);//从文件中读点并返回点的个数

DatabaseOperation(Point_Num,point);//数据库相关操作

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐