search
HomeDatabaseMysql Tutorial初识MySQL数据库的各种CMD命令窗口下的指令

初识MySQL数据库的各种CMD命令窗口下的指令

Jun 07, 2016 pm 03:26 PM
cmdmysqlFirst acquaintanceOrderinstructiondatabasewindow

今天我们就来看一下数据库的各种命令,以下命令全部是从CMD命令窗口下的命令行输入指令,首先如果如果输入mysql,系统提示“mysql不是内部命令或外部命令。那么这其实是环境变量没有设置好的原因,例如我的mysql的安装目录是C:\news\mySql,则在环境变量中的

今天我们就来看一下数据库的各种命令,以下命令全部是从CMD命令窗口下的命令行输入指令,首先如果如果输入mysql,系统提示“mysql不是内部命令或外部命令。那么这其实是环境变量没有设置好的原因,例如我的mysql的安装目录是C:\news\mySql,则在环境变量中的系统变量PATH路径中输入C:\news\mySql\MySQL Server 5.5\bin,重新启动CMD,这时再输入mysql,应该就能正常操作了,当然前提是mysql服务要正常启动,如果没有启动那么CMD会显示CANNOT CONNECT th.........。

首先mysql语句是不区分大小写的。来看第一个命令:

mysqladmin -uroot -p123  password  456

        这句话的意思是将root的密码更改为456 ,原来的密码是123,-u 表示用户名,紧跟着的是root用户名,-p表示密码后面的123是登录密码,中间的空格可以省略。但password和新密码之间的空格不可省略。

1.1、连接到远程数据库,基本格式如下:

        Mysql    -h 电脑名(IP地址) -u 用户名  -p 密码

例如我的数据库是本地的,数据库ip地址就是127.0.0.1,那么就可以这样写:

        mysql  -h 127.0.0.1  -u root   -p 123

1.2、MySQL权限管理:

    mysql权限管理遵循最少权限原则,即一个用户(一个进程),应该拥有能够执行分配给它的任务的最低级别的权限。所以可以建立一个权限不如root的用户来执行对数据库的日常管理。管理员的权限有Create temporary tables、File、Lock Tables、Process、Reload、Replication client、Replication slave、Show databases、Shutdown、Super,此外还有两个特别的权限,分别是All和Usage,分别表示所有权限和不授予权限即只有登录数据库的权利而没有其他的权利,通常会在以后授予更多的权限。

1.3、增加新用户

        首先要登录mysql数据库,root登录之后先选择数据库之后再进行增加新用户的操作,

>use database_name;  //选择数据库

以下几步是连着一起运行的,直到最后以分号结束,回车键并不会导致命令结束!是要碰到分号按回车才表示结束语句。        

>grant all

       >on  database_name.*   //对某个数据库下的所有表有all权限

   >to  rick   identified by  '123'

   >with  grant  option ;

上面这几句话表示创建了一个用户名为rick,密码为123的用户并且拥有对dtabase_name数据库所有操作权限。最后一句话表示允许这个用户向其他人授予权限。grant就是授予的意思。

        收回用户的权限

>revoke all 

>from rick;

        接下来创建一个没有任何授权的常规用户

>grant suage

>on  database_name.* 

>to rick identified by '123' ;

   也可以授予一些适当的权限,例如select,delete等等。

        >grant  select,delete,insert,alert,create,drop,update

>on database_name.*

>to rick identified by  '123';

1.4、删除用户

 mysql>drop database testDB; //删除用户的数据库

删除账户及权限:>drop user 用户名@'%';

 这句话也可以删除:

 >drop user 用户名@ localhost; 

1.5、创建数据库:create database name;

1.6、删除数据库:drop database name ;

1.7、创建一个数据表范例:

        create  tablename (

id   int(2) unsigned not null auto_increment,

name   char(10) not null,

age      tinyint unsigned not null,

addr    text not null,

time    timestamp

);

1.8、备份数据库

有两种方法:第一种是方法是复制数据库文件时使用LOCK TABLES命令锁定这些表。比较少,所以省略不说了,来讲第二种方法:使用MySQLdump命令。MySQLdump命令将数据库导出到.sql文件中。命令使用格式如下:

Mysqldump [options]  database [table]

options一般为--opt,table指的是具体的数据库名。

不知道命令可以用mysqldump --help命令查看用法。

注意mysqldump命令是在cmd的窗口下直接输入的,所以要先退出mysql的客户端,即不能是在mysql>状态下输入,先退出再操作,因为我处于mysql的状态下所以这条命令一直无效,白费了很多气力!

1.9、还原数据库

例如我要把stu.sql文件导入到studyphp数据库中,则命令如下所示:

>use studyphp

>source  c:/stu.sql;

2.0、show命令

  a. show tables或show tables from database_name; // 显示当前数据库中所有表的名称 

   b. show databases; // 显示mysql中所有数据库的名称 

   c. show columns from table_name from database_name; 或show columns from database_name.table_name;   // 显示表中列名称 

   d. show grants for user_name;   //   显示一个用户的权限,显示结果类似于grant 命令 

   e. show index from table_name;   // 显示表的索引 

   f. show status;   // 显示一些系统特定资源的信息,例如,正在运行的线程数量 

   g. show variables; // 显示系统变量的名称和值 
   
   h. show   processlist; // 显示系统中正在运行的所有进程,也就是当前正在执行的查询。大多数用户可以查看 
                                 他们自己的进程,但是如果他们拥有process权限,就可以查看所有人的进程,包括密码。 


   i. show table status; // 显示当前使用或者指定的database中的每个表的信息。信息包括表类型和表的最新更新时间 


   j. show privileges;   // 显示服务器所支持的不同权限 


   k. show create database database_name; // 显示create database 语句是否能够创建指定的数据库 


   l. show create table table_name; // 显示create database 语句是否能够创建指定的数据库 


   m. show engies;   // 显示安装以后可用的存储引擎和默认引擎。 


   n. show innodb status; // 显示innoDB存储引擎的状态 


   o. show logs; // 显示BDB存储引擎的日志 


   p. show warnings; // 显示最后一个执行的语句所产生的错误、警告和通知 


   q. show errors; // 只显示最后一个执行语句所产生的错误


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),