VPN技术 · 2023年11月10日 0

MySQL常用命令

MySQL 常用命令

MySQL 常用命令

1. 导出整个数据库
使用命令:
mysqldump -u 用户名 -p --default-character-set=latin1 数据库名 > 导出的文件名
例如:
mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql
2. 导出单个表
使用命令:
mysqldump -u 用户名 -p 数据库名 表名 > 导出的文件名
例如:
mysqldump -u wcnc -p smgp_apps_wcnc users > wcnc_users.sql
3. 导出数据库结构
使用命令:
mysqldump -u wcnc -p -d --add-drop-table smgp_apps_wcnc > d:wcnc_db.sql
参数说明:
-d 表示不导出数据,–add-drop-table 在每个创建表语句之前增加一个删除表的命令。
4. 导入数据库
A: 使用 source 命令
进入 MySQL 控制台:
mysql -u root -p
然后选择数据库:
mysql> use 数据库
接着使用 source 命令导入脚本文件:
mysql> source wcnc_db.sql

B: 使用 mysqldump 命令

mysqldump -u username -p dbname < filename.sql

C: 使用 mysql 命令

mysql -u username -p -D dbname < filename.sql

一、启动与退出
1. 进入 MySQL:启动 MySQL Command Line Client 并输入安装时的密码,提示符为:mysql>
2. 退出 MySQL:使用 quit 或 exit
二、数据库操作
1. 创建数据库
命令:
create database <数据库名>
例如:
mysql> create database xhkdb;
2. 显示所有数据库
命令:
show databases;
3. 删除数据库
命令:
drop database <数据库名>
例如:
mysql> drop database xhkdb;
4. 连接数据库
命令:
use <数据库名>
例如:
mysql> use xhkdb;
提示:Database changed
5. 查看当前使用的数据库
mysql> select database();

6. 查看当前数据库中的表信息:
mysql> show tables;

三、表操作(在连接某个数据库后进行)
1. 创建表
命令:
create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);

例如:
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default 0,
> degree double(16,2));
2. 获取表结构
命令:
desc 表名show columns from 表名
例如:
mysql> DESC MyClass
mysql> show columns from MyClass;
3. 删除表
命令:
drop table <表名>
例如:
mysql> drop table MyClass;
4. 插入数据
命令:
insert into <表名> [( <字段名1>[,..<字段名n> ])] values ( 值1 )[, ( 值n )]
例如:
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99),(3,'Wang',96.59);
5. 查询表中的数据
1) 查询所有行
命令:
select <字段1,字段2,..> from <表名> where <表达式>
例如:
mysql> select * from MyClass;
2) 查询前几行数据
例如:
mysql> select * from MyClass order by id limit 0,2;
或者:
mysql> select * from MyClass limit 0,2;
6. 删除表中数据
命令:
delete from 表名 where 表达式
例如:
mysql> delete from MyClass where id=1;
7. 修改表中数据
命令:
update 表名 set 字段=新值 where 条件
例如:
mysql> update MyClass set name='Mary' where id=1;
8. 在表中增加字段
命令:
alter table 表名 add 字段 类型 其他;
例如:
mysql> alter table MyClass add passtest int(4) default 0;
9. 更改表名
命令:
rename table 原表名 to 新表名;
例如:
mysql> rename table MyClass to YouClass;
更新字段内容
update 表名 set 字段名 = 新内容
update 表名 set 字段名 = replace(字段名,旧内容,新内容);
文章前面加入4个空格:
update article set content=concat(' ',content);
字段类型
1. INT[(M)] 型:正常大小整数类型
2. DOUBLE[(M,D)] [ZEROFILL] 型:正常大小(双精密)浮点数字类型
3. DATE 日期类型:范围为 1000-01-01 到 9999-12-31,格式为 YYYY-MM-DD
4. CHAR(M) 型:定长字符串类型,存储时用空格填满右边
5. BLOB TEXT 类型,最大长度为 65535(2^16-1)个字符。
6. VARCHAR 型:变长字符串类型
5. 导入数据库表
(1) 创建 .sql 文件
(2) 先创建一个库:
mysqladmin -u root -p create auction
(3) 导入 auction.sql 文件:
mysql -u root -p auction < auction.sql
6. 修改数据库
(1) 在表中增加字段:
alter table dbname add column userid int(11) not null primary key auto_increment;
7. MySQL 数据库授权
grant select,insert,delete,create,drop on *.* to 用户名@localhost identified by 密码;
例如:创建一个用户帐号以便访问数据库:
grant usage on test.* to [email protected];
之后创建了一个用户 testuser,只能从 localhost 连接到 test 数据库,且具有 SELECT、INSERT、DELETE 和 UPDATE 权限。结束操作并退出 MySQL 客户端:
mysql> exit
Bye!

