search
HomeDatabaseMysql Tutorialmysql命令行操作

mysql命令行操作

Jun 07, 2016 pm 03:53 PM
mysqlCommand Lineoperateconnect

一、连接MYSQL 式: mysql -h主机地址 -u用户名 -p用户密码 或者: mysql -u 用户名 -p // 回车后要求输入密码,密码不可见 1、连接到本机上的MYSQL。 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root -p,回车后提示你输密码. 注意用户

一、连接MYSQL

格式: mysql -h主机地址 -u用户名 -p用户密码

或者: mysql -u 用户名 -p // 回车后要求输入密码,密码不可见

1、连接到本机上的MYSQL。
首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root -p,回车后提示你输密码.注意用户名前可以有空格也可以没有空格,但是如果-p后带有用户密码,那么-p与密码之间必须没有空格,否则让你重新输入密码.例如以下都是合法的登陆:(帐号:root   密码:123)
mysql -u root -p
mysql -uroot -p
mysql -uroot -p123


如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是: mysql>

2、连接到远程主机上的MYSQL。假设远程主机的IP为:110.110.110.110,用户名为root,密码为abcd123。则键入以下命令:
mysql -h110.110.110.110 -u root -p 123;(注:u与root之间可以不用加空格,其它也一样)

3、退出MYSQL命令: exit (回车)

二、修改密码

格式:mysqladmin -u用户名 -p旧密码 password 新密码

1、给root加个密码ab12。首先在DOS下进入目录mysql\bin,然后键入以下命令
mysqladmin -u root -password ab12
注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。

2、再将root的密码改为djg345。
mysqladmin -u root -p ab12 password djg345

注意:和上面不同,下面的因为是MYSQL环境中的命令,所以后面都带一个分号作为命令结束符

3、命令行修改root密码:
mysql> UPDATE mysql.user SET password=PASSWORD(’新密码’) WHERE User=’root’;
mysql> FLUSH PRIVILEGES;

4、显示当前的user:
mysql> SELECT USER();


三、增加新用户

格式:grant select on 数据库.* to 用户名@登录主机 identified by “密码”

1、增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用root用户连入

MYSQL,然后键入以下命令:
grant select,insert,update,delete on *.* to test1”%" Identified by “abc”;
但增加的用户是十分危险的,你想如某个人知道test1的密码,那么他就可以在internet上的任何一台电脑上登录你的mysql数据库并对你的数据可以为所欲为了,解决办法见2。

2、增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作(localhost指本地主机,即MYSQL数据库所在的那台主机),
这样用户即使用知道test2的密码,他也无法从internet上直接访问数据库,只能通过MYSQL主机上的web页来访问了。
grant select,insert,update,delete on mydb.* to test2@localhost identifiedby “abc”;
如果你不想test2有密码,可以再打一个命令将密码消掉。
grant select,insert,update,delete on mydb.* to test2@localhost identified by “”;

四、操作技巧

1、如果你打命令时,回车后发现忘记加分号,你无须重打一遍命令,只要打个分号回车就可以了。
也就是说你可以把一个完整的命令分成几行来打,完后用分号作结束标志就OK。

2、你可以使用光标上下键调出以前的命令。

五、对数据库的操作

1、显示当前数据库服务器中的数据库列表:
mysql> SHOW DATABASES;
注意:mysql库里面有MYSQL的系统信息,我们改密码和新增用户,实际上就是用这个库进行操作。

2、显示数据库中的数据表:
mysql> USE 库名;
mysql> SHOW TABLES;


3、显示use的数据库名:
mysql> SELECT DATABASE();

4、建立数据库:
mysql> CREATE DATABASE 库名;

5、删除数据库:
mysql> DROP DATABASE 库名;

6、导入.sql文件命令:
mysql> USE 数据库名;
mysql> SOURCE d:/mysql.sql;

也可以在DOS环境下键入以下命令进行导入:
mysql -uroot -proot databasename 注意:导入前请保证mysql中必须有databasename这个数据库;

六、备份数据库

注意,mysqldump命令在DOS的 mysql\bin 目录下执行,不能在mysql环境下执行,因此,不能以分号“;”结尾。若已登陆mysql,请运行退出命令mysql> exit

1.导出整个数据库
导出文件默认是存在mysql\bin目录下
mysqldump -u用户名 -p数据库名 > 导出的文件名
mysqldump -uroot -p123456 database_name > outfile_name.sql

2.导出一个表
mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
mysqldump -u user_name -p database_name table_name > outfile_name.sql

3.导出一个数据库结构
mysqldump -u user_name -p -d –add-drop-table database_name > outfile_name.sql
-d 没有数据 –add-drop-table 在每个create语句之前增加一个drop table

4.带语言参数导出
mysqldump -uroot -p –default-character-set=latin1 –set-charset=gbk –skip-opt database_name > outfile_name.sql

七、将文本数据转到数据库中

1、文本数据应符合的格式:字段数据之间用tab键隔开,null值用\n来代替.例:
3 rose 大连二中 1976-10-10
4 mike 大连一中 1975-12-23
假设你把这两组数据存为school.txt文件,放在c盘根目录下。

2、数据传入命令 
mysql> load data local infile "c:\school.txt" into table 表名;
注意:你最好将文件复制到mysql\bin目录下,并且要先用use命令打表所在的库。

