基于Linux的MySQL操作实例(软件安装,mysql基本操作,mysql数据类型,建表、插入数据操作)
前言
关于MySQL的概念性的东西,就在这里不多说了,本篇以实例操作为主,主要进行的操作有:MySQL软件安装,mysql基本操作,mysql数据类型,建表、插入数据操作。
欢迎各路大神批评指教,谢谢各位。
安装软件
- 源码安装,见脚本和源码包
- rpm包安装(需要手动安装依赖包)
本文使用的rpm安装包都是从MySQL官网下载的。
由于个人原因GitHub暂时不能上传,我将文件放在了百度云盘,有需要的朋友请前往下载。若是不能下载,请私信我。
MySQL5.6,点击下载
http://pan.baidu.com/s/1jIr11Sq
当使用rpm包安装时,可能会出现缺乏依赖包的情况,
可以使用先安装光盘自带的mysql-server,mysql,yum会自动安装依赖包等,
然后启动服务,检查端口号,由于mysql在默认的情况下,若不加载数据库将不能启动服务,但是通过光盘自带的软件包使用yum安装时,
这些配置都会自动做好。
然后卸载(卸载时需要添加--nodeps选项,忽略依赖关系)低版本mysql软件包,并删除主配置文件
[root@mysqlBak mysql_rpm]# yum -y install mysql-server mysql
[root@mysqlBak mysql_rpm]# service mysqld restart
停止 mysqld: [确定]
初始化 MySQL 数据库: WARNING: The host 'mysqlBak.wolf.cn' could not be looked up with resolveip.
……
Please report any problems with the /usr/bin/mysqlbug script!
[确定]
正在启动 mysqld: [确定]
[root@mysqlBak mysql_rpm]# netstat -anptu | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2310/mysqld
[root@stu ~]# service mysqld stop
停止 mysqld: [确定]
[root@mysqlBak mysql_rpm]# rm -rf /etc/my.cnf
[root@mysqlBak mysql_rpm]# ls /var/lib/mysql/
ibdata1 ib_logfile0 ib_logfile1 mysql test
[root@mysqlBak mysql_rpm]# rm -rf /var/lib/mysql/*
// 卸载时,注意添加忽略依赖关系选项,
[root@mysqlBak mysql_rpm]# rpm -e --nodeps mysql-server mysql
[root@mysqlBak mysql_rpm]# ls
MySQL-client-5.6.15-1.el6.x86_64.rpm MySQL-shared-5.6.15-1.el6.x86_64.rpm
MySQL-devel-5.6.15-1.el6.x86_64.rpm MySQL-shared-compat-5.6.15-1.el6.x86_64.rpm
MySQL-embedded-5.6.15-1.el6.x86_64.rpm MySQL-test-5.6.15-1.el6.x86_64.rpm
MySQL-server-5.6.15-1.el6.x86_64.rpm
//推荐使用U升级安装,可以替换冲突文件
[root@mysqlBak mysql_rpm]# rpm -Uvh MySQL-*
Preparing... ########################################### [100%]
1:MySQL-devel ########################################### [ 14%]
2:MySQL-client ########################################### [ 29%]
3:MySQL-test ########################################### [ 43%]
4:MySQL-embedded ########################################### [ 57%]
5:MySQL-shared-compat ########################################### [ 71%]
6:MySQL-shared ########################################### [ 86%]
7:MySQL-server ########################################### [100%]
///root/.mysql_secret' 生成的随机密码文件
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
……
[root@mysqlBak mysql_rpm]# /etc/init.d/mysql start
Starting MySQL. SUCCESS!
[root@mysqlBak mysql_rpm]# netstat -anptu | grep mysql
tcp 0 0 :::3306 :::* LISTEN 2565/mysqld
//查看文件内容,记录随机密码
[root@mysqlBak mysql_rpm]# cat /root/.mysql_secret
# The random password set for the root user at Mon Jan 16 11:31:07 2017 (local time): Xrf4Yrok
//使用随机密码先进行登录,然后进行修改密码等操作
[root@mysqlBak mysql_rpm]# mysql -hlocalhost -u root -pXrf4Yrok
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.15
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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement //需要先设置密码,然后进行其他操作
mysql> set password for root@"localhost"=password("123456");
Query OK, 0 rows affected (0.03 sec)
mysql> quit
Bye
//若没有其他配置需求可不进行该操作
[root@mysqlBak mysql_rpm]# cp /usr/share/mysql/my-default.cnf /etc/my.cnf
//可以直接制定数据库
[root@mysqlBak mysql_rpm]# mysql -u root -p123456 test
……
mysql> select database();
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
mysql> quit
Bye
//也可以不指定该数据库
[root@mysqlBak mysql_rpm]# mysql -u root -p123456
……
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
MySQL概述
基本信息
- 主配置文件 /etc/my.cnf
- 服务名 mysqld或mysql (根据不同版本略有不同)
- 进程名 mysqld或mysql
- 进程所有者/组 mysql/mysql
- 端口号 3306
- 传输协议 tcp
- 数据库目录 /var/lib/mysql/
数据库基本操作流程
1.连接数据库服务器 mysql
2.选择库 use 库名;
3.创建表
4.向表中插入记录 insert into
5.曾删改查等数据操作
6.断开连接 quit
数据以文件的形式存储在数据库目录下
数据库基本使用概述
操作指令类型
- MySQL指令:环境切换,看状态,退出等控制
- SQL指令:数据库定义/查询/操纵/授权语句
基本操作注意事项
- 操作指令不区分大小写
- 每条SQL语句以;结束或分隔
- 不支持tab键自动补齐
- \c可废弃当前编写错的操作指令
数据库名称命名规则
- 具有唯一性
- 区分字母大小写
- 只能使用数字、字母、“_”
- 不能是纯数字
- 不要使用特殊字符和关键字
操作命令基本格式
show databases; 显示已有的库
create database 库名;
use 库名; 切换库
select database(); 查看当前所在的库
drop database 库名; 删除已有的库
show tables; 显示当前所在库下已有的表
创建表(表存放在库里)
create table 库名.表名(
字段名1 类型,
字段名2 类型,
字段名3 类型
);
select 字段名列表 from 表名; 查看表记录
desc 表名; 查看表结构
insert into 表名 values(值1,值2……);向表中插入记录
delete from 表名; 删除表中的所有记录
drop table 表名; 删除表
建表的语法格式
create table 表名(
字段名 类型(宽度) 约束条件,
字段名 类型(宽度) 约束条件,
字段名 类型(宽度) 约束条件
);
mysql数据库类型概述
字符类型 (eg:姓名 家庭地址)
char 定长 255
varchar 变长 255+
65532
大文本类型
blob
text
数值类型 (eg:工资,成绩,年龄,身高,体重)
整数类型
根据存储数值的范围又分为:
tinyint
smallint
MEDIUMINT
int
bigint浮点型
float double
单精度 双精度
float(n,m)
double(n,m)
n 表示总位数
m 表小数位位数
整数.小数
1023.77
数值类型的宽度是显示宽度,不能够控制给字段赋值的大小,字段值的大小由字段类型决定。
-
日期时间类型 (eg:生日,注册时间,入职时间)
年 year YYYY 2016
01-69 20XX
70-99 19XX
00 0000
日期 date YYYYMMDD 20161219
时间 time HHMMSS 144518
日期时间 ( 约会时间 )
datetime / timestamp
YYYYMMDDHHMMSS
20170214183018datetime 与 timestamp 的区别?
当不给timestamp类型的字段赋值时,用系统当前的时间给字段赋值。使用时间函数获取时间给日期时间类型字段赋值?
now() 获取当前系统时间
year() 获取年份
date() 获取日期
month() 获取月份
day() 获取日期(几号)
time() 获取时间 枚举类型 (eg:爱好,性别,专业 )
字段的值只能在列举的范围内选择
enum(值列表) 单选
set(值列表) 多选查看建表过程
show create table 表名;
MySQL操作实例
[root@mysqlBak mysql_rpm]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
……
//查看当前数据库
//以下都是mysql自带数据库,不要轻易修改系统自带数据库,以免出现错误
//mysql是授权库,test是测试库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
//创建新的数据库
mysql> create database user_list;
Query OK, 1 row affected (0.00 sec)
//查看当前使用的数据库,若是在登录mysql时指定时,当前即可看到指定的数据库
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
//更改当前使用的数据库
mysql> use user_list;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| user_list |
+------------+
1 row in set (0.00 sec)
mysql> ls
-> \c
mysql> show tables;
Empty set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| user_list |
+--------------------+
5 rows in set (0.00 sec)
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
mysql> show tables
-> ;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)
//切换到我们新建的数据库,
mysql> use user_list;
Database changed
//查看当前数据库内的表,结果为空
mysql> show tables;
Empty set (0.00 sec)
//使用SQL指令创建name_list表
mysql> create table name_list ( n_id int(10) primary key, name char(10), age int(3), sex enum("boy","girl") );
Query OK, 0 rows affected (0.36 sec)
//查看name_list表结构
mysql> desc name_list
-> ;
+-------+--------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------------+------+-----+---------+-------+
| n_id | int(10) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| sex | enum('boy','girl') | YES | | NULL | |
+-------+--------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
//插入数据
mysql> insert into name_list values(
-> 1,"tom",14,"boy");
Query OK, 1 row affected (0.05 sec)
//查看name_list内全部数据
mysql> select * from name_list;
+------+------+------+------+
| n_id | name | age | sex |
+------+------+------+------+
| 1 | tom | 14 | boy |
+------+------+------+------+
1 row in set (0.01 sec)
//字符类型实例
mysql> create table t1(name varchar(256));
mysql> create table t7(name char(3),age tinyint);
mysql> insert into t7 values("jim",19);
mysql> create table t10(name char(3),age tinyint unsigned);
mysql> create table t11(name char(3), level int(3));
mysql> insert into t11 values("bob",1024);
mysql> create table t12(level1 int(3) zerofill,level2 int(7) zerofill);
mysql> create table t13 (name char(10),age tinyint(2) unsigned , pay float(7,2));
mysql> insert into t13 values("bob",21,18000.23);
//时间函数实例
mysql> create table t14 (name char(10),age tinyint(2) unsigned , pay float(7,2),s_year year,birthday date,up_class time,meetting datetime);
mysql> insert into t14 values("bob",21,18000,1991,20160718,083000,20160818180000);
mysql> insert into t14 values("tom",29,28000,now(),now(),now(),now());
mysql> select year(20170918);
+----------------+
| year(20170918) |
+----------------+
| 2017 |
+----------------+
1 row in set (0.03 sec)
mysql> select year(20190918);
+----------------+
| year(20190918) |
+----------------+
| 2019 |
+----------------+
1 row in set (0.00 sec)
mysql> select year( now() );
+---------------+
| year( now() ) |
+---------------+
| 2016 |
+---------------+
1 row in set (0.00 sec)
mysql> select day( now() );
+--------------+
| day( now() ) |
+--------------+
| 17 |
+--------------+
1 row in set (0.00 sec)
mysql> select month( now() );
+----------------+
| month( now() ) |
+----------------+
| 6 |
+----------------+
1 row in set (0.00 sec)
mysql> select time( now() );
+---------------+
| time( now() ) |
+---------------+
| 09:14:38 |
+---------------+
1 row in set (0.00 sec)
mysql> insert into t14 values("lucy",29,28000,year(20190817),now(),time(now()),now());
mysql> select sleep(2);
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2.01 sec)
mysql> insert into t14(s_year)values(00);
Query OK, 1 row affected (0.07 sec)
mysql> select s_year from t14;
+--------+
| s_year |
+--------+
| 1991 |
| 2016 |
| 2019 |
| 0000 |
+--------+
4 rows in set (0.00 sec)
mysql> insert into t14(s_year)values(100);
ERROR 1264 (22003): Out of range value for column 's_year' at row 1
mysql> create table t15(time1 datetime ,time2 timestamp);
mysql> insert into t15 values(now(),now());
mysql> insert into t15(time2) values(20160617173423);
//枚举类型实例
mysql> create table t16(
-> name char(10),
-> sex enum("boy","girl","no"),
-> likes set("moneny","girl","book","film")
);
mysql> insert into t16 values("bob","man","A,football");
mysql> desc t16;
+-------+------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------------------------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','no') | YES | | NULL | |
| likes | set('moneny','girl','book','film') | YES | | NULL | |
+-------+------------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into t16 values("bob","man","A,football");
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
mysql> insert into t16 values("bob","boy","A,football");
ERROR 1265 (01000): Data truncated for column 'likes' at row 1
mysql> insert into t16 values("bob","boy","moneny,book");
Query OK, 1 row affected (0.04 sec)
mysql> select * from t16;
+------+------+-------------+
| name | sex | likes |
+------+------+-------------+
| bob | boy | moneny,book |
+------+------+-------------+
1 row in set (0.00 sec)
mysql> insert into t16 values("lucy",2,"book");
mysql> insert into db1.t16 values(null,null,null)
mysql> create table t17(
->name char(10) not null,
->age tinyint(2) unsigned default 21,
->sex enum("boy","girl","no") not null default "boy",
->likes set("moneny","girl","book","film") not null
->default "moneny,girl");
mysql> insert into t17(name)values("bob");
mysql> insert into t17 values("lucy",23,"no","book,film");
mysql> insert into t17 values(NULL,23,"no","book,film");
mysql> insert into t17 values(NULL,NULL,"no","book,film");
ERROR 1048 (23000): Column 'name' cannot be null
mysql> insert into t17 values("NULL",NULL,"no","book,film");
Query OK, 1 row affected (0.03 sec)