[文章作者:张宴 本文版本:v1.0 最后修改:2007.08.06 转载请注明出处:http://blog.zyan.cc]

  我一直习惯用phpMyAdmin对MySQL数据库进行管理,曾修改了一个《可管理多台远程MySQL服务器的phpMyAdmin 2.10.2》。但有些机器上因为安全等因素,只设置了允许从本机访问自身的MySQL端口,而且没有安装Apache和PHP,因而不能使用phpMyAdmin。于是,我只好使用命令行方式来对MySQL数据库进行管理。为了方便以后从命令行操作数据库可以更方便(拷贝、粘贴),我写下了这篇文章。

一、从命令行登录MySQL数据库服务器
1、登录使用默认3306端口的MySQL
/usr/local/mysql/bin/mysql -u root -p

2、通过TCP连接管理不同端口的多个MySQL(注意:MySQL4.1以上版本才有此项功能)
/usr/local/mysql/bin/mysql -u root -p --protocol=tcp --host=localhost --port=3307

3、通过socket套接字管理不同端口的多个MySQL
/usr/local/mysql/bin/mysql -u root -p --socket=/tmp/mysql3307.sock

4、通过端口和IP管理不同端口的多个MySQL
/usr/local/mysql/bin/mysql -u root -p -P 3306 -h 127.0.0.1



二、数据库操作SQL语句
1、显示服务器上当前存在什么数据库
SHOW DATABASES;

2、创建名称为rewin的数据库
CREATE DATABASE rewin;

3、删除名称为rewin的数据库
DROP DATABASE rewin;

4、选择rewin数据库
USE rewin;



三、表操作SQL语句(登录之后必须用以上的USE命令选择一个数据库,再进行表操作)
1、显示当前数据库中存在什么表
SHOW TABLES;

2、创建数据库表zhangyan:在mysql>后粘贴以下SQL语句,存储引擎为MYISAM,字段id为主键、唯一索引。
CREATE TABLE `zhangyan` (
`id` INT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 20 ) NOT NULL ,
`password` CHAR( 32 ) NOT NULL ,
`time` DATETIME NOT NULL ,
`number` FLOAT( 10 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;

3、查看zhangyan表结构
DESCRIBE zhangyan;

4、从表中检索信息
4.1、从zhangyan表中检索所有记录
SELECT * FROM zhangyan;

4.2、从zhangyan表中检索特定的行:字段username等于abc,字段number等于1,按字段id降序排列
SELECT * FROM zhangyan WHERE username = 'abc' AND number='1' ORDER BY id DESC;

4.3、从zhangyan表中检索指定的字段:username和password
SELECT username, password FROM zhangyan;

4.4、从zhangyan表中检索出唯一的不重复记录:
SELECT DISTINCT username FROM zhangyan;

5、插入信息到zhangyan表
INSERT INTO zhangyan (id, username, password, time, number, content) VALUES ('', 'abc', '123456', '2007-08-06 14:32:12', '23.41', 'hello world');

6、更新zhangyan表中的指定信息
UPDATE zhangyan SET content = 'hello china' WHERE username = 'abc';

7、删除zhangyan表中的指定信息
DELETE FROM zhangyan WHERE id = 1;

8、清空zhangyan表
DELETE FROM zhangyan;

9、删除zhangyan表
DROP TABLE zhangyan;

10、更改表结构,将zhangyan表username字段的字段类型改为CHAR(25)
ALTER TABLE zhangyan CHANGE username username CHAR(25);

11、将当前目录下的mysql.sql导入数据库
SOURCE ./mysql.sql;



四、数据库权限操作SQL语句
1、创建一个具有root权限,可从任何IP登录的用户sina,密码为zhangyan
GRANT ALL PRIVILEGES ON *.* TO 'sina'@'%' IDENTIFIED BY 'zhangyan' WITH GRANT OPTION;
FLUSH   PRIVILEGES;

2、创建一个具有“数据操作”、“结构操作”权限,只能从192.168.1.***登录的用户sina,密码为zhangyan
GRANT SELECT , INSERT , UPDATE , DELETE , FILE , CREATE , DROP , INDEX , ALTER , CREATE TEMPORARY TABLES , CREATE VIEW , SHOW VIEW , CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON *.* TO 'sina'@'192.168.1.%' IDENTIFIED BY 'zhangyan';

3、创建一个只拥有“数据操作”权限,只能从192.168.1.24登录,只能操作rewin数据库的zhangyan表的用户sina,密码为zhangyan
GRANT SELECT , INSERT , UPDATE , DELETE ON  rewin.zhangyan TO 'sina'@'192.168.1.24' IDENTIFIED BY 'zhangyan';

4、创建一个拥有“数据操作”、“结构操作”权限,可从任何IP登录,只能操作rewin数据库的用户sina,密码为zhangyan
GRANT SELECT , INSERT , UPDATE , DELETE , CREATE , DROP , INDEX , ALTER , CREATE TEMPORARY TABLES , CREATE VIEW , SHOW VIEW , CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON rewin.* TO 'sina'@'%' IDENTIFIED BY 'zhangyan';

5、删除用户
DROP USER 'sina'@'%';


PS:如果想了解更多的MySQL操作资料,请参考MySQL官方的中文参考手册


Tags: , ,



技术大类 » 数据库技术 | 评论(55) | 引用(7) | 阅读(56821)
易发国际真人娱乐场所
[2014-5-21 07:11]
来源:易发国际
内容: 易发国际出于亚洲著名公司专业提供,易发国际真人娱乐,百家乐,轮盘,骰宝,龙虎,路单,棋牌,游乐场等易发国际精彩娱乐。信用第一值得大众信赖。
易发国际真人娱乐场所
[2014-5-21 07:16]
来源:易发国际
内容: 易发国际出于亚洲著名公司专业提供,易发国际真人娱乐,百家乐,轮盘,骰宝,龙虎,路单,棋牌,游乐场等易发国际精彩娱乐。信用第一值得大众信赖。
易发国际真人娱乐场所
[2014-5-21 07:16]
来源:易发国际
内容: 易发国际出于亚洲著名公司专业提供,易发国际真人娱乐,百家乐,轮盘,骰宝,龙虎,路单,棋牌,游乐场等易发国际精彩娱乐。信用第一值得大众信赖。
易发国际真人娱乐场所
[2014-5-21 07:17]
来源:易发国际
内容: 易发国际出于亚洲著名公司专业提供,易发国际真人娱乐,百家乐,轮盘,骰宝,龙虎,路单,棋牌,游乐场等易发国际精彩娱乐。信用第一值得大众信赖。
猫爪子
2016-3-19 17:20
通过socket套接字管理不同端口的多个MySQL/usr/local/mysql/bin/mysql -u root -p --socket=/tmp/mysql3307.sock谢谢 .  我正需要这个.
购物搜索联盟曾伊材伍
[2016-3-21 01:00]
来源:钱丙厘冽
内容:购物客,中国第一家购物搜索联盟。现已收录当当、卓越、京东、新蛋、红孩子、DHC、玛萨玛索、VANCL、金象网等国内最具品质的B2C商家,致力于向广大消费者提供最便捷的网购入口、让用户轻松找到低价正品。http://sounion.gouwuke.com/
可以发外链发广告贴的广告专区论坛
[2016-3-21 13:56]
来源:邓享舌吟
内容:最爱广告论坛,哪些论坛可以发广告?最爱广告论坛可以发免费广告,这是一个可以发外链发广告贴的广告专区论坛。欢迎各位企业站长、seoer前来发布企业广告软文外链SEO优化信息。 http://www.zuiailuntan.com/
可以发外链发广告贴的广告专区论坛
[2016-3-21 19:25]
来源:刘孟佳幽
内容:最爱广告论坛,哪些论坛可以发广告?最爱广告论坛可以发免费广告,这是一个可以发外链发广告贴的广告专区论坛。欢迎各位企业站长、seoer前来发布企业广告软文外链SEO优化信息。 http://www.zuiailuntan.com/
shzz Email
2022-7-4 19:25
I’ve read some good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website.  Digital real estate business
shz Email
2022-7-4 19:27
I think this is a standout amongst the most critical data for me. What"s more, i"m happy perusing your article. Be that as it may, ought to comment on some broad things  Digital real estate business
shzz Email
2022-7-4 19:28
Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.  Digital real estate business
shz Email
2022-7-4 19:31
Hi, I log on to your new stuff like every week. Your humoristic style is witty, keep it up  Digital real estate business
shzzzz Email
2022-7-4 19:31
Thank you so much for ding the impressive job here, everyone will surely like your post.  Digital real estate business
shzzzz Email
2022-7-4 19:34
A great website with interesting and unique material what else would you need.  Digital real estate business
shz Email
2022-7-4 19:35
I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.  Digital real estate business
shzzzz Email
2022-7-4 19:37
I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up..  Digital real estate business
shz Email
2022-7-4 19:39
I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.  Digital real estate business
shz Email
2022-7-4 19:39
You actually make it look so easy with your performance but I find this matter to be actually something which I think I would never comprehend. It seems too complicated and extremely broad for me. I'm looking forward for your next post, I’ll try to get the hang of it!  Digital real estate business
shz Email
2022-7-4 19:42
It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips. Perhaps you could write next articles referring to this article. I want to read more things about it!  Digital real estate business
Lucky cola
2024-2-7 16:03
Embark on epic quests and conquer virtual worlds.  Lucky cola
分页: 3/4 第一页 上页 1 2 3 4 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]