Mysql的常用操作

來源:互聯網
上載者:User

標籤:linux   mysql

1.查詢:日誌、DB、Tables、Descript


show master logs;    ##查看日誌/usr/local/mysql/data/目錄下;適用於mysql5.5


show databases;      ##查看所有資料庫   


show tables;     ##查看當前資料庫的所有表


use  資料庫名;     ##進入一個資料庫


desc 表名;            ##查看一個表的結構


2.建立,刪除DB、Tables


create database 資料庫名;   ##建立一個新的資料庫


create table 資料庫名.表名(欄位1 類型 ,欄位2 類型 not null ... ,primary key(欄位名));    ##建立表,名稱使用字母、_、數字,不要使用$、#、-等字元


drop table 表名;           ##刪除一個表


drop database 資料庫名;    ##刪除一個資料庫


3.管理表中的記錄:select、insert、update、delete


文法:select 欄位 from 表名 where 運算式;    ##語句最後跟"\G"可方便查看


文法:insert into 資料庫名.表名(欄位1 類型,...) values (欄位1的值,...);


文法:update 資料庫名.表名 set 欄位1=值, ... where 條件運算式


文法:delete from 資料庫名.表名 where 條件運算式


1)mysql的root忘記密碼:


sed -i ‘/ \[mysqld\] /a skip-grant-tables ‘ /etc/my.cnf    ##設定跳過許可權


/etc/init.d/mysqld restart


mysql    ##登入


update mysql.user set password=password("123123") where user=‘root‘;    ##修改root使用者密碼


flush privileges;    ##更新許可權


oy4.許可權管理:grant、show grants、revoke


文法:grant 許可權列表 on 資料庫名.表名 to ‘使用者‘@‘客戶機IP’ identified by ‘密碼’;


文法:show grants for ‘使用者’@‘客戶機IP’;


文法:revoke 許可權列表 on 資料庫名.表名 from ‘使用者’@‘客戶機IP‘;


5.備份與恢複:mysqldump、mysql


1)冷備份:


/etc/init.d/mysqld stop


tar -Jcvf /opt/mysql_bak-$(date +%F).tar.xz /usr/local/mysql/data


恢複:


tar -Jxvf /opt/mysql_bak-*.tar.xz -C /usr/local/mysql/data


2)熱備份:


mysqldump -u使用者名稱 -p密碼 --databases 資料庫名 >備份檔案名    ##備份單個資料庫


mysqldump -u使用者名稱 -p密碼 --opt --all-databases >備份檔案名   ##備份整個資料庫


mysqldump -u使用者名稱 -p密碼 --opt --all-databases --lock-talbes=0 >備份檔案名


恢複:


mysql -u使用者名稱 -p密碼 <備份檔案名



3)在base中操作mysql:


mysql  -u使用者名稱  -p密碼  <<END


語句


END


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.