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

Ubuntu下安装mysql和使用

2015-11-04 20:08 561 查看

Ubuntu下安装mysql和使用

Ubuntu下安装mysql和使用

安装mysql的客户端

用户操作

创建数据库

途中遇到的问题

编码

安装mysql的客户端

sudo apt-get install -y mysql-server
sudo apt-get isntall -y mysql-client
sudo apt-get install -y libmysqlclient-dev


查看版本

mysql --version


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

运行mysql

mysql


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

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

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

cat /etc/mysql/debian.cnf


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

mysql -u debian-sys-maint -p


没意外进入到mysql中。

用户操作

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

创建用户可以

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


又或者

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


结果如下

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(这就是数据库名字)中还有很多有用的表。

好了,接着删除用户

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等的权限了。

show grants for mine;   #查看用户权限
grant all on mysql.* to mine;   #赋予所有权限
flush  privileges ;   #命令更新,立即看到结果
show grants for mine; #这是就会发现更新了


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

创建数据库

先看原来有什么

show database;


创建数据库mine

`create database mine;


Ok,然后使用mine数据库

use mine


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

途中遇到的问题

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

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


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

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

mysql>source data.sql;


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

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

alter database mine character set utf8;


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

编码

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

g++  test.cpp -o test -lmysqlclient


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

//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, password, 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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: