當前位置:
首頁 >
·MySQL数据库管理(SQL操作命令,解决忘记密码,设置用户权限)
發(fā)布時間:2024/2/28
47
豆豆
生活随笔
收集整理的這篇文章主要介紹了
·MySQL数据库管理(SQL操作命令,解决忘记密码,设置用户权限)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 數(shù)據(jù)庫基本操作命令
- MySQL查看當前服務器中包含的庫
- 查看當前使用的庫中包含的表
- 顯示數(shù)據(jù)表的結(jié)構(gòu)(字段)命令
- SQL操作管理命令
- DDL操作命令
- DDL語句創(chuàng)建庫,表的命令
- DDL語句刪除庫,表的命令
- DML操作命令
- DML語句的作用是
- 向數(shù)據(jù)表中插入新的數(shù)據(jù)記錄命令(insert)
- alter用法總結(jié)
- 修改,更新數(shù)據(jù)表中的數(shù)據(jù)記錄(update)
- 刪除整個表
- 在數(shù)據(jù)表中刪除指定的數(shù)據(jù)記錄命令(delete)
- **清空表的數(shù)據(jù)**
- 刪除數(shù)據(jù)庫和表
- 查看表結(jié)構(gòu)命令
- DQL操作命令
- 創(chuàng)建臨時表
- DCL操作命令
- DCL語句的作用
- 設置用戶權(quán)限的命令
- 撤銷用戶權(quán)限的命令
- 遠程連接實例
- 安裝mysql服務進行遠程連接
- 設置環(huán)境變量
- 忘記密碼
- 改該密碼
數(shù)據(jù)庫基本操作命令
MySQL查看當前服務器中包含的庫
show databases;
mysql -u root -p進入數(shù)據(jù)庫 > show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)查看當前使用的庫中包含的表
需要先使用USE語句切換到使用的庫
- use 數(shù)據(jù)庫名
- show tables
顯示數(shù)據(jù)表的結(jié)構(gòu)(字段)命令
mysql> describe [數(shù)據(jù)庫名.]表名; ===>可以使用desc [數(shù)據(jù)庫名.]表名; mysql> describe user; +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Host | char(60) | NO | PRI | | | | User | char(32) | NO | PRI | | |SQL操作管理命令
- SQL語言
- 是Structured Query Language的縮寫,及結(jié)構(gòu)化查詢語言
- 是關(guān)系型數(shù)據(jù)庫的標準語言
- 用于維護管理數(shù)據(jù)庫,如數(shù)據(jù)查詢,數(shù)據(jù)更新,訪問控制,對象管理等功能
- SQL分類
- DDL:數(shù)據(jù)定義語言
- DML:數(shù)據(jù)操縱語言
- DQL:數(shù)據(jù)查詢語言
- DCL:數(shù)據(jù)控制語言
DDL操作命令
DDL語句用于創(chuàng)建數(shù)據(jù)庫對象,如庫,表,索引等
DDL語句創(chuàng)建庫,表的命令
創(chuàng)建數(shù)據(jù)表:create table 表名(字段定義…)創(chuàng)建數(shù)據(jù)庫:create database 數(shù)據(jù)庫名創(chuàng)建數(shù)據(jù)表
mysql> create database zhang; //創(chuàng)建庫 Query OK, 1 row affected (0.00 sec) mysql> use zhang; Database changed mysql> create table benat (id int(3) not null primary key auto_increment,name varchar(10) not null,score decimal(5,2),address varchar(50) default '未知');整數(shù)型int(3);000-999- double 浮點型 8字節(jié)- decimal(5,2)有效數(shù)字是5位,小數(shù)點后面保留2位 (100.00 099.50)- float 單精度浮點 4字節(jié)- char (10)固定長度字符串- varcha (50)可變長度字符串- char字符- create table benat 創(chuàng)建表 - not null 不允許為空auto_increment 自動增長default 未知- 字段約束:非空not null;默認default ‘’;主鍵primary key;自增auto increment。存儲引擎:myisam;innodb。字符集:utf8。DDL語句刪除庫,表的命令
- 刪除指定的數(shù)據(jù)表:drop table [數(shù)據(jù)庫名.]表名
- 刪除指定的數(shù)據(jù)庫:drop database 數(shù)據(jù)庫名
DML操作命令
DML語句的作用是
- DML語句用于對表中的數(shù)據(jù)進行管理
- 包括以下操作
- insert:插入新數(shù)據(jù)
- update:更新原有數(shù)據(jù)
- delete:刪除不需要的數(shù)據(jù)
向數(shù)據(jù)表中插入新的數(shù)據(jù)記錄命令(insert)
mysql> insert into 表名(字段1,字段2,字段3,......) values (字段1的值,字段2的值,字段3的值,.....); mysql> insert into benat values (2,'lisi',77,'shanghai'); 'insert into 后面不跟字段名(默認所有字段),values后面按順序?qū)懭?#39;mysql> select * from info; +----+----------+-------+----------+ | id | name | score | address | +----+----------+-------+----------+ | 1 | zhangsan | 88.00 | beijing | | 2 | lisi | 77.00 | shanghai | +----+----------+-------+----------+添加用戶
mysql> insert into benat (id,name,score,address) values (2,'lisi',88.6,'shanghai'); mysql> select * from benat; +----+----------+-------+----------+ | id | name | score | address | +----+----------+-------+----------+ | 1 | zhangsan | 88.60 | biejing | | 2 | lisi | 88.60 | shanghai | +----+----------+-------+----------+ 2 rows in set (0.00 sec)alter用法總結(jié)
- 刪除列
- 增加列
重命名列
ALTER TABLE 【表名】 CHANGE 【列名】【新名】給info表添加一個字段為hobby,整型數(shù)據(jù)類型,不允許為空
alter table info add hobby int(3) not null
查詢A表中的行數(shù)
顯示大于80的
mysql> select * from benat where score > 80;創(chuàng)建一個新表tmp 把大于80 的數(shù)據(jù)導入進去
mysql> create table tmp as select * from benat where score > 80;修改,更新數(shù)據(jù)表中的數(shù)據(jù)記錄(update)
update更新:表名 set 字段名 1=值1[,字段名2=值2] where條件表達式 mysql> update benat set name='zhangsan' where id=2; 修改id=2的名字 mysql> select * from benat; +----+----------+-------+----------+ | id | name | score | address | +----+----------+-------+----------+ | 2 | zhangsan | 88.60 | shanghai | | 3 | wangwu | 77.00 | shanghai | +----+----------+-------+----------+mysql> update benat set name='zhangsan'; //修改所有id的name' mysql> select * from benat; +----+----------+-------+----------+ | id | name | score | address | +----+----------+-------+----------+ | 2 | zhangsan | 88.60 | shanghai | | 3 | zhangsan | 77.00 | shanghai | +----+----------+-------+----------+mysql> update benat set score=77 where name='lisi'; 添加lisi mysql> select * from benat; +----+----------+-------+----------+ | id | name | score | address | +----+----------+-------+----------+ | 1 | zhangsan | 88.60 | biejing | | 2 | lisi | 77.00 | shanghai | | 3 | wangwu | 77.30 | hangzhou | | 4 | zhaoliu | 99.00 | suzhou | | 5 | tianxing | 60.00 | 未知 | +----+----------+-------+----------+ 5 rows in set (0.00 sec)刪除整個表
mysql> delete from tmp; //刪除在數(shù)據(jù)表中刪除指定的數(shù)據(jù)記錄命令(delete)
mysql> delete from 表名 where 條件表達式; mysql> delete from tmp where score >= 90 刪除90分以上的 mysql> select * from tmp; +----+----------+-------+----------+ | id | name | score | address | +----+----------+-------+----------+ | 1 | zhangsan | 88.60 | biejing | | 2 | lisi | 88.60 | shanghai | +----+----------+-------+----------+清空表的數(shù)據(jù)
mysql> delete from table_name; mysql> truncate table table_name;刪除數(shù)據(jù)庫和表
使用DDL語句刪除庫、表
- 刪除指定的數(shù)據(jù)表
刪除整個數(shù)據(jù)庫
mysql> drop database zhang;查看表結(jié)構(gòu)命令
mysql> desc benat; +---------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+----------------+ | id | int(3) | NO | PRI | NULL | auto_increment | | name | varchar(10) | NO | | NULL | | | score | decimal(5,2) | YES | | NULL | | | address | varchar(50) | YES | | 未知 | | +---------+--------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)DQL操作命令
- DQL是數(shù)據(jù)查詢語句,只有一條:SELECT
- 用于從數(shù)據(jù)表中查找符合條件的數(shù)據(jù)記錄
- 查詢時可不指定條件
查詢時指定條件
mysql> select 字段名1,字段名2,...... from 表名 where 條件表達式; mysql> select score from benat; +-------+ | score | +-------+ | 88.60 | | 77.00 | +-------+ mysql> select score,address from benat; +-------+----------+ | score | address | +-------+----------+ | 88.60 | shanghai | | 77.00 | shanghai | +-------+----------+ mysql> select score,address from benat where id=2; +-------+----------+ | score | address | +-------+----------+ | 88.60 | shanghai | +-------+----------+ 1 row in set (0.00 sec)創(chuàng)建臨時表
用于存放臨時的數(shù)據(jù),不會長時間的存在,斷開連接就會被刪除,數(shù)據(jù)存放在內(nèi)存之中,不在磁盤。
mysql> create temporary table tmp(id int (5) not null primary key auto_increment, -> name char(10) not null, -> socre double(5) default '0' -> )engine=innodb default charset=utf8; mysql> desc tmp; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(5) | NO | PRI | NULL | auto_increment | | name | char(10) | NO | | NULL | | | socre | decimal(5,2) | YES | | 0.00 | | +-------+--------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)復制表
mysql> create table tmp as select * from benat; 復制 like方法 mysql> create table newtest like test; ===>從test完整復制表的結(jié)構(gòu)生車工newtest mysql> insert into newtest select * from test; ===>導入數(shù)據(jù)DCL操作命令
DCL語句的作用
- 設置或查看用戶的權(quán)限,或者創(chuàng)建用戶
設置用戶權(quán)限的命令
-
若用戶已存在,則更改用戶密碼
-
若用戶不存在,則新建用戶
-
GRANT 權(quán)限列表 ON 數(shù)據(jù)庫名.表名 TO 用戶名@來源地址 [IDENTIFIED BY ‘密碼′
查看用戶的權(quán)限
#示例
mysql> show grants for ‘test’@‘20.0.0.20’;
撤銷用戶權(quán)限的命令
-
REVOKE 權(quán)限列表 ON 數(shù)據(jù)庫名.表名 FROM 用戶名@來源地址
mysql> revoke all privileges on *.* from 'root'@'%'; mysql> show grants for 'root'@'%'; +----------------------------------------------------+ | Grants for root@% | +----------------------------------------------------+ | GRANT USAGE ON *.* TO 'root'@'%' WITH GRANT OPTION | +----------------------------------------------------+ 1 row in set (0.00 sec)
遠程連接實例
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456'; '//all privileges:所有權(quán)限,%:所有終端' Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> grant all privileges on *.* to 'root'@'locahost' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)主機查看
查看數(shù)據(jù)庫
安裝mysql服務進行遠程連接
設置環(huán)境變量
mysql -uroot -p忘記密碼
設置一個tom用戶
mysql> grant all privileges on *.* to 'tom'@'locahost' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> quit修改配置文件
root@localhost mysql]# vim /etc/my.cnf server-id=1 skip-grant-tables //添加 (跳過驗證) [root@localhost bin]# systemctl restart mysqld.service [root@localhost bin]# mysql -u tom -p 再次登錄就不用密碼 Enter password: mysql>改該密碼
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A mysql> show tables; +---------------------------+ | Tables_in_mysql | +---------------------------+ . . . | time_zone_transition_type | | user | +---------------------------+ mysql> select * from user; authentication_string 用戶密碼的位置mysql> update mysql.user set authentication_string=password('123456') where user='tom'; Query OK, 0 rows affected, 1 warning (0.01 sec) Rows matched: 1 Changed: 0 Warnings: 1 mysql> flush privileges;總結(jié)
以上是生活随笔為你收集整理的·MySQL数据库管理(SQL操作命令,解决忘记密码,设置用户权限)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存价格战:性能对比、价格亲民,哪款内存
- 下一篇: mysql事务用法介绍及储存引擎介绍(M