ubuntu下初试mysql

本文详细介绍了如何安装、配置、使用MySQL数据库,包括创建数据库、表,设置权限,导入导出数据,以及基本的查询和操作技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


启动MYSQL服务 servic mysqld start

启动MYSQL网络 net start mysql
停止MYSQL
网络 net stop mysql

1.安装 mysql:
root@kklinux:~# sudo apt-get install mysql-server

主要配置文件位置:  /etc/mysql/my.cnf
数据库表文件夹:datadir= /var/lib/mysql/ 相当于Windows下MySQL的date文件夹

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.  Commands end with ; or \g.
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   table   student (name varchar(20),  sex char(1),
         -> birth date ,  addr varchar(20));
Query OK, 0 rows affected (0.07 sec)

再查看数据库:mysql> show tables;
发现多了一个<student>表。

11.查看<student>表的表结构:
mysql> desc student;
+-------+-----------------+-------+-----+---------+---------+
| Field  | Type              | Null  | Key | Default | Extra |
+-------+-----------------+-------+-----+---------+---------+
| name | varchar(20) | YES        | NULL              |
| sex     | char(1)         | YES        | NULL              |
| birth   | date              | YES        | NULL              |
| addr   | varchar(20) | YES        | NULL              |
+--------+-----------------+------+-----+----------+--------+
4 rows in set (0.00 sec)

12.向表中添加记录:
mysql> insert  into  student
         ->values('kk','f','1988-06-17','NanNing');

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

 load data infile 'TXT文件的完整路径' into table 表名;
 linux:TXT文件的完整路径=‘/tmp/kk.txt’
 windowns:TXT文件的完整路径='e:\\kk.txt'
 .txt的文件用Tab隔离(kk.txt 文件原本不存在)
 表中有auto_increment属性的字段,在.txt文件中使用null

ubuntu下初试mysqlselect  *   from  table_name  into  outfile  ' path/filename.txt ' ;

 

18.显示某个表创建时的全部信息 :

 show create table table_name;

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(选出1020)<第一个记录集的编号是0>
select * from students order by id limit 9,10;


查询时间:
select now();
查询当前用户:
select user();
查询数据库版本:
select version();
查询当前使用的数据库:
select database();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值