八、对表的操作

1、显示数据表的结构:
mysql> DESCRIBE 表名; (DESC 表名)

2、建立数据表:
mysql> USE 库名; //进入数据库
mysql> CREATE TABLE 表名 (字段名 VARCHAR(20), 字段名 CHAR(1));

3、删除数据表:
mysql> DROP TABLE 表名;

4、重命名数据表
alter table t1 rename t2;

5、显示表中的记录:
mysql> SELECT * FROM 表名;

6、往表中插入记录:
mysql> INSERT INTO 表名 VALUES (”hyq”,”M”);

7、更新表中数据:
mysql-> UPDATE 表名 SET 字段名1=’a',字段名2=’b’ WHERE 字段名3=’c';

8、将表中记录清空:
mysql> DELETE FROM 表名;

9、用文本方式将数据装入数据表中:
mysql> LOAD DATA LOCAL INFILE “D:/mysql.txt” INTO TABLE 表名;

10、 显示表的定义,还可以看到表的约束,例如外键

mysql> SHOW CREATE TABLE yourtablename   ; 
还可以通过 mysqldump 将表的完整定义转储到文件中,当然包括外键定义。     

还可以通过下面的指令列出表 T 的外键约束:     
mysql> SHOW TABLE STATUS FROM yourdatabasename LIKE 'T'   
外键约束将会在表注释中列出。

存储过程

11、创建存储过程

CREATE PROCEDURE procedureName (in paramentName type, in paramentName type,……)
BEGIN
SQL sentences;
END

12、调用存储过程
mysql> CALL procedureName(paramentList);

例:mysql> CALL addMoney(12, 500);

13、查看特定数据库的存储过程
方法一:mysql> SELECT `name` FROM mysql.proc WHERE db = 'your_db_name' AND `type` = 'PROCEDURE';
方法二:mysql> show procedure status;

14、删除存储过程
mysql> DROP PROCEDURE procedure_name;
mysql> DROP PROCEDURE IF EXISTS procedure_name;

15、查看指定的存储过程定义
mysql> SHOW CREATE PROCEDURE proc_name;
mysql> SHOW CREATE FUNCTION func_name;

---------- 示例一-----------
mysql> DELIMITER $$   
mysql> USE `db_name`$$   //选择数据库
mysql> DROP PROCEDURE IF EXISTS `addMoney`$$   //如果存在同名存储过程,则删除之
mysql> CREATE DEFINER= `root`@`localhost` PROCEDURE `addMoney`(IN xid INT(5),IN xmoney INT(6))   
mysql> BEGIN 
mysql> UPDATE USER u SET u.money = u.money + xmoney WHERE u.id = xid;   //分号";"不会导致语句执行,因为当前的分割符被定义为$$
mysql> END$$   //终止
mysql> DELIMITER ;    //把分割符改回分号";"

mysql> call addMoney(5,1000); //执行存储过程

---------- 示例二-----------
mysql> delimiter // 
mysql> create procedure proc_name (in parameter integer) 
mysql> begin 
mysql> if parameter=0 then 
mysql> select * from user order by id asc; 
mysql> else 
mysql> select * from user order by id desc; 
mysql> end if; 
mysql> end; 
mysql> //        //此处“//”为终止符
mysql> delimiter ; 
mysql> show warnings; 
mysql> call proc_name(1); 
mysql> call proc_name(0);


九、修改表的列属性的操作

1、为了改变列a,从INTEGER改为TINYINT NOT NULL(名字一样),
并且改变列b,从CHAR(10)改为CHAR(20),同时重命名它,从b改为c:
mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);

2、增加一个新TIMESTAMP列,名为d:
mysql> ALTER TABLE t2 ADD d TIMESTAMP;

3、在列d上增加一个索引,并且使列a为主键:
mysql> ALTER TABLE t2 ADD INDEX (d), ADD PRIMARY KEY (a);

4、删除列c:
mysql> ALTER TABLE t2 DROP COLUMN c;

5、增加一个新的AUTO_INCREMENT整数列,命名为c:
mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,ADD INDEX (c);
注意,我们索引了c,因为AUTO_INCREMENT柱必须被索引,并且另外我们声明c为NOT NULL,
因为索引了的列不能是NULL

十、一个建库和建表以及插入数据的实例

drop database if exists school; //如果存在SCHOOL则删除
create database school; //建立库SCHOOL
use school; //打开库SCHOOL
create table teacher //建立表TEACHER
(
id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default ‘深圳’,
year date
); //建表结束
//以下为插入字段
insert into teacher values('','allen','大连一中','1976-10-10');
insert into teacher values('','jack','大连二中','1975-12-23');
如果你在mysql提示符键入上面的命令也可以,但不方便调试。
(1)你可以将以上命令原样写入一个文本文件中,假设为school.sql,然后复制到c:\下,并在DOS状态进入目录\mysql\bin,然后键入以下命令:
mysql -uroot -p密码 如果成功,空出一行无任何显示;如有错误,会有提示。(以上命令已经调试,你只要将//的注释去掉即可使用)。
(2)或者进入命令行后使用 mysql> source c:\school.sql; 也可以将school.sql文件导入数据库中。


http://blog.csdn.net/sd0902/article/details/8394106
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor