如何用cmd连接数据库Mysql数据库

cmd连接mysql的方法详解
字体:[ ] 类型:转载 时间:
本篇文章是对cmd连接mysql的方法进行了详细的分析介绍,需要的朋友参考下
连接:mysql -h主机地址 -u用户名 -p用户密码 (注:u与root可以不用加空格,其它也一样)断开:exit (回车)
创建授权:grant select on 数据库.* to 用户名@登录主机 identified by \"密码\"修改密码:mysqladmin -u用户名 -p旧密码 password 新密码删除授权: revoke select,insert,update,delete om *.* fromtest2@
显示数据库:显示数据表:显示表结构:describe 表名;
创建库:create database 库名;删除库:drop database 库名;使用库:use 库名;
创建表:create table 表名 (字段设定列表);删除表:drop table 表名;修改表:alter table t1 rename t2查询表:select * from 表名;清空表:delete from 表名;备份表: mysqlbinmysqldump -h(ip) -uroot -p(password) databasenametablename & tablename.sql恢复表: mysqlbinmysql -h(ip) -uroot -p(password) databasenametablename & tablename.sql(操作前先把原来表删除)
增加列:ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,ADDINDEX (c);修改列:ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b cCHAR(20);删除列:ALTER TABLE t2 DROP COLUMN
备份数据库:mysql\bin\mysqldump -h(ip) -uroot -p(password) databasename& database.sql恢复数据库:mysql\bin\mysql -h(ip) -uroot -p(password) databasename& database.sql复制数据库:mysql\bin\mysqldump --all-databases &all-databases.sql修复数据库:mysqlcheck -A -o -uroot -p54safer
文本数据导入: load data local infile \"文件名\" into table 表名;数据导入导出:mysql\bin\mysqlimport database tables.txt第一招、mysql服务的启动和停止net stop mysqlnet start mysql
第二招、登陆mysql语法如下: mysql -u用户名-p用户密码键入命令mysql -uroot -p,回车后提示你输入密码,输入12345,然后回车即可进入到mysql中了,mysql的提示符是:mysql&注意,如果是连接到另外的机器上,则需要加入一个参数-h机器IP
第三招、增加新用户格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"如,增加一个用户user1密码为password1,让其可以在本机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令:grant select,insert,update,delete on *.* touser1@localhost Identified by "password1";如果希望该用户能够在任何机器上登陆mysql,则将localhost改为"%"。如果你不想user1有密码,可以再打一个命令将密码去掉。grant select,insert,update,delete on mydb.* touser1@localhost identified by"";
第四招: 操作数据库登录到mysql中,然后在mysql的提示符下运行下列命令,每个命令以分号结束。1、 显示数据库列表。缺省有两个数据库:mysql和test。mysql库存放着mysql的系统和用户权限信息,我们改密码和新增用户,实际上就是对这个库进行操作。2、 显示库中的数据表:3、 显示数据表的结构:describe 表名;4、 建库与删库:create database 库名;drop database 库名;5、 建表:use 库名;create table 表名(字段列表);drop table 表名;6、 清空表中记录:delete from 表名;7、 显示表中的记录:select * from 表名;
第五招、导出和导入数据1. 导出数据:mysqldump --opt test & mysql.test即将数据库test数据库导出到mysql.test文件,后者是一个文本文件如:mysqldump -u root -p123456 --databases dbname &mysql.dbname就是把数据库dbname导出到文件mysql.dbname中。2. 导入数据:mysqlimport -u root -p123456 & mysql.dbname。不用解释了吧。3. 将文本数据导入数据库:文本数据的字段数据之间用tab键隔开。load data local infile "文件名" into table 表名;1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql& SHOW DATABASES;2:2、创建一个数据库MYSQLDATAmysql& CREATE DATABASE MYSQLDATA;3:选择你所创建的数据库mysql& USE MYSQLDATA; (按回车键出现Database changed时说明操作成功!)4:查看现在的数据库中存在什么表mysql& SHOW TABLES;5:创建一个数据库表mysql& CREATE TABLE MYTABLE (name VARCHAR(20), sexCHAR(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" INTOTABLE MYTABLE;9:导入.sql文件命令(例如D:/mysql.sql)mysql&mysql&source d:/mysql.10:删除表mysql&drop TABLE MYTABLE;11:清空表mysql&delete from MYTABLE;12:更新表中数据mysql&update MYTABLE set sex="f" where name='hyq';13:备份数据库mysqldump -u root库名&xxx.data14:
例2:连接到远程主机上的MYSQL  假设远程主机的IP为:110.110.110.110,用户名为root,密码为abcd123。则键入以下命令:   
  mysql-h110.110.110.110 -uroot -pabcd123   
  (注:u与root可以不用加空格,其它也一样)   
  3、退出MYSQL命令:exit
(一) 连接MYSQL:&& 格式: mysql -h主机地址 -u用户名-p用户密码1、例1:连接到本机上的MYSQL&&首先在打开DOS窗口,然后进入mysql安装目录下的bin目录下,例如: D:\mysql\bin,再键入命令mysql -uroot-p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是:mysql&2、例2:连接到远程主机上的MYSQL&&假设远程主机的IP为:10.0.0.1,用户名为root,密码为123。则键入以下命令:&& mysql -h10.0.0.1 -uroot-p123(注:u与root可以不用加空格,其它也一样)3、退出MYSQL命令&& exit (回车)(二) 修改密码:&& 格式:mysqladmin -u用户名 -p旧密码password 新密码1、例1:给root加个密码123。首先在DOS下进入目录C:\mysql\bin,然后键入以下命令:&& mysqladmin -uroot -password123&&注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。2、例2:再将root的密码改为456&& mysqladmin -uroot -pab12password 456(三) 增加新用户:(注意:和上面不同,下面的因为是MYSQL环境中的命令,所以后面都带一个分号作为命令结束符)&& 格式:grant select on 数据库.* to用户名@登录主机 identified by "密码"&&例1、增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MYSQL,然后键入以下命令:&& grantselect,insert,update,delete on *.* to test1@"%" Identified by"abc";&&但例1增加的用户是十分危险的,你想如某个人知道test1的密码,那么他就可以在internet上的任何一台电脑上登录你的mysql数据库并对你的数据可以为所欲为了,解决办法见例2。&&例2、增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作(localhost指本地主机,即MYSQL数据库所在的那台主机),这样用户即使用知道test2的密码,他也无法从internet上直接访问数据库,只能通过MYSQL主机上的web页来访问了。&& grantselect,insert,update,delete on mydb.* to test2@localhost identifiedby "abc";&&如果你不想test2有密码,可以再打一个命令将密码消掉。&& grantselect,insert,update,delete on mydb.* to test2@localhost identifiedby "";(四) 显示命令1、显示数据库列表:&&&&刚开始时才两个数据库:mysql和test。mysql库很重要它里面有MYSQL的系统信息,我们改密码和新增用户,实际上就是用这个库进行操作。2、显示库中的数据表:&& use mysql; //打开库&&3、显示数据表的结构:&& describe 表名;4、建库:&& create database 库名;5、建表:&& use 库名;&& create table 表名(字段设定列表);6、删库和删表:&& drop database 库名;&& drop table 表名;7、将表中记录清空:&& delete from 表名;8、显示表中的记录:&& select * from 表名;
MySQL导入导出命令1.导出整个数据库  mysqldump -u 用户名 -p 数据库名 & 导出的文件名  mysqldump -u wcnc -p smgp_apps_wcnc &wcnc.sql2.导出一个表  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 在每个create语句之前增加一个drop table
4.导入数据库  常用source 命令  进入mysql数据库控制台,  如mysql -u root -p  mysql&use 数据库  然后使用source命令,后面参数为脚本文件(如这里用到的.sql)  mysql&source d:wcnc_db.sql (注:如果写成sourced:\wcnc_db.sql,就会报语法
使用load data 批量导入数据,这种做法可以瞬间导入数据,用处非常大! 代码如下:LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt'&&& [REPLACE | IGNORE]&&& INTO TABLE tbl_name&&& [FIELDS&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 字段操作,设置每个字段的分隔符
&&&&&&& [TERMINATED BY 'string']&&&&&&& [[OPTIONALLY] ENCLOSED BY 'char']&&&&&&& [ESCAPED BY 'char' ]&&& ]&&& [LINES&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 行操作,从某一个字符开始,到某个字符
&&&&&&& [STARTING BY 'string']&&&&&&& [TERMINATED BY 'string']&&& ]&&& [IGNORE number LINES]&&&&&&&&&&&&&& 行操作,忽略某行
&&& [(col_name_or_user_var,...)]&&&&&&& 字段操作,写入的字段与数据对应
&&& [SET col_name = expr,...)]示例:load data infile '/test/test.file' intotable 'test' fields terminated by "\t" (fieldsOne,fieldsTwo);意思是, 载入/test/test.file到表test中,使用\t分割字段,写入fieldsOne和fieldsTwo中,默认以换行符作为一个行分割!
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具1:在开始菜单  运行cmd
2:输入 mysql -u 这里输入数据库名字 -p
回车 然后会提示你输入密码
输入密码 例如:5rt598124
3:输入使用的数据库  例如  use s
4:输入导入的源sql文件 例如source e:\www\s482_2011.sql
阅读(...) 评论()2440人阅读
java(36)
一、MySQL 连接本地数据库,用户名为“root”,密码“123”(注意:“-p”和“123” 之间不能有空格)
C:\&mysql -h localhost -u root -p123
二、MySQL 连接远程数据库(192.168.0.201),端口“3306”,用户名为“root”,密码“123”
C:\&mysql -h 172.16.16.45 -P 3306 -u root -p123
三、MySQL 连接本地数据库,用户名为“root”,隐藏密码
C:\&mysql -h localhost -u root -p
Enter password:
四、MySQL 连接本地数据库,用户名为“root”,指定所连接的数据库为“test”
C:\&mysql -h localhost -u root -p123 -D test
mysql&select database();
+------------+
| database() |
+------------+
+------------+
下面是 MySQL 客户端命令的详细参数:
Ver 14.12 Distrib 5.0.41, for Win32 (ia32)
Copyright (C) 2002 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Usage: mysql [OPTIONS] [database]
-?, --help
Display this help and exit.
-I, --help
Synonym for -?
--auto-rehash
Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
-A, --no-auto-rehash
No automatic rehashing. One has to use 'rehash' to get
table and field completion. This gives a quicker start of
mysql and disables rehashing on reconnect. WARNING:
use --disable-auto-rehash instead.
-B, --batch
Don't use history file. Disable interactive behavior.
(Enables --silent)
--character-sets-dir=name
Directory where character sets are.
--default-character-set=name
Set the default character set.
-C, --compress
Use compression in server/client protocol.
-#, --debug[=#]
This is a non-debug version. Catch this and exit
-D, --database=name Database to use.
--delimiter=name
Delimiter to be used.
-e, --execute=name
Execute command and quit. (Disables --force and history
-E, --vertical
Print the output of a query (rows) vertically.
-f, --force
Continue even if we get an sql error.
-G, --named-commands
Enable named commands. Named commands mean this program's
see mysql& help . When enabled, the
named commands can be used from any line of the query,
otherwise only from the first line, before an enter.
Disable with --disable-named-commands. This option is
disabled by default.
-g, --no-named-commands
Named commands are disabled. Use \* form only, or use
named commands only in the beginning of a line ending
with a semicolon (;) Since version 10.9 the client now
starts with this option ENABLED by default! Disable with
'-G'. Long format commands still work from the first
line. WARNING: use
--disable-named-commands instead.
-i, --ignore-spaces Ignore space after function names.
--local-infile
Enable/disable LOAD DATA LOCAL INFILE.
-b, --no-beep
Turn off beep on error.
-h, --host=name
Connect to host.
-H, --html
Produce HTML output.
Produce XML output
--line-numbers
Write line numbers for errors.
-L, --skip-line-numbers
Don't write line number for errors. WARNING: -L is
deprecated, use long version of this option instead.
-n, --unbuffered
Flush buffer after each query.
--column-names
Write column names in results.
-N, --skip-column-names
Don't write column names in results. WARNING: -N is
deprecated, use long version of this options instead.
-O, --set-variable=name
Change the value of a variable. Please note that this
you can set variables directly with
--variable-name=value.
--sigint-ignore
Ignore SIGINT (CTRL-C)
-o, --one-database
Only update the default database. This is useful for
skipping updates to other database in the update log.
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty.
-W, --pipe
Use named pipes to connect to server.
-P, --port=#
Port number to use for connection.
--prompt=name
Set the mysql prompt to this value.
--protocol=name
The protocol of connection (tcp,socket,pipe,memory).
-q, --quick
Don't cache result, print it row by row. This may slow
down the server if the output is suspended. Doesn't use
history file.
Write fields without conversion. Used with --batch.
--reconnect
Reconnect if the connection is lost. Disable with
--disable-reconnect. This option is enabled by default.
-s, --silent
Be more silent. Print results with a tab as separator,
each row on new line.
--shared-memory-base-name=name
Base name of shared memory.
-S, --socket=name
Socket file to use for connection.
Enable SSL for connection (automatically enabled with
other flags). Disable with --skip-ssl.
--ssl-ca=name
CA file in PEM format (check OpenSSL docs, implies
--ssl-capath=name
CA directory (check OpenSSL docs, implies --ssl).
--ssl-cert=name
X509 cert in PEM format (implies --ssl).
--ssl-cipher=name
SSL cipher to use (implies --ssl).
--ssl-key=name
X509 key in PEM format (implies --ssl).
--ssl-verify-server-cert
Verify server's &Common Name& in its cert against
hostname used when connecting. This option is disabled by
-t, --table
Output in table format.
-T, --debug-info
Print some debug info at exit.
--tee=name
Append everything into outfile. See interactive help (\h)
also. Does not work in batch mode. Disable with
--disable-tee. This option is disabled by default.
Disable outfile. See interactive help (\h) also. WARNING:
use --disable-tee instead
-u, --user=name
User for login if not current user.
-U, --safe-updates
Only allow UPDATE and DELETE that uses keys.
-U, --i-am-a-dummy
Synonym for option --safe-updates, -U.
-v, --verbose
Write more. (-v -v -v gives the table output format).
-V, --version
Output version information and exit.
-w, --wait
Wait and retry if connection is down.
--connect_timeout=# Number of seconds before connection timeout.
--max_allowed_packet=#
Max packet length to send to, or receive from server
--net_buffer_length=#
Buffer for TCP/IP and socket communication
--select_limit=#
Automatic limit for SELECT when using --safe-updates
--max_join_size=#
Automatic limit for rows in a join when using
--safe-updates
--secure-auth
Refuse client connecting to server if it uses old
(pre-4.1.1) protocol
--show-warnings
Show warnings after every statement.
Default options are read from the following files in the given order:
C:\my.ini C:\my.cnf C:\WINDOWS\my.ini C:\f D:\MySQL\my.ini D:\f
The following groups are read: mysql client
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit
--no-defaults
Do not read default options from any options file
--defaults-file=# Only read default options from the given file #
--defaults-extra-file=# Read this file after the global files are read
Variables (--variable-name=value)
and boolean options {FALSE|TRUE}
Value (after reading options)
--------------------------------- -----------------------------
auto-rehash
character-sets-dir
(No default value)
default-character-set
(No default value)
named-commands
local-infile
(No default value)
line-numbers
unbuffered
column-names
sigint-ignore
shared-memory-base-name
(No default value)
(No default value)
(No default value)
ssl-capath
(No default value)
(No default value)
ssl-cipher
(No default value)
(No default value)
ssl-verify-server-cert
debug-info
(No default value)
safe-updates
i-am-a-dummy
connect_timeout
max_allowed_packet
net_buffer_length
select_limit
max_join_size
secure-auth
show-warnings
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:149624次
积分:2202
积分:2202
排名:第17306名
原创:70篇
转载:32篇
评论:17条
[ 职业 ]: java工程师
[ 总述 ]: 一个曾经以为可以改变世界的年轻人
[ 致自己 ]
世界有多残酷,
我们就该有多坚强!
[ 联系 ]: zhoudong_
(2)(3)(4)(1)(2)(2)(8)(6)(3)(1)(2)(1)(2)(2)(3)(11)(2)(3)(4)(11)(17)(12)

我要回帖

更多关于 如何用cmd连接数据库 的文章

 

随机推荐