启动MYSQL服务 servic
启动MYSQL网络 net start mysql
停止MYSQL网络
1.安装 mysql:
root@kklinux:~# sudo apt-get install mysql-server
主要配置文件位置:
数据库表文件夹:datadir= /var/lib/mysql/
2.安装 mysql后在终端输入命令 :
$ mysql -s root -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
3.应开启服务:
$ sudo /etc/init.d/mysql start
4.指定用户-u(user),密码-p(password)进入mysql:
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.
Your MySQL connection id is 44
Server version: 5.1.54-1ubuntu4 (Ubuntu)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
5.配置MySql:
mysql> GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "root";
6.创建数据库:
mysql> create database mydb;
Query OK, 1 row affected (0.00 sec)
7.显示数据库:
mysql> show databases;
+ - - - - - - - - - - - - - - - - +
| Database
+ - - - - - - - - - - - - - - - - +
| information_schema
| mydb
| mysql
+ - - - - - - - - - - - - - - - - +
3 rows in set (0.00 sec)
8.选择操作的数据库:
mysql> use mysql;
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
9.查看数据库中的表(如以上的mysql数据库):
mysql> show tables;
+ - - - - - - - - - - - - - - - -- -
| Tables_in_mysql
+ - - - - - - - - - - - - - - - -- -
| columns_priv
| db
| event
| func
| general_log
| help_category
| help_keyword
| help_relation
| help_topic
| host
| ndb_binlog_index
| plugin
| proc
| procs_priv
| servers
| slow_log
| tables_priv
| time_zone
| time_zone_leap_second
| time_zone_name
| time_zone_transition
| time_zone_transition_type |
| user
+ - - - - - - - - - - - - - - - - - - - - +
23 rows in set (0.00 sec)
10.创建一个数据库表<student>:(只要不是分号<;>结束,回车换行可继续输入)
mysql> create
Query OK, 0 rows affected (0.07 sec)
再查看数据库:mysql> show tables;
发现多了一个<student>表。
11.查看<student>表的表结构:
mysql> desc student;
+-------+-----------------+-------+-----+---------+---------+
| Field
+-------+-----------------+-------+-----+---------+---------+
| name | varchar(20) | YES
| sex
| birth
| addr
+--------+-----------------+------+-----+----------+--------+
4 rows in set (0.00 sec)
12.向表中添加记录:
mysql> insert
Query OK, 1 row affected (0.05 sec)13.加载文档中的数据:
mysql.txt内容为:
-----------------------------
aa f 1977-07-07 shanghai
bb f 1978-12-12 guangzhou
dd m 1970-09-02 guangxi
-----------------------------
mysql> load data local infile "mytable.txt" into table student;
15.查看表中的数据:
mysql> select * from student;
16.显示变量:
show variables like 'character_set_%';
17.导入.TXT文件与导出*.TXT

18.显示某个表创建时的全部信息 :
19.显示当前实例信息:
status;
20.查看数据库或表的创建代码:
show create database mysql;
show create table db;
删除student_course数据库中的students数据表:
$ rm -f student_course/students.*
备份数据库:(将数据库test备份)
$ mysqldump -u root -p test>c:\test.txt
$ mysqldump -u root -p mydbname > /home/kk/mydb_dump.2011-10-29
备份表格:(备份test数据库下的mytable表格)
$ mysqldump -u root -p test mytable>c:\test.txt
将备份数据导入到数据库:(导回test数据库)
$ mysql -u root -p test
mysql> backup table test to 'c:';
mysql> restore table test from 'c:';
创建临时表:(建立临时表zengchao)
create temporary table zengchao(name varchar(10));
创建表是先判断表是否存在
create table if not exists students(……);
从已经有的表中复制表的结构
create table table2 select * from table1 where 1<>1;
复制表
create table table2 select * from table1;
对表重新命名
alter table table1 rename as table2;
修改列的类型
alter table table1 modify id int unsigned;//修改列id的类型为int unsigned
alter table table1 change id sid int unsigned;//修改列id的名字为sid,而且把属性修改为int
unsigned
创建索引
alter table table1 add index ind_id (id);
create index ind_id on table1 (id);
create unique index ind_id on table1 (id);//建立唯一性索引
删除索引
drop index idx_id on table1;
alter table table1 drop index ind_id;
联合字符或者多个列(将列id与":"和列name和"="连接)
select concat(id,':',name,'=') from students;
limit(选出10到20条)<第一个记录集的编号是0>
select * from students order by id limit 9,10;
查询时间:select now();
查询当前用户:select user();
查询数据库版本:select version();
查询当前使用的数据库:select database();