Mysql基本操作總結

來源:互聯網
上載者:User

標籤:

資料庫的基本操作

1.建立資料庫

# create database xxx;

2.查看資料庫

# show databases;

3.刪除資料庫

# drop database xxx;

4.使用/切換資料庫

# use xxx;

 

表的基本操作
1.查看資料庫中所有的表

# show tables;

2.建立表

# create table xxx;

3.查看錶結構

# desc xxx;

4.查看錶詳細結構

# show create table xxx;

5.刪除表

# drop table xxx;

6.修改表名

# alter table xxx rename yyy;

7.修改欄位的資料類型

# alter table 表名 modify 屬性名稱 新資料類型; (注意外鍵不能改)# alter table B modify user_name char(50);

8.修改欄位名與資料類型

# alter table 表名 change 舊屬性名稱 新屬性名稱 資料類型;# alter table GG change user_name your_name char(50);

9.增加欄位

# alter table 表明 add 新欄位名 資料類型
# alter table GG add phone varchar(20);# alter table GG add age int(4) not null;# alter table GG add address varchar(30) not null after phone;

10.刪除欄位

# alter table GG drop age;

11.刪除某條記錄

# delete from 表名 where 條件# delete from 表明                 //刪除所有記錄

12.修改表的儲存引擎

# alter table A engine=MyISAM;

13.刪除表的外鍵約束

# alter table 表名 drop foreign key 外鍵別名# alter table yy2 drop foreign key y_fk;

14.刪除表

# drop table xxx;刪除父表:先刪除所有關聯子表的外鍵,再刪除父表

 

使用者登入與管理相關操作

1. 建立使用者

-> create user ‘test1‘@‘localhost‘identified by ‘123456‘;-> create user ‘test2‘@‘localhost‘identified by ‘PASSWORD(123456)‘;-> insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject)values(‘hostname‘,‘test3‘,PASSWORD(‘123456‘),‘‘,‘‘,‘‘);

2. 查看使用者資訊

-> select Host,User,Password from mysql.user where User=‘test1‘ or User=‘test2‘ or User=‘test3‘;+-----------+-------+-------------------------------------------+| Host | User | Password |+-----------+-------+-------------------------------------------+| localhost | test1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || localhost | test2 | *A4734F92867FE18E0DD415197D5C33D2E705F0DC || hostname | test3 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |+-----------+-------+-------------------------------------------+

3. 刪除使用者

-> drop user ‘test2‘@‘localhost‘;-> delete from mysql.user where Host=‘localhost‘ and User=‘test3‘;

4. 修改root使用者密碼

# mysqladmin -u root -p password ‘新密碼‘  Enter password:                   --->在這裡要輸入正確的舊密碼-> update mysql.user set Password=PASSWORD(‘新密碼‘) where User=‘root‘ and Host=‘localhost‘;

5. 修改普通使用者密碼

# mysql -u test1 -p-> set password=PASSWORD(‘aixocm‘);-> set password for ‘test1‘@‘localhost‘=PASSWORD(‘aixocm‘);

6. 解決忘記root使用者密碼問題

# /etc/init.d/mysqld start --skip-grant-tables
# mysql -u root -p --->這樣可以無密碼登陸進去了-> update mysql.user set Password
=PASSWORD(‘新密碼‘) where User=‘root‘ and Host=‘localhost‘;

 

使用者權限管理操作

1. 添加許可權

grant 許可權on 資料庫名.表名to ‘username‘@‘Host‘
//建立使用者並增加許可權grant select,update-> on *.* --->對所有資料庫的所有表-> to ‘test5‘@‘localhost‘ identified by ‘aixocm‘ -> with grant option;grant delete -> on *.*-> to ‘test5‘@‘localhost‘ -> with grant option;

2. 取消許可權

revoke delete-> on *.*-> from ‘test5‘@‘localhost‘;
//取消所有許可權revoke all privileges,grant option from ‘test5‘@‘localhost‘;

3. 查看許可權

-> select User,Host,Select_priv,Update_priv,Delete_priv from mysql.user where User=‘test5‘;+-------+-----------+-------------+-------------+-------------+| User | Host | Select_priv | Update_priv | Delete_priv |+-------+-----------+-------------+-------------+-------------+| test5 | localhost |    Y  |     Y       |      Y      |+-------+-----------+-------------+-------------+-------------+-> show grants for ‘root‘@‘localhost‘\G;*************************** 1. row ***************************Grants for [email protected]: GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*5DBA547161E5D9C6508C3C6CF5D1B26940E3BC13‘ WITH GRANT OPTION*************************** 2. row ***************************Grants for [email protected]: GRANT PROXY ON ‘‘@‘‘ TO ‘root‘@‘localhost‘ WITH GRANT OPTION

4.重新整理(載入)許可權

-> flush privileges;

 

 

儲存引擎相關操作
1.查看資料庫支援的儲存引擎

# show engines;# show engines\G;Engine:名稱Support:是否支援(YES/DEFAULT)Comment:說明Transaction:是否支援事務XA:是否支援分布式交易處理的XA規範savapoint:是否支援儲存點

2.查看儲存引擎詳細資料

# show engine innodb status\G;

3.查看預設儲存引擎 

# show variables like ‘storage_engine‘;

4.修改預設儲存引擎

# vim /etc/my.cnf(設定檔)default-storage-engine=xxx

 

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.