您的位置:首页 > 数据库 > MySQL

MySQL第一天

2018-02-22 19:23 387 查看
DATABASE day1:
一、搭建数据库服务器
二、数据库服务的基本使用
三、MySQL数据类型
四、管理记录
五、用户授权与权限撤销
六、数据备份与恢复
七、MySQL主从同步
八、数据读写分离
九、MySQL优化
十、MySQL 集群
#######################
一、搭建数据库服务器
数据库服务器是用来存储数据
1、购买服务器的硬件配置:存储 CPU 内存
2、安装操作系统:Linux UNIX Windows
3、安装提供数据库服务的软件包:
3.1有哪些类型的软件:mysql mariadb sql-server db2
3.2来源:官网下载 系统安装光盘自带的
3.3类型:RPM(不可修改) 源码包(可自定义)
3.4软件包的开源是否跨平台

安装前准备
基本需要
-1.采用RHEL7.2系统搭建MySQL服务器
-2.关闭防火墙 systemctl stop firewalld
-3.关闭selinux sed 's/=enforcing/=permissive/g' /etc/selinux/config
-4.软件mysql-5.7.17-1
准备工作
停止mariadb服务
删除文件

1.安装软件包
[root@db1 09.mysql]# rm -rf mysql-community-server-minimal-5.7.17-1.el7.x86_64.rpm

[root@db1 09.mysql]# ls mysql-*.rpm

[root@db1 09.mysql]# rpm -Uvh mysql-community-*.rpm 安装,如果存在旧版本 就升级 ,也可以查看需要哪些依赖包
警告:mysql-community-client-5.7.17-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
perl(Data::Dumper) 被 mysql-community-test-5.7.17-1.el7.x86_64 需要
perl(JSON) 被 mysql-community-test-5.7.17-1.el7.x86_64 需要

[root@db1 09.mysql]# yum list |grep "perl-Data-Dumper"
perl-Data-Dumper.x86_64 2.145-3.el7 192.168.4.254_rhel7
[root@db1 09.mysql]# yum list |grep "perl-JSON"
perl-JSON.noarch 2.59-2.el7 192.168.4.254_rhel7
perl-JSON-PP.noarch 2.27202-2.el7 192.168.4.254_rhel7
[root@db1 09.mysql]# yum -y install perl-Data-Dumper.x86_64 perl-JSON.noarch perl-JSON-PP.noarch
[root@db2 09.mysql]# rpm -Uvh mysql-community-*.rpm

2.启动服务
systemctl start mysqld
systemctl enable mysqld

3.与MySQL数据库服务相关信息
进程名 mysqld
进程所有者和所属组 mysql mysql
默认端口号 3306
传输协议 TCP
配置文件 /etc/my.cnf
数据库目录 /var/lib/mysql
日志文件 /var/log/mysqld.log (存放初始密码)

使用数据库管理员root用户初始密码连接数据库服务
[root@db1 09.mysql]# grep -i password /var/log/mysqld.log
2018-02-22T03:21:32.096060Z 1 [Note] A temporary password is generated for root@localhost: ;Go-_qk(2Jon
[root@db1 09.mysql]# which mysql
[root@db1 09.mysql]# mysql -uroot -p";Go-_qk(2Jon"
mysql>
mysql> quit //断开连接

设置数据库管理员root用户本机登录密码
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)

mysql> alter user root@localhost identified by "123456";
Query OK, 0 rows affected (0.00 sec)

mysql> quit

vim /etc/my.cnf #永久生效密码
[mysqld]
validate_password_policy=0
validate_password_length=6

[root@db1 09.mysql]# systemctl stop mysqld.service
[root@db1 09.mysql]# systemctl start mysqld.service

[root@db1 09.mysql]# mysql -uroot -p123456
mysql> show databases;

二、数据库服务的基本使用
把数据存储到数据库服务器上过程
1.连接数据库服务器
不作授权的时候只允许数据库管理员root用户在数据库本机连接数据库服务
mysql -hlocalhost -uroot -p123456
2.创建库(文件夹)
mysql> create database 库名; ##等于在/var/lib/mysql创建了一个目录
mysql> system ls /var/lib/mysql ##可以用linux命令,不用分号
mysql> select database(); ##类似linux中的pwd
mysql> use 库名; ##进入库
mysql> drop database 库名; ##删除库
mysql> drop database 库名\c ##终止继续打命令
3.建表(系统文件)
表必须保存在库里。
mysql> use mysql; ##进入库
mysql> show tables; ##查看库里面所有表
mysql> select * from user; ##查看表中所有列的内容
mysql> select user,host from user; 查看表中user,host列内容
mysql> desc user; ##查看表中有什么列,什么类型
mysql> select user,host,select_priv from user; 查看表中select_priv,user,host列内容
###############################################################3
建表命令
行(记录)
列(字段名)
把学生信息保存到gamedb库里
name age
tom 19
jim 21

