mysql常用的一些命令

來源:互聯網
上載者:User

標籤:mysql 常用命令

一 授權登入

參考

http://blog.csdn.net/andy_yf/article/details/7487519

http://www.2cto.com/database/201303/195876.html 

GRANT ALL PRIVILEGES ON cacti.* TO ‘[email protected]‘localhost‘ IDENTIFIED BY  ‘[email protected]‘;  ##只給cacti這個資料庫授權grant all on *.* to ‘root‘@‘localhost‘ identified by ‘huningfei‘;   ##只允許本地串連資料庫grant all on *.* to ‘root‘@‘%‘identified by ‘password‘;  ##允許任何主機串連資料庫grant all on *.* to [email protected]‘localhost‘  ##dba使用者管理所有資料庫的許可權


二 設定mysql登入密碼

第一種方法:

mysqladmin -uroot password  ‘huningfei‘ (不是登陸mysql之後運行)



第二種方法:(只要是root使用者的密碼全部更改)包括;[email protected]%  [email protected]等

用UPDATE直接編輯user表

    mysql -u root  mysql> use mysql;  mysql> UPDATE user SET Password = PASSWORD(‘newpass‘) WHERE user = ‘root‘;  mysql> FLUSH PRIVILEGES;


三 取消授權並刪除使用者

參考 http://www.cnblogs.com/wanghetao/p/3806888.html 

1如何查看授權的所有使用者

SELECT DISTINCT CONCAT(‘User: ‘‘‘,user,‘‘‘@‘‘‘,host,‘‘‘;‘) AS query FROM mysql.user;


查看資料庫中具體某個使用者的許可權

mysql> show grants for ‘cactiuser‘@‘%‘; 

2 取消授權

MySQL取消許可權和刪除使用者   作為管理員,既然能夠建立使用者和授權,同樣也可以取消授權和刪除使用者。要取消某個使用者的許可權,可以使用REVOKE語句。該語句的文法格式如下: 

Revoke privileges (columns) on what from user ;

 

其中privileges是要取消的許可權,user是要被取消許可權的使用者名稱。  樣本:    下面的代碼實現了取消sss使用者在localhost機器上的所有許可權的功能。 

> revoke all on *.* from [email protected] ;Query OK, 0 rows affected (0.00 sec)

3 刪除使用者

REVOKE語句只能取消使用者的許可權,而不可以刪除使用者。即使取消了所有的許可權,使用者仍然可以串連到伺服器。要想徹底的刪除使用者,必須使用DELETE語句將該使用者的記錄從MySQL資料庫中的user表中刪除。該語句的文法格式如下: 

Delete from user where user = "user_name" and host = "host_name" ; delete from user where user="" and host="localhost";

 

使用DELETE刪除使用者sss,代碼如下: 

mysql> use mysqlDatabase changedmysql> delete from user where user=‘sss‘ and host=‘localhost‘ ;mysql>flush privileges ;Query OK, 1 row affected (0.02 sec)


四 修改mysql的搜尋引擎

1 查看mysql儲存引擎命令,在mysql>提示符下搞入show engines;欄位 Support為:Default表示預設儲存引擎  
2、設定InnoDB為預設引擎:在設定檔my.cnf中的 [mysqld] 下面加入default-storage-engine=INNODB 一句

3、重啟mysql伺服器:mysqladmin -u root -p shutdown或者service mysqld restart 登入mysql資料庫,


五 查看mysql連結的ip數

 1 netstat -an | grep ESTABLISHED |grep 3306 | awk {‘print $5‘}|sed  ‘s/:.*$//g‘


 2 mysql -uroot -p  -e"show processlist\G;"| egrep "Host\:" | awk -F: ‘{ print $2 }‘| sort | uniq -c  ##這個需要輸入mysql的使用者名稱



六 增刪改查

查看資料庫 show databases

查看在現在在哪個資料庫下:select database();

切換資料庫  use dbname

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

查看錶: show tables

查看錶的詳細資料 desc 表名

查看建表的語句  show create table  表名

建立一個資料庫: create database hu;

建立表: create table tb1 (`id` int(4),`name` char(40)) 

表裡插入資料:insert into tb1 values (2,‘hu‘);

刪除一條資料:delete from db1 where name=‘55‘;

更新 update db1.t1 set name=‘aaa‘ where id=1;  
清空表 truncate table db1.t1; 
刪除表 drop table db1.t1; 
刪除資料庫 drop database db1; 
修複表 repair table tb1 ; (discuz.user修複discuz庫裡面的user表)

查看mysql狀態 show status;


本文出自 “漸行漸遠” 部落格,請務必保留此出處http://825536458.blog.51cto.com/4417836/1878134

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.