这篇文章主要为大家详细介绍了Ubuntu安装和使用mysql的简单示例,具有一定的参考价值,可以用来参考一下。
---- 来自www.q1010.com
sudo apt-get install -y mysql-server
sudo apt-get isntall -y mysql-client
sudo apt-get install -y libmysqlclient-dev
查看版本
---- 来自www.q1010.com
mysql --version
---- 来自www.q1010.com
mysql
---- 来自www.q1010.com
cat /etc/mysql/debian.cnf
---- 来自www.q1010.com
mysql -u debian-sys-maint -p
---- 来自www.q1010.com
INSERT INTO mysql.user(Host,User,Password) VALUES("localhost","test",password("1234"));
---- 来自www.q1010.com
CREATE USER 'user1';
CREATE USER 'use2'@'localhost';;
CREATE USER 'user3' @'localhost' IDENTIFIED BY '123333';
---- 来自www.q1010.com
SELECT user,password,host FROM mysql.user;
---- 来自www.q1010.com
DELETE FROM mysql.user WHERE user='test' AND host='localhost';
DROP USER 'use1';
DROP USER 'use2'@'localhost';
DROP USER 'user3' @'localhost';
---- 来自www.q1010.com
show grants for mine; #查看用户权限
grant all on mysql.* to mine; #赋予所有权限
flush privileges ; #命令更新,立即看到结果
show grants for mine; #这是就会发现更新了
---- 来自www.q1010.com
show database;
---- 来自www.q1010.com
`create database mine;
---- 来自www.q1010.com
use mine
---- 来自www.q1010.com
mysql -D mine -u mine -p < data.sql
---- 来自www.q1010.com
mysql>source data.sql;
---- 来自www.q1010.com
alter database mine character set utf8;
---- 来自www.q1010.com
g++ test.cpp -o test -lmysqlclient
---- 来自www.q1010.com
//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);
}
本文来自:http://www.q1010.com/177/2394-0.html
注:关于Ubuntu安装和使用mysql的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:Ubuntu
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。