系統(tǒng)環(huán)境Ubuntu 12.041、安裝mysql ubuntu下安裝mysql是比較簡單的,直接通過apt-get安裝
- sudo apt-get install mysql-server-5.5
2、登入mysql
- mysql [-h host_name] [-u user_name] [-p password]
其中參數(shù)-h后面要給出連接的數(shù)據(jù)庫的IP地址或者域名、參數(shù)-u后面要給出登錄的用戶名、參數(shù)-p表示登錄的密碼。 有時連接本機(jī)默認(rèn)的Mysql數(shù)據(jù)庫服務(wù)器,則可以直接在命令行中輸入如下簡寫形式(root用戶):
之后,系統(tǒng)會提示你輸入root用戶的密碼 
3、mysql的基本命令
在mysql中,輸入help或者?命令,即可用查看mysql支持的內(nèi)部操作命令。
(1)顯示數(shù)據(jù)庫列表
顯示數(shù)據(jù)庫列表命令比較簡單,直接輸入show databases;即可。

(2)選擇一個數(shù)據(jù)庫
選擇一個數(shù)據(jù)庫比較簡單,使用use dbname,其中dbname為要選擇的數(shù)據(jù)庫名字。比如,這里我們選擇test數(shù)據(jù)庫:

(3)查看一個數(shù)據(jù)庫中的所有表
通過show tables,可以查看一個數(shù)據(jù)庫中所有的數(shù)據(jù)庫表。
查看Mysql表結(jié)構(gòu)的命令,如下:
show columns from 表名;
describe 表名;
desc 表名;
show create table 表名;(查看create腳本)
(4)退出
簡單的命令,quit;
需要注意的是,每個命令后面需要加上分號“;”,因?yàn)榉痔柋硎疽粋€事務(wù)的結(jié)束。
(5) mysqldump工具備份和恢復(fù)
幾個常用用例:
1.導(dǎo)出整個數(shù)據(jù)庫
mysqldump -u 用戶名 -p 數(shù)據(jù)庫名 > 導(dǎo)出的文件名
mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql
2.導(dǎo)出一個表
mysqldump -u 用戶名 -p 數(shù)據(jù)庫名 表名> 導(dǎo)出的文件名
mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql
3.導(dǎo)出一個數(shù)據(jù)庫結(jié)構(gòu)
mysqldump -u wcnc -p -d --add-drop-table smgp_apps_wcnc >/home/dk/wcnc_db.sql
-d 沒有數(shù)據(jù) --add-drop-table 在每個create語句之前增加一個drop table
4.導(dǎo)入數(shù)據(jù)庫
常用source 命令
進(jìn)入mysql數(shù)據(jù)庫控制臺,
如mysql -u root -p
mysql>use 數(shù)據(jù)庫
然后使用source命令,后面參數(shù)為腳本文件(如這里用到的.sql)
mysql>source /home/wcnc_db.sql
4、mysql數(shù)據(jù)庫操作實(shí)例。
(1)創(chuàng)建班級數(shù)據(jù)庫
創(chuàng)建數(shù)據(jù)庫的命令式create databases dbname,其中dbname為數(shù)據(jù)庫名,例如當(dāng)前要創(chuàng)建的數(shù)據(jù)庫名為class,因此需要輸入下面的命令:
- mysq->create database class;

(2)創(chuàng)建學(xué)生信息表
- mysql>use class;
- mysql>create table student (nid INT UNIQUE,name VARCHAR(20),age int);

(3)插入基本數(shù)據(jù)
向已經(jīng)創(chuàng)建好的數(shù)據(jù)表中插入3條基本記錄,SQL語句如下:
- mysql->insert into student values(100, 'Lee', 16);
- mysql->insert into student values(101, 'Tom', 17);
- mysql->insert into student values(102, 'Harry', 15);
(4)查詢?nèi)繑?shù)據(jù)
利用select語句查詢?nèi)坑涗洝?br style="word-wrap: break-word" />
- mysql->select * from student;
(5)刪除其中一條記錄
這里我們假設(shè)Tom同學(xué)轉(zhuǎn)學(xué)了,那么他已經(jīng)不在我們的班級class里面了,所以需要把他從class刪除。
mysql->delete from student where name = 'Tom';
(6)更新其中一條記錄
班級class中還存在一種情況,就是有學(xué)生要改名了,比如Lee要改名為Tony了。具體的sql語句如下:
mysql->update student set name = 'Tony' where name = 'Lee';
(7)刪除數(shù)據(jù)庫
如果學(xué)生畢業(yè),那么我們這個班級就沒有存在的必要了,這是就可以刪除掉這個數(shù)據(jù)庫class了。
刪除的步驟如下:
a、刪除所有的數(shù)據(jù)表,SQL語句為:
mysql->drop table class;
b、刪除數(shù)據(jù)庫,SQL語句為:
mysql->delete database class;
5、Mysql數(shù)據(jù)庫連接之C語言API
首先要安裝一個包libmysql++-dev包,不然編譯代碼的時候會出現(xiàn)“
mysql/mysql.h: No such file or directory”錯誤
sudo apt-get install libmysql++-dev
示例代碼:
- #include<mysql/mysql.h>
- #include<stdio.h>
- int main()
- {
- MYSQL mysql;
- MYSQL_RES *res;
- MYSQL_ROW row;
- char *query = "select * from dbtablename;";
- int t,r;
- mysql_init(&mysql);
- if(!mysql_real_connect(&mysql,"localhost","dbusername",
"dbpassword","dbname",0,NULL,0))
- {
- printf("Error connecting to database:%s\n",mysql_error(&mysql));
- }
- else
- printf("Connected........");
-
- else
- printf("Connected........");
- t=mysql_query(&mysql,query);
- if(t)
- {
- printf("Error making query:%s\n",mysql_error(&mysql));
- }
- else
- {
- printf("Query made ....\n");
- res = mysql_use_result(&mysql);
- if(res)
- {
- for(r=0;r<=mysql_field_count(&mysql);r++)
- {
- row = mysql_fetch_row(res);
- if(row<0) break;
- for(t=0;t<mysql_num_fields(res);t++)
- printf("%s ",row[t]);
- printf("\n");
- }
- }
- mysql_free_result(res);
- }
- mysql_close(&mysql);
- return 0;
- }
編譯之~注意一定要先安裝libmysql++-dev包。編譯指令:gcc c_mysql.c -lmysqlclient -o c_mysql
運(yùn)行./c_mysql