ubuntu mysql 使用,Ubuntu安装和使用mysql的简单示例

安装MysqL的客户端

---- 来自jb51.cc

sudo apt-get install -y MysqL-server

sudo apt-get isntall -y MysqL-client

sudo apt-get install -y libMysqLclient-dev查看版本

---- 来自jb51.cc

MysqL --version

MysqL Ver 14.14 Distrib 5.5.44,for debian-linux-gnu (i686) using readline 6.3

运行MysqL

---- 来自jb51.cc

MysqL

这样是默认以当前用户运行,不过我这却显示

ERROR 1045 (28000): Access denied for user ‘wang’@’localhost’ (using password: NO)

这说明MysqL中没有这个用户的信息,所以要看回相关的配置文件。(注意using password: NO只是代表没有输入口令,而不是该用户没有口令)

---- 来自jb51.cc

cat /etc/MysqL/debian.cnf

会显示客户端[client]的信息,直接使用[client]节提供的user和password登录MysqL就可以了。

---- 来自jb51.cc

MysqL -u debian-sys-maint -p

没意外进入到MysqL中。

用户操作先熟悉一下,如上进入到MysqL后,一下都是在MysqL中操作。

创建用户可以

---- 来自jb51.cc

INSERT INTO MysqL.user(Host,User,Password) VALUES("localhost","test",password("1234"));

又或者

---- 来自jb51.cc

CREATE USER 'user1';

CREATE USER 'use2'@'localhost';;

CREATE USER 'user3' @'localhost' IDENTIFIED BY '123333';

结果如下

---- 来自jb51.cc

SELECT user,password,host FROM MysqL.user;

| user | host | password |

| use1 | % | |

| use2 | localhost | |

| user3 | localhost | *0166E21E66009700F528CA21179AF9AD81120DA2 |

| test | localhost | *A4B6157319038724E3560894F7F932C8886EBFCF |

查看用户用SELECT user FROM MysqL.user,基本上MysqL(这就是数据库名字)中还有很多有用的表。

好了,接着删除用户

---- 来自jb51.cc

DELETE FROM MysqL.user WHERE user='test' AND host='localhost';

DROP USER 'use1';

DROP USER 'use2'@'localhost';

DROP USER 'user3' @'localhost';

熟悉以后正式开始,创建一个user为mine和password为mine的用户,上述。

接着就是赋予mine能执行select等功能,创建database和table等的权限了。

---- 来自jb51.cc

show grants for mine; #查看用户权限

grant all on MysqL.* to mine; #赋予所有权限

flush privileges ; #命令更新,立即看到结果

show grants for mine; #这是就会发现更新了

当然回收权限可以revoke all on MysqL. from mine;*。这是mine可以干活了。

---- 来自jb51.cc

show database;

创建数据库mine

---- 来自jb51.cc

`create database mine;

Ok,然后使用mine数据库

---- 来自jb51.cc

use mine

至于表的话,简单的很,这里就不mark了。需要注意的是linux大小写敏感,表名是大写,就不能通过小写调用了。(mark)

途中遇到的问题嫌逐句输入麻烦,选择批量输入,方法就是先保存到.sql文件中再导入。我是保存到data.sql中。

---- 来自jb51.cc

MysqL -D mine -u mine -p < data.sql

将data.sql导入到mine数据库中。

又或者在进入以后导入(已进入数据库mine中),即

---- 来自jb51.cc

MysqL>source data.sql;

中文不能当缺省值,恩,和不能插入中文差不多,更详细的可百度MysqL正常插入并显示中文数据需满足的条件。

不过我这修改字符集(mine为数据库名,可替换)也就ok了:)。

---- 来自jb51.cc

alter database mine character set utf8;

也可以在创建时,即CREATE DATABASE mine DEFAULT CHARACTER SET=UTF8;。

编码MysqL可以其他语言调用,如Java的JDBC。因为是Linux嘛,这里说说c调用,值得注意的是编译时需要链接MysqL的库,即添加 -lmsqlclient 这个参数

---- 来自jb51.cc

g++ test.cpp -o test -lMysqLclient

代码参考Howto: Connect MysqL server using C program API under Linux or UNIX

---- 来自jb51.cc

//sample -- test.c

#include //apt安装的这,其他的具体分析

#include

#include

#include

static void output_error(MysqL * MysqL);

int main(int argc,char* argv[]) {

MysqL MysqL;

MysqL_RES * result;

MysqL_ROW row;

MysqL_FIELD * fields;

const char* host = "localhost";

const char* user = "mine";

const char* password = "mine";

const char* database = "mine";

const int port = 3306;

const char* socket = NULL;

const int flag = 0;

const char* sql ;

int num_fields;

unsigned long * lengths;

int i;

//initialize the database

if(!MysqL_init(&MysqL) ) {

output_error(&MysqL);

}

printf("MysqL initialized successfully ! \n");

//connect to the database;

if(!MysqL_real_connect(&MysqL,host,user,database,port,socket,flag)) {

output_error(&MysqL);

}

printf("MysqL connect successfully! \n");

printf("\n\n\nthe content of the table data in the database mine\n");

printf("-----------------------------------------------------------\n");

//do the select query on the database;

sql = "select * from data" ;

//printf("%d : %d/n",sizeof(sql),strlen(sql)); // 4:18 sizeof(sql):the size of point --(4); strlen(sql):

if( MysqL_real_query(&MysqL,sql,strlen(sql)) ){

output_error(&MysqL);

}

//fetch the the result set of the query!

result = MysqL_store_result(&MysqL);

if(result) {

fields = MysqL_fetch_fields(result); // fetch the struct of result

num_fields = MysqL_num_fields(result); // fetch the number of result fields;

//lengths = MysqL_fetch_lengths(result);

for(i=0; i

printf("%s\t",fields[i].name );

}

printf("\n");

while(row = MysqL_fetch_row(result)) {

for(i=0; i

printf("%s \t",row[i]);

}

printf("\n");

}

//release the result of set for release the memory

MysqL_free_result(result);

}

else {

output_error(&MysqL);

}

printf("\n\n-----------------------------------------------------------\n");

//close the connetion to the database

MysqL_close(&MysqL);

return 0;

}

static void output_error(MysqL * MysqL) {

fprintf(stderr,"errorno: %d \n",MysqL_errno(MysqL) );

fprintf(stderr,"error info: %s\n",MysqL_error(MysqL) );

exit(1);

}

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值