1、Ubuntu 安裝MySQL
sudo apt-get install mysql-server
在安裝過程中會提示設置root密碼;
2、使用MySQL
2.1、登錄MySQL
命令格式: mysql -h 主機名 -u 用戶名 –p
-h : 該命令用於指定客戶端所要登錄的MySQL主機名, 登錄當前機器該參數可以省略;
-u : 所要登錄的用戶名;
-p : 告訴服務器將會使用一個密碼來登錄, 如果所要登錄的用戶名密碼為空, 忽略此選項。
在安裝完MySQL之後,使用root登錄:
mysql –u root -p //在本地登錄時可以不用-h選項;
2.2、創建數據庫:
命令格式:create database 數據庫名 [其他選項];
eg: create database samp_db ;
創建數據庫sample_db;可以使用show databases; 查看創建的所有數據庫;
2.3、創建用戶:
grant all on sample_db.* to wangz@local identified by "ying";
表明用戶wangz使用密碼ying只能在本地登錄數據庫sample_db,並可以對sample_db進行所有操作;
grant all on sample_db.* to wangz@'%' identified by "ying";
表明用戶wangz使用密碼ying在任意主機登錄數據庫sample_db,並可以對sample_db進行所有操作;
2.4、選擇操作的數據庫: (兩種方式)
2.4.1、在登錄數據庫時指定:
命令格式: mysql -D 數據庫名 -h 主機名 -u 用戶名 -p
eg:mysql -D sample_db -u wangz -p
2.4.2、登錄後使用use選擇:
命令格式:use 數據庫名; (use後可以不使用;)
2.5、創建數據庫表:
eg: create table students(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
其中,每行代表一列,後面緊跟列的屬性;
2.6、操作數據庫:
2.6.1、插入數據:
Insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
a)、包含所有值
insert into students values(NULL, "wang", "男", 25, "13800000000");
b)、只包含部分值
insert into students (name, sex, age) values("zhang", "男", 24);
2.6.2、查詢數據:
select 列名稱 from 表名稱 [查詢條件];
eg: a)、無條件查詢
select name, age from students;
select * from students;
b)、條件查詢
select 列名稱 from 表名稱 where 條件;
select tel from students where name=”wang”;
2.6.3、更新表中的數據:
update 表名稱 set 列名稱=新值 where 更新條件;
eg: update students set tel=default where id=5;
將id=5的行的tel字段更新為默認值;
2.6.4、刪除表中的數據:
delete from 表名稱 where 刪除條件;
eg: delete from students where id=2;
delete from students; //刪除表中的所有數據:
3、遠程登錄MySQL數據庫失敗問題解決
MySQL安裝時默認只允許本地登錄,如果遠程登錄,mysql -u wangz -h 192.168.1.110 -p
會提示:ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.110' (111)
解決辦法:
修改 /etc/mysql/my.cnf文件,將47行 bind-address = 127.0.0.1 註釋掉;
重啟MySQL服務: sudo service mysql restart
重新遠程登錄,mysql -u wangzhao -h 192.168.1.110 -p
如果還是登錄失敗,提示:ERROR 1045 (28000): Access denied for user 'wangzhao'@'wangz.local' (using password: YES)
這是由於創建用戶是host設置為了local,
可以在本地登錄MySQL,使用select host,user,password from mysql.user; 查看用戶,你會發現用戶wangzhao的host對應的是local
4、使用C語言連接數據庫
4.1、使用到的函數:
4.1.1、初始化一個連接句柄:
MYSQL *mysql_init(MYSQL *);
成功:返回一個指向新分配的鏈接句柄結構的指針
失敗:返回NULL
4.1.2、建立物理連接:
MYSQL *mysql_real_connect(MYSQL *connection, const char *server_host,
const char *sql_user_name, const char *sql_password,
const char *db_name, unsigned int port_number,
const char *unix_socket_name, unsigned int flags );
connection 參數為通過mysql_init函數創建的句柄;
server_host 參數可為主機名或IP值;
sql_user_name 訪問數據庫的用戶名;
sql_password 訪問數據庫的密碼;
4.1.3、斷開連接:
void mysql_close(MYSQL *connection);
4.1.4、執行SQL命令:
int mysql_query(MYSQL *connection, const char *query);
成功:返回0;
失敗:非0;
4.1.5、數據控制:
MYSQL_RES *mysql_store_result(MYSQL *connection);
成功時返回一個指向返回集的指針,否則返回NULL;
my_ulonglong mysql_num_rows(MYSQL_RES *result);
該函數獲取返回集中的行數,若為0表示沒有返回行;
my_ulonglong mysql_num_fields(MYSQL_RES *result);
該函數獲取返回集中的字段數,若為0表示沒有返回;
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
該函數從mysql_store_result返回集中取出一行,當沒有數據或者出錯時返回NULL
4.2、簡單測試:
#include
#include
#include
#include
#define HOST "localhost"
#define USERNAME "root"
#define PASSWORD "wangz"
#define DATABASE "sample_db"
void exe_sql(char* sql);
void query_sql(char* sql);
int main(int argc, char *argv[]) {
char *query = "select * from users;";
char *exe = "insert into users values(4,'li',28,NULL);";
exe_sql(exe); //插入數據
query_sql(query); //查詢數據
return 0;
}
void exe_sql(char* sql)
{
MYSQL my_connection;
int res;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) { //連接成功
printf("數據庫執行exe_sql連接成功!\n");
res = mysql_query(&my_connection, sql); //執行插入語句
if (res) {
printf("Error: mysql_query !\n");
mysql_close(&my_connection); //關閉連接
}
else {
printf("%ld 行受到影響!\n",(long int)mysql_affected_rows(&my_connection)); //mysql_affected_rows()會返回執行sql後影響的行數
mysql_close(&my_connection); //關閉連接
}
}
else
printf("數據庫執行exe_sql連接失敗!\n");
}
void query_sql(char* sql)
{
MYSQL my_connection;
int res;
MYSQL_RES *res_ptr;
MYSQL_FIELD *field;
MYSQL_ROW result_row;
int row, col;
int i, j;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) { //連接成功
printf("數據庫查詢query_sql連接成功!\n");
mysql_query(&my_connection, "set names utf8");
res = mysql_query(&my_connection, sql); //執行查詢語句
if (res) {
printf("Error: mysql_query !\n");
mysql_close(&my_connection);
}
else {
res_ptr = mysql_store_result(&my_connection);
if (res_ptr) {
col = mysql_num_fields(res_ptr);
row = mysql_num_rows(res_ptr) + 1;
printf("查詢到 %ld 行 \n", (long int)row);
for (i = 0; field = mysql_fetch_field(res_ptr); i++)
printf("%-15s", field->name);
printf("\n\n");
for (i = 1; i < row; i++) {
result_row = mysql_fetch_row(res_ptr);
for (j = 0; j < col; j++)
printf("%-15s", result_row[j]);
printf("\n");
}
}
mysql_close(&my_connection);
}
}
}運行結果:
執行前:
執行後:
閱讀更多 有理想的代碼dog 的文章