MySQL Server,即真正的MySQL服务器附赠一个MySQL Client程序。这是一个命令行客户端,可以通过MySQL Client登录MySQL,然后,输入SQL语句并执行。
通过TCP进行连接和通信
MySQL Client的可执行程序是mysql,MySQL Server的可执行程序是mysqld
本地连接。默认端口号是3306,即如果发送到本机MySQL Server,地址就是127.0.0.1:3306。
也可以只安装MySQL Client,然后连接到远程MySQL Server。假设远程MySQL Server的IP地址是10.0.1.99,那么就使用-h指定IP或域名:
mysql -h 10.0.1.99 -u root -p
使用MySQL client连接mysql服务器
[wangguowei@master ~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
配置用户登录主机的限制。设置为%就是任意主机可以登录。
本质上,MySQL Workbench和MySQL Client命令行都是客户端,和MySQL交互,唯一的接口就是SQL。
在交互窗口中创建删除数据库
mysql> create database wangguowei;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hive_168 |
| mysql |
| test |
| test_db |
| wangguowei |
+--------------------+
6 rows in set (0.00 sec)
对一个数据库进行操作时,要首先将其切换为当前数据库:
use test;
查询当前选定数据库中包含的数据表
show tables;
查询某一个数据表的表结构
mysql> desc students;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| class_id | bigint(20) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| gender | varchar(1) | NO | | NULL | |
| score | int(11) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
舒服了!
| students | CREATE TABLE `students` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`class_id` bigint(20) NOT NULL,
`name` varchar(100) NOT NULL,
`gender` varchar(1) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
命令修改数据表ALTER TABLE students ADD COLUMN birth VARCHAR(10) NOT NULL;
ALTER TABLE students CHANGE COLUMN birth birthday VARCHAR(20) NOT NULL;
ALTER TABLE students DROP COLUMN birthday;
使用EXIT命令退出MySQL:
mysql> EXIT
Bye
注意EXIT仅仅断开了客户端和服务器的连接,MySQL服务器仍然继续运行。