1: 使用 SHOW 语句查看当前存在的数据库:
mysql> SHOW DATABASES;
2: 创建一个数据库 MYSQLDATA
mysql> Create DATABASE MYSQLDATA;
3: 选择创建的数据库
mysql> USE MYSQLDATA;
提示:Database changed 表示操作成功!
4: 查看当前数据库中的表
mysql> SHOW TABLES;
5: 创建一个数据库表
mysql> Create TABLE MYTABLE (name VARCHAR(20), sex CHAR(1));
6: 显示表的结构:
mysql> DESCRIBE MYTABLE;
7: 向表中添加记录
mysql> insert into MYTABLE values ('hyq','M');
8: 用文本方式将数据装入数据库表(例如 D:/mysql.txt)
mysql> LOAD DATA LOCAL INFILE 'D:/mysql.txt' INTO TABLE MYTABLE;
9: 导入 .sql 文件命令(例如 D:/mysql.sql)
mysql> use database;
mysql> source d:/mysql.sql;
10: 删除表
mysql> drop TABLE MYTABLE;
11: 清空表
mysql> delete from MYTABLE;
12: 更新表中数据
mysql> update MYTABLE set sex='f' where name='hyq';

以下是无意中在网络上看到的 MySQL 管理心得。
摘自:http://www1.xjtusky.com/article/htmldata/2004_12/3/57/article_1060_1.html

在 Windows 中,MySQL 以服务形式存在,使用前需确保服务已启动,未启动可用 net start mysql 命令启动。在 Linux 中,启动时可用 /etc/rc.d/init.d/mysqld start 命令,启动者需具有管理员权限。
刚安装的 MySQL 包含一个空密码的 root 帐户和一个匿名帐户,这存在安全隐患。对于重要应用,建议删除匿名帐户并为 root 设置密码,命令如下:
use mysql;
delete from User where User="";
update User set Password=PASSWORD('newpassword') where User='root';
如需限制用户的登录终端,可更新 User 表中相应用户的 Host 字段。修改后需重新启动数据库服务,登录时可使用类似命令:
mysql -uroot -p;
mysql -uroot -pnewpassword;
其中 mydb 是要登录的数据库名称。
在开发和实际应用中,用户不应仅使用 root 用户连接数据库,尽管方便,但会带来安全隐患,建议为应用中使用的用户赋予合适的数据库权限。MySQL 的用户管理通过 User 表实现,添加新用户的方法有两种:一是在 User 表插入相应数据行,设置权限;二是通过 GRANT 命令创建用户。GRANT 的常用用法如下:
grant all on mydb.* to 用户名 identified by 'password';
grant usage on *.* to 用户名 identified by 'password';
grant select,insert,update on mydb.* to 用户名 identified by 'password';
grant update,delete on mydb.TestTable to 用户名 identified by 'password';
若要赋予用户相应对象上的权限管理能力,可在 GRANT 后面添加 WITH GRANT OPTION 选项。对于插入 User 表的用户,Password 字段应使用 PASSWORD 函数加密更新,以防被窃取。对于不再使用的用户,应予以清除,权限过界的用户应及时回收,回收权限可以通过更新 User 表字段或使用 REVOKE 操作。
下面是从其它资料获得的常用权限解释:
全局管理权限:
FILE: 在 MySQL 服务器上读写文件。
PROCESS: 显示或终止其他用户的服务线程。
RELOAD: 重载访问控制表,刷新日志等。
SHUTDOWN: 关闭 MySQL 服务。
数据库/数据表/数据列权限:
ALTER: 修改已存在的数据表(例如增加/删除列)和索引。
CREATE: 建立新的数据库或数据表。
DELETE: 删除表的记录。
DROP: 删除数据表或数据库。
INDEX: 建立或删除索引。
INSERT: 增加表的记录。
SELECT: 显示/搜索表的记录。
UPDATE: 修改表中已存在的记录。
特殊权限:
ALL: 允许执行任何操作(与 root 相同)。
USAGE: 仅允许登录,其他不允许操作。