mysql常用語句


mysql常用語句

> show databases;                    (查看當前存在的數據庫)
> use mysql;                         (切換到數據庫mysql)
> show tables;                       (查看數據庫中的表信息)
> describe user;                     (顯示user表的信息)
> create database auth               (創建新的數據庫auth)

> create database auth default character set utf8 collate utf8_general_ci;  (指定字符集爲utf8,否則中文會亂碼)

> use auth;
> create table users (user_name char(30) not null,user_passwd char(20) not null default

'123456',primary key (user_name));       (建名爲users的表)
> drop table auth.users;                        (刪除數據庫auth中的users表)
> drop database auth;                                  (刪除數據庫auth)
> insert into auth.users(user_name,user_passwd) values('zhangsan',encrypt('123456'));            

       (在表users中插入用戶名zhangsan和密碼123456)
> select * from auth.users;                     (查看users中的數據記錄)
> update auth.users set user_passwd=encrypt('654321') where user_name='zhangsan';                

            (更改用戶的密碼:密文)
> update mysql.user set password=password('123456') where user='root';(明文)


> alter table aa drop bb; (刪除aa表中的bb字段)

> alter table aa add bb int not null; (爲aa表添加bb字段,int類型,非空)

> alter table aa modify bb varchar(100); (修改aa表的bb字段類型爲varchar,100字節)



> flush privileges;                (刷新用戶信息)
> delete from auth.users where user_name='zhangsan'; (刪數據庫auth中的表users裏的用戶zhangsan)
> delete from mysql.user where user_name user=' ';  (刪除數據庫mysql中的user表裏的空用戶)
> grant all on auth.* to admin1@'localhost' identified by '123456';  (授權用戶admin1,允許其從本

機連接到mysql服務器,對auth數據庫的所有表具有完全權限)
> grant select on auth.* to admin2@'192.168.0.0/24' identified by '123456';(授權admin2,允許其從

網段192.168.0.0/24中訪問mysql服務器,可以查詢auth庫中的所有表)
> grant select,insert on auth.* to admin3@'%.benet.com' identified by '1234';(授權用戶admin3,允

許其從benet.com域內的任何主機訪問mysql服務器,對auth庫中的所有表具有查詢、插入的權限)
> show grants for root@'localhost'; (查看用戶root從本機連接到mysql服務器時的權限)
> show grants for admin3@'%.benet.com'; (查看用戶admin3從benet.com域內的客戶機訪問mysql服務器時

的權限)
> revoke all on auth.* from admin3@'%.benet.com'; (撤銷用戶admin3從benet.com域內訪問數據庫auth的

所有權限)




查看數據庫上設置的賬號及對應ip訪問(上面grant設的賬號及對應ip都可看到):

> use mysql;

> select * from user;   


給普通賬號備份的權限:

> grant select,reload,lock tables on *.* to xxxx@'xxxxx' identified by 'xxxxxx';



> exit   或   quit            (退出)

# mysqldump - u root - p auth > mysql-auth.sql   密碼  (備份整個auth數據庫)

# mysqldump - u root - p'123456' auth > mysql-auth.sql  (帶密碼備auth數據庫,做自動備份時有用)

# mysqldump - u root - p mysql user > mysql_user.sql 密碼  (備份數據庫mysql中的user表)

# mysqldump - u root - p --all-databases > mysql-all.sql  密碼 (備份所有數據庫的內容)
# mysql - u root - p < mysql-all.sql  (恢復備份文件mysql-all.sql)
# mysql - u root - p auth < mysql-auth.sql  (恢復單個數據庫時需指定目標數據庫名稱) 

# mysql - u root - p mysql < mysql_user.sql 密碼  (恢復數據庫mysql的user表,恢復單個表時只需指定庫名即可)


修改表名:

> rename table aa to bb;


修改表字段名:


alter table 表名 change 舊字段名 新字段名  字段類型


> alter table aa change bb cc varchar(50);



不進入mysql而執行mysql語句(可以是任何sql語句):

 

# mysql -u root -p'123456' -e "show databases;"

   

# mysql -u root -p'123456' -e "create database aa;"


取消mysql的大小區分:


# vi /etc/mysql/my.cnf


lower_case_table_names=1  (0爲區分,1爲不區分,默認爲0)

:wq


# service mysql restart




 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章