mysql> create table gamedb.stu( name char(10),age int(2)); ##创建表中的列、类型,列为name,age,类型为char,int。
Query OK, 0 rows affected (0.19 sec)

mysql> use gamedb; ##进入库

mysql> show tables; ##查看表库中所有表
+------------------+
| Tables_in_gamedb |
+------------------+
| stu |
+------------------+
1 row in set (0.00 sec)

mysql> desc stu;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+

mysql> select * from stu;
Empty set (0.00 sec)

mysql> insert into stu values("jim",21); ##插入表中列内的内容,name为jim,age为21
mysql> insert into stu values("tom",19);

mysql> select * from stu; ##查看stu表中所有列的内容(值)
+------+------+
| name | age |
+------+------+
| jim | 21 |
| tom | 19 |
+------+------+
2 rows in set (0.00 sec)

mysql> select name from stu; ##查看stu表中name列的内容(值)
+------+
| name |
+------+
| jim |
| tom |
+------+
2 rows in set (0.00 sec)

mysql> delete from stu; ##删除表中所有列内的内容(值)
Query OK, 2 rows affected (0.06 sec)

mysql> select * from stu; ##查看表在所有列中的内容(值)
Empty set (0.00 sec)

mysql> drop table stu; ##删除表
Query OK, 0 rows affected (0.12 sec)

mysql> drop database gamedb; ##删除库
Query OK, 0 rows affected (0.00 sec)

管理数据库服务使用的时sql命令;
sql命令使用规则?
sql命令类型

三、MySQL数据类型
1.整数型 (数值类型的宽度只是显示几位数,不能限制数值的大小,如果不指定宽度默认位11位,所以尽量指明宽度(节省空间),不然会空格补位)
数值不够指定宽度时,左边空格补位
宽度仅显示宽度,存数值的大小由类型决定
使用关键字ZEROFILL时,填0代替空格补位
类型 大小 范围(有符合) 范围(无符合) 用途
tinyint 1字节 -128~127 255 微小整数
smallint 2字节 -32768~32767 0~65535 小整数
mediumint 3字节 -2^23~2^23-1 0~2^24-1 中整数
int 4字节 -2^31~2^31-1 0~2^32-1 大整数
bigint 8字节 -2^63~2^63-1 0~2^64-1 极大整数
float 4字节 单精度浮点数
double 8字节 双精度浮点数

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> use db1;
Database changed

mysql> create table db1.t2(name char(4) ,age tinyint); ##无符号表示 age tinyint unsigned
Query OK, 0 rows affected (0.41 sec)

mysql> desc t2;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | char(4) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into t2 values("tom",127);
Query OK, 1 row affected (0.04 sec)

mysql> select * from t2;
+------+------+
| name | age |
+------+------+
| tom | 127 |
+------+------+
1 row in set (0.00 sec)

输入表的values不够指定的宽度时左边用0填充
mysql> create table db.t2(id int(3),level int(5) zerofill);
Query OK, 0 rows affected (0.43 sec)

2.浮点数
float(n,m) n总宽度,m小数位数
mysql> create table db1.t3( age tinyint unsigned,rust float(5,2),pay float(7,2));
Query OK, 0 rows affected (0.40 sec)

mysql> desc t3;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| age | tinyint(3) unsigned | YES | | NULL | |
| rust | float(5,2) | YES | | NULL | |
| pay | float(7,2) | YES | | NULL | |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into t3 values(21,59.9,18000.23);
Query OK, 1 row affected (0.08 sec)

mysql> select * from t3;
+------+-------+----------+
| age| rust| pay |
+------+-------+----------+
| 21 | 59.90 | 18000.23 |
+------+-------+----------+
1 row in set (0.00 sec)

