在幸福中痛苦的掙扎

          專注于效率 專注于價值
           
          書法.JAVA BLOG

          mysql 常用操作命令

          有些時候我們不得不去用命令行操作mysql數(shù)據(jù)庫,下面是筆者總結(jié)的常見操作命令:

          1.連接
          ? 命令:? mysql -h主機地址 -u用戶名 -p密碼
          ? 本地:連接?? cmd進入mysql/bin之后, 輸入mysql -uroot -p然后回車 會提示你輸入密碼 接著回車即可(如果密碼為空);
          ? 遠程連接:假設(shè)遠程主機的IP為:222.222.212.212,用戶名為root,密碼為abcdef。則鍵入以下命令:
          ???? mysql -h222.222.212.212 ?-uroot? -pabcdef

          2.對數(shù)據(jù)庫和表的操作
          ???
          ???查看用戶擁有權(quán)限的數(shù)據(jù)庫?:????? show databases;(注意分號)
          ?
          ?? 創(chuàng)建數(shù)據(jù)庫test:? create database testdb;

          ?? 切換到testdb數(shù)據(jù)庫:?????? use testdb;

          ?? 刪除數(shù)據(jù)庫testdb: drop database?testdb;

          ?? 查看當(dāng)前數(shù)據(jù)庫中有權(quán)限的表: show tables;

          ?? 創(chuàng)建表s_position,department,,depart_pos,testtable:
          ???? create table s_position
          ???? (
          ???????? id int not null auto_increment,
          ???????? name varchar(20) not null default? '經(jīng)理',????????? #設(shè)定默認值
          ???????? description varchar(100),
          ???????? primary key PK_positon (id)?????????????????????????? ?#設(shè)定主鍵
          ???? );????
          ???? create table department
          ???? (
          ???????? id int not null auto_increment,
          ???????? name varchar(20) not null default '系統(tǒng)部',????????? #設(shè)定默認值
          ???????? description varchar(100),
          ???????? primary key PK_department (id)??????????????????????? #設(shè)定主鍵
          ???? );
          ???? create table depart_pos
          ???? (
          ???????? department_id int not null,
          ???????? position_id int not null,
          ???????? primary key PK_depart_pos (department_id,position_id)? #設(shè)定復(fù)和主鍵
          ???? );
          ???? create table testtable
          ???? (
          ???????? id int not null auto_increment primary key,???? #設(shè)定主鍵
          ???????? name varchar(20) not null default '無名氏',? #設(shè)定默認值
          ???????? department_id int not null,
          ???????? position_id int not null,
          ???????? unique (department_id,position_id)????????????? #設(shè)定唯一值
          ???? );

          ?? 查看表testtable的結(jié)構(gòu):?? desc testtable;

          ?? 刪除表testtable:?????
          ??????? drop table testtable;???????

          ? 修改表結(jié)構(gòu) 操作數(shù)據(jù)(增,刪,改,查)
          ???.....(同sql語句,略)

          ?3. 備份和恢復(fù)
          ?? 對數(shù)據(jù)的備份和恢復(fù)命令,在網(wǎng)上有許多種版本,經(jīng)筆者親身測驗,下面的命令是可行方法之一:
          ???
          ??? 備份數(shù)據(jù)庫testdb:??
          ??? 本地:mysqldump -uroot? -pabcdef testdb>e:testdb.sql?
          ??? 遠程:mysqldump -h222.222.212.212 -uroot? -pabcdef testdb>e:testdb.sql?
          ???這里假設(shè)數(shù)據(jù)庫的用戶名和密碼分別是root和abcdef, 導(dǎo)出到額盤根目錄下,得到的testdb.sql是一個sql腳本,不包括建庫的語句,所以你需要手工創(chuàng)建數(shù)據(jù)庫下次才可以導(dǎo)入 恢復(fù)數(shù)據(jù)庫testdb;
          ??????
          ???? 恢復(fù)數(shù)據(jù)庫testdb:??????首先 需要創(chuàng)建一個空庫testdb(原因如上);
          ???? 命令:??
          ???? 本地:mysql -uroot -pabcdef? testdb<e:testdb.sql????
          ???? 遠程:mysql -h222.222.212.212? -uroot -pabcdef? testdb<e:testdb.sql????
          ???? 后面e:testdb.db代表本地sql文件路徑,如果mysql安裝在本地可以把sql文件考到mysql/bin的安裝目錄下,就不用寫路徑了.


          注:在網(wǎng)上有些朋友說,mysql的安裝目錄必須為默認目錄(即c:/mysql),否則會出問題,筆者也測驗了一下在安裝d盤的效果,結(jié)果暫時沒有發(fā)現(xiàn)什么問題.,這說明有時候命令執(zhí)行異常可能和安裝目錄無關(guān).^_^

          posted on 2006-11-24 17:41 書法 閱讀(509) 評論(1)  編輯  收藏 所屬分類: 數(shù)據(jù)庫

          Feedback

          # re: mysql 常用操作命令 2006-12-10 16:44 quto

          頂一下  回復(fù)  更多評論   



          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          My Links

          Blog Stats

          News

          常用鏈接

          留言簿(4)

          隨筆分類(6)

          隨筆檔案(6)

          文章檔案(1)

          收藏夾(4)

          友情鏈接

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜


          copyright@2006 書法(soft)版權(quán)所有.
          E_mail:posoft@126.com     點     點我找我
          主站蜘蛛池模板: 海盐县| 中卫市| 霍林郭勒市| 岚皋县| 荆门市| 聂荣县| 西充县| 康马县| 德江县| 庄浪县| 犍为县| 夹江县| 公安县| 镇平县| 六盘水市| 横峰县| 淮北市| 泾阳县| 五河县| 汉源县| 镇江市| 定结县| 海林市| 眉山市| 行唐县| 泰顺县| 六盘水市| 武强县| 慈利县| 文昌市| 南郑县| 洛川县| 阿合奇县| 榆林市| 马关县| 河池市| 临邑县| 长春市| 读书| 阿拉尔市| 潞西市|