search
HomeDatabaseMysql TutorialWindows下 MySQL命令 常用操作_MySQL

Windows下 MySQL命令 常用操作_MySQL

Jun 01, 2016 pm 01:10 PM
OrderCommonly usedoperatedatabaseScript

Windows平台下MySQL常用操作与命令

(一)客户端连接MySQL数据库服务器

命令格式:mysql -h 数据库服务器IP -u 用户名 -p 数据库名称

安装MySQL数据库完成以后,切换到MySQL安装目录的bin目录下面(例如我的是D:/AppServ/MySQL/bin>),执行如下命令连接MySQL数据库服务器:

D:/AppServ/MySQL/bin>mysql -h localhost -u root -p

提示输入登录密码,然后登录成功,如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

(二)显示当前数据库服务器上所有的数据库

显示当前数据库服务器上所有的数据库名称列表,执行如下命令:

mysql> show databases;

可以看到,所有的数据库以列表的形式显示出来,如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

(三)选中某个指定的数据库

命令格式:use 数据库名称

选中某个指定的数据库(例如存在一个名称为blog的数据库),可以执行如下命令:

mysql> use blog;

执行结果如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

(四)查询选定的数据库中存在的所有表

命令格式:show tables

或者

show tables from 数据库名称

使用use命令指定了blog数据库,执行如下命令:

mysql> show tables;

显示数据库blog中的所有表,如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

如果之前并没有执行use命令选定指定的数据库,要查看某个数据库中的表,可以执行如下命令:

mysql> show tables from blog;

from关键字相当于use,指定某个数据库,如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

(五)查看数据库中某个表结构

命令格式:describe 表名

假设数据库为blog,要查看表jblog_category的结构,执行如下命令:

mysql> describe jblog_category;

如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

或者用SHOW CREATE TABLE tablename/G;

(六)导出某个数据库,保存为SQL脚本文件

命令格式:mysqldump -u 用户名 -p 数据库名称 > SQL脚本文件名称.sql

或者

mysqldump -u 用户名 -p 数据库名称 > SQL脚本文件所在绝对路径

例如导出数据example为example.sql脚本,可以执行如下命令:

D:/AppServ/MySQL/bin>mysqldump -u root -p example > example.sql

如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

可以在目录D:/AppServ/MySQL/bin>下看到example.sql脚本文件。

(七)通过SQL脚本文件导入指定的数据库

命令格式:source SQL脚本文件名称.sql

或者

source SQL脚本文件所在绝对路径

准备工作:先把exmple数据库删除,再重新导入,如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

可以看到,MySQL数据库服务器上已经没有example这个数据库了。

假如SQL脚本文件为example.sql在当前D:/AppServ/MySQL/bin目录下,执行如下命令导入数据库:

mysql> create database example;
Query OK, 1 row affected (0.02 sec)

mysql> use example;
Database changed
mysql> source example.sql

如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

然后执行,就可以看到,sql脚本文件中,包括建表,插入数据等等,将数据全部导入到指定的数据库example中。

另外,source命令可以指定绝对路径,如:D:/AppServ/MySQL/bin/example.sql也是可以的。

(八)另一种通过SQL脚本文件还原数据的方法

命令格式:

mysql -h 数据库服务器IP -u 用户名 -p 数据库名称

或者

mysql -h 数据库服务器IP -u 用户名 -p 数据库名称

假设存在导出的备份脚本文件example.sql,现在创建一个example数据库,然后执行如下命令进行还原:

D:/AppServ/MySQL/bin>mysql -h localhost -u root -p example

可以看到提示输出口令,然后执行还原,如图所示:

Windows平台下MySQL常用操作与命令 - 阿飘 - 阿飘的博客

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 the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

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 Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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),