3.字符类型 1个字节一个字符
定长:char(字符数) 最大长度255字符 ##不够指定字符长度在右边用空格补齐(空间浪费,常用)
变长:varchar(字符数) 最大65532个字符 ##按实际大小分配空间(系统帮忙计算空间,但影响处理速度)
大文本类型:text/blob 字符数大于65535存储时使用
mysql> create table db1.t8(name char(5),age tinyint,rust float(5,2),pay float(7,2));
Query OK, 0 rows affected (0.39 sec)
mysql> desc t8;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name | char(5) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| rust | float(5,2) | YES | | NULL | |
| pay | float(7,2) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> insert into t8 values("tom",21,159.92,18000.23);
Query OK, 1 row affected (0.04 sec)

mysql> select * from t8;
+------+------+--------+----------+
| name | age | rust | pay |
+------+------+--------+----------+
| tom | 21 | 159.92 | 18000.23 |
+------+------+--------+----------+
1 row in set (0.00 sec)

4.日期时间类型
类型 占位(位数) 空间
year YYYY 1字节
date YYYYMMDD 4字节
time HHMMSS 3字节
datetime YYYYMMDDHHMMSS 8字节
timestamp YYYYMMDDHHMMSS 4字节

year() 获取指定时间中的年

date() 获取指定时间中的年月日

month() 获取指定时间中的月

day()  获取指定时间中的日期

time() 获取指定时间中的时间(小时分钟秒)

now()  获取当前时间(年月日小时分钟秒)

mysql> create table t3(name char(8),up_class year,birthdate date,meetting time);
Query OK, 0 rows affected (0.49 sec)

mysql> insert into t3 values("tom",2018,19910709,083000);
Query OK, 1 row affected (0.05 sec)

mysql> select * from t3;
+------+----------+------------+----------+
| name | up_class | birthdate | meetting |
+------+----------+------------+----------+
| tom | 2018 | 1991-07-09 | 08:30:00 |
+------+----------+------------+----------+
1 row in set (0.00 sec)

使用两位数给year字段赋值
01-69 20XX
70-99 19XX
00 0000

使用时间函数获取时间给字段赋值
year() month() date() day() time() ##都需要在括号里填写信息,可以填函数,也可以填数字(自己指定)
now() ##系统当前时间

datetime 与 timestamp 区别?(不给datetime和timestamp赋值时,datetime值为空,timestamp为系统当前时间)
mysql> create table t6(party datetime,meetting timestamp);
Query OK, 0 rows affected (0.27 sec)

mysql> insert into t6 values(now(),now());
Query OK, 1 row affected (0.03 sec)

mysql> insert into t6 values(19910709095928,19910709095928);
Query OK, 1 row affected (0.03 sec)

mysql> insert into t6(party) values(19910709095928);
Query OK, 1 row affected (0.05 sec)

mysql> insert into t6(meetting) values(19910709095928);
Query OK, 1 row affected (0.03 sec)

mysql> select * from t6;
+---------------------+---------------------+
| party | meetting |
+---------------------+---------------------+
| 2018-02-22 20:58:29 | 2018-02-22 20:58:29 |
| 1991-07-09 09:59:28 | 1991-07-09 09:59:28 |
| 1991-07-09 09:59:28 | 2018-02-22 21:01:45 |
| NULL | 1991-07-09 09:59:28 |
+---------------------+---------------------+
4 rows in set (0.00 sec)

5.枚举 (字段值在规定的范围内选择)
单选 enum(值列表)
多选 set(值列表)

mysql> create table t7(name char(10),sex enum("boy","girl","secret"),love set("IT","film","game","music"));
Query OK, 0 rows affected (0.17 sec)

mysql> insert into t7 values("bob","boy","IT,game");
Query OK, 1 row affected (0.02 sec)

ysql> insert into t7 values("lucy","girl","IT,film,music");
Query OK, 1 row affected (0.07 sec)

mysql> select * from t7;
+------+------+---------------+
| name | sex | love |
+------+------+---------------+
| bob | boy | IT,game |
| lucy | girl | IT,film,music |
+------+------+---------------+
2 rows in set (0.00 sec)

6.约束条件
null 允许位空,默认
not null 不允许位空
key 索引类型
default 设置默认值,默认位null
mysql> create table t9(
-> name char(10) not null default "",
-> age tinyint(2) unsigned not null default 19,
-> sex enum("boy","girl","no") not null default "no",
-> likes set("it","film","game","music") not null default "film,game");
Query OK, 0 rows affected (0.15 sec)

