mysql 增查刪改,備份恢複,授權等基礎常用命令

來源:互聯網
上載者:User

標籤:mysql 增查刪改   備份恢複   授權等基礎常用命令   

增 建立庫,表

查 查看有哪些表,哪些庫

刪 刪除庫,刪除表

改 修改表資料


mysql 登入   mysql -uroot -p(跟密碼)mysql 命令大部分都是以;結尾


增   create   (da1 庫名 ta1 表名)

建立庫 create database da1;     

建立表 create table ta1 (`id` int(4), `name` char(40)); 

         

 id/第1個欄位名字   int/類型 /整形4位   第2個是name    類型是char  字串最多40個 (也可以指定 ENGINE=MyISAM DEEAULT CHARSET=gbk)   

create table t1 (`id` int(4), `name` char(40)) ENGINE=MyISAM DEEAULT CHARSET=gbk;    (為防止亂碼,最好指定字元集)


查看都有哪些庫  show databases;

查看某個庫的表 use da1; show tables;

查看錶的欄位   desc ta1;  

查看建表語句    show create table tb\G; 

查看資料庫版本 select version(); 

查看mysql狀態 show status;

查看mysql隊列 show processlist; 

cat .mysql_history    查看mysql命令曆史,每次退出後才會儲存退出前的命令曆史


刪 

刪除資料庫 drop database da1; 

刪除表 drop table da1.ta1;

清空整個表(清理資料,表還在) truncate table ta1; 


插入表     insert ta2(id,name)values(1,‘zhangsan‘)

updata ta1 set id=3 where name=‘2222‘;   

          tab1中的name是2222的id替換成3   select * from tab1  查看

update ta1 set name=‘aaa‘ where id=1;

          tab1中的id是1的name替換成aaa   select * from tab1  查看

刪除一行資料   delete from ta1 where name=‘1’   select * from tab1查看

          刪除tab1表中name等於1的這一行


mysqlDatabase Backup

備份 mysqldump -uroot -p da1 >1.sql 

恢複 mysql -uroot -p da1 <1.sql      

只備份一個表  mysqldump -uroot -p da1 ta1 > 2.sql

備份時指定字元集 mysqldump -uroot -p --default-character-set=utf8  da1 >1.sql

恢複也指定字元集 mysql -uroot -p --default-character-set=utf8  da1  < 1.sql


授權=建立使用者並授權  

grant all on *.* to user1 identified by ‘123456‘; 

        

    on    後面跟的是指定哪一個庫或者對應那一個表

    all    代表所有許可權,後面跟的是指定

    user1    使用者       by  後面跟密碼

select * from mysql.user where user=‘user1’\G;

                 查看使用者user1的mysql庫的user表的欄位

grant all on da1.* to ‘user2‘@‘192.168.239.140‘ identified by ‘123456‘; 

    ‘user2‘@‘192.168.239.140‘    指定給使用者user2,並指定來源ip192.168.239.140可使用  

grant all on da1.* to ‘user3‘@‘%‘ identified by ‘123456‘;

     (% 代表所有的主機)

更改密碼     UPDATE mysql.user SET password=PASSWORD("newpwd") WHERE user=‘username‘ ;   


其他命令

當前是哪個使用者  select user();

當前庫 select database();

查詢行   select count(*) from

       select count(*) from mysql.user;   查詢mysql.user有幾行

修複 repair table     最後面顯示ok 修複成功

          例  repair table pre_forum_attachtype;    修複 pre_forum_attachtype 

 這個是藉助/data/mysql/da1下的pre_forum_faq.frm和pre_forum_faq.MYD兩個指定檔案來恢複pre_forum_faq.MYI


本文出自 “linux系統學習擴充” 部落格,請務必保留此出處http://zhangxiaoxiong.blog.51cto.com/11657691/1784953

mysql 增查刪改,備份恢複,授權等基礎常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.