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<MysqL/MysqL.h> //apt安装的这,其他的具体分析
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
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<num_fields; i++) {
            printf("%s\t",fields[i].name );
        }
        printf("\n");
        while(row = MysqL_fetch_row(result)) {
            for(i=0; i<num_fields; 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);
}
原文链接:https://www.f2er.com/mysql/530275.html

猜你在找的MySQL相关文章