mysql> describe t9;
+-------+---------------------------------+------+-----+-----------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------------+------+-----+-----------+-------+
| name | char(10) | NO | | | |
| age | tinyint(2) unsigned | NO | | 19 | |
| sex | enum('boy','girl','no') | NO | | no | |
| likes | set('it','film','game','music') | NO | | film,game | |
+-------+---------------------------------+------+-----+-----------+-------+
4 rows in set (0.01 sec)

7.修改表结构
alter table 库.表 执行动作;
添加新字段add (after,first)
原来样子:
mysql> select * from t7;
+------+------+---------------+
| name | sex | love |
+------+------+---------------+
| bob | boy | IT,game |
| lucy | girl | IT,film,music |
+------+------+---------------+
2 rows in set (0.00 sec)
添加最后:
mysql> alter table t7 add email varchar(30) not null default "stu@tedu.cn";
Query OK, 0 rows affected (0.81 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加到sex列之后:
mysql> alter table t7 add age tinyint(2) unsigned default 21 after sex;
Query OK, 0 rows affected (0.97 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加到首:
mysql> alter table t7 add stu_id char(9) first;
Query OK, 0 rows affected (0.92 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看结构:
mysql> desc t7;
+--------+---------------------------------+------+-----+-------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------------------+------+-----+-------------+-------+
| stu_id | char(9) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| love | set('IT','film','game','music') | YES | | NULL | |
| email | varchar(30) | NO | | stu@tedu.cn | |
+--------+---------------------------------+------+-----+-------------+-------+
6 rows in set (0.00 sec)

mysql> select * from t7;
+--------+------+------+------+---------------+-------------+
| stu_id | name | sex | age | love | email |
+--------+------+------+------+---------------+-------------+
| NULL | bob | boy | 21 | IT,game | stu@tedu.cn |
| NULL | lucy | girl | 21 | IT,film,music | stu@tedu.cn |
+--------+------+------+------+---------------+-------------+
2 rows in set (0.00 sec)

修改字段类型modify
mysql> alter table t7 modify email char(50) not null default "yaya@163.com";
Query OK, 2 rows affected (0.42 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> desc t7;
+--------+---------------------------------+------+-----+--------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------------------+------+-----+--------------+-------+
| stu_id | char(9) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| love | set('IT','film','game','music') | YES | | NULL | |
| email | char(50) | NO | | yaya@163.com | |
+--------+---------------------------------+------+-----+--------------+-------+
修改字段位置modify
mysql> alter table t7 modify age tinyint(2) unsigned default 21 after sex;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc t7;
+--------+---------------------------------+------+-----+--------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------------------+------+-----+--------------+-------+
| stu_id | char(9) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| love | set('IT','film','game','music') | YES | | NULL | |
| email | char(50) | NO | | yaya@163.com | |
+--------+---------------------------------+------+-----+--------------+-------+
6 rows in set (0.00 sec)
删除字段drop
mysql> alter table t7 drop love,drop stu_id;
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from t7;
+------+------+------+-------------+
| name | sex | age | email |
+------+------+------+-------------+
| bob | boy | 21 | stu@tedu.cn |
| lucy | girl | 21 | stu@tedu.cn |
+------+------+------+-------------+
2 rows in set (0.00 sec)

修改字段名change
mysql> alter table t7 change email mail char(30); ##类型也可以改变,也可以改变
Query OK, 2 rows affected (0.53 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from t7;
+------+------+------+-------------+
| name | sex | age | mail |
+------+------+------+-------------+
| bob | boy | 21 | stu@tedu.cn |
| lucy | girl | 21 | stu@tedu.cn |
+------+------+------+-------------+
2 rows in set (0.00 sec)

mysql> desc t7;
+-------+-----------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| mail | char(30) | YES | | NULL | |
+-------+-----------------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
修改表名
mysql> alter table t7 rename studentinfo;
Query OK, 0 rows affected (0.07 sec)

mysql> desc studentinfo;
+-------+-----------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| sex | enum('boy','girl','secret') | YES | | NULL | |
| age | tinyint(2) unsigned | YES | | 21 | |
| mail | char(30) | YES | | NULL | |
+-------+-----------------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> show tables;
+--------------+
| Tables_in_db |
+--------------+
| studentinfo |
| t1 |
| t2 |
| t3 |
| t5 |
| t9 |
+--------------+
6 rows in set (0.00 sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  5646 546