MySQL数据库入门———常用基础命令
mysql 連接數(shù)據(jù)庫命令:
MySQL 連接本地數(shù)據(jù)庫,用戶名為“root”,密碼“123”(注意:“-p”和“123” 之間不能有空格)
mysql -h localhost -u root -p123MySQL 連接遠程數(shù)據(jù)庫(192.168.0.201),端口“3306”,用戶名為“root”,密碼“123”
mysql -h 172.16.16.45 -P 3306 -u root -p123MySQL 連接本地數(shù)據(jù)庫,用戶名為“root”,隱藏密碼
mysql -h localhost -u root -pEnter password:MySQL 連接本地數(shù)據(jù)庫,用戶名為“root”,指定所連接的數(shù)據(jù)庫為“test”
mysql -h localhost -u root -p123 -D test 查看版本 status;?
創(chuàng)建、刪除、查看數(shù)據(jù)庫
create database test_data;show databases like "test%";?
創(chuàng)建gbk字符集的數(shù)據(jù)庫
GBK:? create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;?? UTF8: create?database test2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;? create?database?test2?DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; show create database test2;刪除數(shù)據(jù)庫
drop database test_data;show databases;
切換到數(shù)據(jù)庫進行操作
use test_gbk;查看當前連接的數(shù)據(jù)庫
select database();查看當前連接數(shù)據(jù)庫的用戶
select user();?
創(chuàng)建用戶、授權、收回權限
當數(shù)據(jù)庫創(chuàng)建完成后,就需要創(chuàng)建用戶,以供需要連接數(shù)據(jù)庫的人員使用與操作數(shù)據(jù)庫,不可能人人使用root登陸,所以權限設置也是很重要的
#創(chuàng)建用戶并all權限給在test_gbk庫所有表,密碼‘123456’ grant all on test_gbk.* to 'testuser'@'localhost' identified by '123456';#刷新權限,使權限生效
flush privileges;
#查看用戶有哪些權限
show grants for 'testuser'@'localhost';
收回權限
revoke insert,update,select,delete on test_gbk.* from 'testuser'@'localhost';?
生產(chǎn)環(huán)境:只允許本機連接
開發(fā)環(huán)境:
新建用戶 grant usage on *.* to 'peony'@'%' identified by 'peony_123' with grant option;設置用戶權限 grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON *.* TO 'peony'@'%' IDENTIFIED BY 'peony_123';賦權給用戶遠程權限 grant all privileges on *.* to 'peony'@'%' identified by 'peony_123' with grant option; # 修改數(shù)據(jù)庫:ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
# 修改表: ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# 修改表字段:
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
?
把庫表的GBK字符集修改為UTF8
alter database test default character set utf8;alter table test default character set utf8;?
把id列設置為主鍵,在Name字段上創(chuàng)建普通索引
alter table test add primary key(id);create index mggindex on test(name(16));?
查看創(chuàng)建的索引及索引類型等信息
show index from test;show create table test\G#下面的命令也可以查看索引類型 show keys from test\G?
轉載于:https://www.cnblogs.com/alter888/p/11127648.html
總結
以上是生活随笔為你收集整理的MySQL数据库入门———常用基础命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bios无法设置u盘启动怎么办 解决bi
- 下一篇: SQL Server 6.5 如何升级到