您好,欢迎访问一九零五行业门户网

MySQL 数据库简单操作

对于想要从事或爱好mysql相关工作的童鞋们,有必要掌握在命令行下对mysql实现一些简单的操作。本文从描述了如何登录到mysql数据库
对于想要从事或爱好mysql相关工作的童鞋们,有必要掌握在命令行下对mysql实现一些简单的操作。本文从描述了如何登录到mysql数据库服务器,如何在mysql提示符下发布命令,创建数据库,,以及执行一些简单的dml操作。
1、连接到与退出mysql
为了连接mysql数据库服务器,当调用mysql时,通常需要提供一个mysql用户名并且很可能需要一个密码。如果服务器
运行在登录服务器之外的其它机器上,还需要指定主机名。联系管理员以找出进行连接所使用的参数 (即,连接的主机
、用户名和使用的密码)。知道正确的参数后,可以按照以下方式进行连接:
shell> mysql -h host -u user -p
mysql> select version(),current_date;
+---------------------------------------+--------------+
| version()                            | current_date |
+---------------------------------------+--------------+
| 5.6.17-enterprise-commercial-advanced | 2014-04-28  |
+---------------------------------------+--------------+
1 row in set (0.03 sec)
--在允许匿名登录到本地服务器的情下可以直接在shell提示符下直接输入mysql即可实现登录
mysql> #提示符告诉你mysql准备为你输入命令。
shell> mysql
--输入分号表示结束命令输入并执行该命令
--成功地连接后,可以在mysql>提示下输入quit (或\q ,exit)随时退出
mysql> quit
bye
--在unix中,也可以按control-d键断开服务器。
--------------------------------------分割线 --------------------------------------
ubuntu 14.04下安装mysql
《mysql权威指南(原书第2版)》清晰中文扫描版 pdf
ubuntu 14.04 lts 安装 lnmp nginx\php5 (php-fpm)\mysql
ubuntu 14.04下搭建mysql主从服务器
ubuntu 12.04 lts 构建高可用分布式 mysql 集群
ubuntu 12.04下源代码安装mysql5.6以及python-mysqldb
mysql-5.5.38通用二进制安装
--------------------------------------分割线 --------------------------------------
2、发布命令
mysql执行命令可分为非交互与交互模式
a) 非交互模式
非交互模式,也叫批模式,也就是将想要运行的命令放在一个文件中,然后告诉mysql从文件读取它的输入。
通常用于返回数据量较大,以及批量管理,执行特殊脚本运行的情形。
shell> mysql [root@linux1 ~]# more query.sql
show databases;
use cnfo
select * from tb_tmp;
[root@linux1 ~]# mysql -u root -pmysql warning: using a password on the command line interface can be insecure.
database
information_schema
cnfo
mysql
performance_schema
test
name    sex    birth
jack    f      2014-04-28
john    m      2013-04-28
--也可以使用下面的2种方式来执行批
mysql > source /
/filename
mysql > \.//finename     
--如下面的演示
[root@linux1 ~]# mysql -u root -pmysql
mysql> source query.sql
+--------------------+
| database          |
+--------------------+
| information_schema |
| cnfo              |
| mysql              |
| performance_schema |
| test              |
+--------------------+
5 rows in set (0.00 sec)
reading table information for completion of table and column names
you can turn off this feature to get a quicker startup with -a
database changed
+------+------+------------+
| name | sex  | birth      |
+------+------+------------+
| jack | f    | 2014-04-28 |
| john | m    | 2013-04-28 |
+------+------+------------+
2 rows in set (0.00 sec)
也可以在shell模式下直接执行sql,如下面的方法:
-e or --execution=option 
shell>mysql -e sql cmd1;sql cmd2;..
shell>mysql --execute=sql cmd1;sql cmd2;..
b) 交互模式
交互模式就是直接在mysql提示符下发布命令并执行命令。
如下操作,不区分大小写,输入回车后会得到命令执行的结果,即为交互模式。
mysql> select version(), current_date;
mysql> select version(), current_date;
mysql> select version(), current_date;
--简单计算
mysql> select power(2,3),(5-1)*4;
+------------+---------+
| power(2,3) | (5-1)*4 |
+------------+---------+
|          8 |      16 |
+------------+---------+
1 row in set (0.00 sec)
--分号分割多行
mysql> select version();select current_date;
+---------------------------------------+
| version()                            |
+---------------------------------------+
| 5.6.17-enterprise-commercial-advanced |
+---------------------------------------+
1 row in set (0.01 sec)
+--------------+
| current_date |
+--------------+
| 2014-04-28  |
+--------------+
1 row in set (0.00 sec)
--换行输入命令
--注,可以输入空行
mysql> select user(),
    -> current_date;
+----------------+--------------+
| user()        | current_date |
+----------------+--------------+
| root@localhost | 2014-04-28  |
+----------------+--------------+
1 row in set (0.00 sec)
--取消执行当前命令
mysql> select current_date()\c
更多详情见请继续阅读下一页的精彩内容:
其它类似信息

推荐信息