linux 运维 mysql数据库 架构

架构设计

5周的时间

两周是数据库   增删改查(更详细)  mariadb(yum)   mysql (源码) 线上5.5  5.6

5.7    redis    mongodb     mha

一周    nginx    httpd  网页(web)

一周    tomcat  app(web)

一周    消息队列  kafka    rabbimq

        存储    iscsi    fastdfs

        日志分析   elk

        堡垒机    jumpserver   

        Mycat    数据库的读写分离

关系型数据库和非关系型数据库

关系型    mysql   mariadb

Mysql

关系型数据库   存储数据是以表结构进行存储的

非关系型数据库    存储数据是以键值对进行存储

Mysql5.7

安装两种方式

  1. rpm安装

Common-libs-client--devel-server

[root@localhost ~]# yum -y remove mariadb-libs

 6  rpm -ivh mysql-community-common-5.7.33-1.el7.x86_64.rpm

    7  rpm -ivh mysql-community-libs-5.7.33-1.el7.x86_64.rpm

    8  rpm -ivh mysql-community-libs-compat-5.7.33-1.el7.x86_64.rpm

9  rpm -ivh mysql-community-client-5.7.33-1.el7.x86_64.rpm

rpm -ivh mysql-community-devel-5.7.33-1.el7.x86_64.rpm

   10  rpm -ivh mysql-community-server-5.7.33-1.el7.x86_64.rpm

源码安装  tar.gz

Mysql的特点

  1. 关系型数据库   通过sql语句进行管理   
  2. 开源的   能够跨平台   windows   linux
  3. 支持主从集群的部署   主主   主从
  4. 支持多种连接方式    php   python   java
  5. 支持数据备份   索引   事务
  6. 支持多种数据类型     int   char

Mysql.com   小海豚

启动数据库

[root@localhost ~]# systemctl start mysqld

[root@localhost ~]# netstat -anput | grep mysqld

tcp6       0      0 :::3306                 :::*                    LISTEN      3692/mysqld         

[root@localhost ~]# systemctl stop mysqld   #停止

[root@localhost ~]# systemctl restart mysqld   #重启

[root@localhost ~]# systemctl enable mysqld   #加入开机自启

Mariadb    5.5

Mysql   5.7     没有密码是无法登录数据库的

查看数据库的默认密码

[root@localhost ~]# cat /var/log/mysqld.log | grep password

2021-09-06T01:13:24.180410Z 1 [Note] A temporary password is generated for root@localhost: eqOshb/hq8Tz

[root@localhost ~]# mysql -uroot -p'eqOshb/hq8Tz'

#因为这个密码是默认密码  是临时的  所以需要更改密码  密码必须更改   如果不更改操作不能实现

mysql> set password=password("1234.Com");  #密码 字符数字字母(大小写)8位

Query OK, 0 rows affected, 1 warning (0.09 sec)   #密码的地方不能上键调取命令

[root@localhost ~]# mysql -uroot -p'1234.Com'

#系统中密码的策略

数据库当中又很多的变量   通过变量可以查看到相关的配置

mysql> show variables like '%password%';   #查看和密码相关的变量

| validate_password_length               | 8               |#密码长度

| validate_password_mixed_case_count     | 1               |#至少含有一个大写字母

| validate_password_special_char_count   | 1               |  #特殊字符至少一个

mysql> set global validate_password_length=4;

mysql> set global validate_password_policy=0;

mysql> set global validate_password_special_char_count=0;

mysql> set global validate_password_mixed_case_count=0;

mysql> set password=password("12345");

Query OK, 0 rows affected, 1 warning (0.01 sec)

[root@localhost ~]# mysql -uroot -p'12345'   #使用新密码测试登录

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |   #存放这用户信息   密码  授权   

| performance_schema |

| sys                |

+--------------------+

4 rows in set (0.00 sec)

mysql> create database test;

Query OK, 1 row affected (0.00 sec)

mysql> use test;

Database changed

#创建表

create table 表名 (

字段一   数据类型  [完整约束条件],

字段二   数据类型  [完整约束条件],

字段三   数据类型  [完整约束条件],

数据类型:指的是数据的种类  例如  数字  小数  浮点数   字符串   时间

整数类型:INT,(11)    BIT,  BOOL,    TINY,  BIG  INT,   SMALL INT,   MEDIUM INT

浮点数:   FLOAT,     DOUBLE,   DECIMAL

字符串:  CHAR   VARCHAR,   TEXT,    LONG TEXT

日期:  Date    Time    Year

完整约束条件

就是对于字段的限制  要求用户对该属性符合特定的要求,如果不能满足完整约束条件,数据库系统将不会执行用户的操作,目的就是为了保证数据库中数据的完整性

  1. 主键   要求当前这个字段非空且唯一

一个表当中只有一个主键

create   table   表名(

字段一  数据类型   primary  key,

字段二   数据类型

mysql> create table test_pri (

    -> id int primary key,

    -> name char(10)

    -> );

mysql> desc test_pri;  #查看表结构

2 rows in set (0.00 sec)

mysql> insert into test_pri values(1,"lala");

验证非空且唯一

mysql> insert into test_pri values(1,"hehe");

mysql> insert into test_pri(name) values("hehe");

删除主键

Alter    table   表名   drop   主键

mysql> alter table test_pri drop primary key;

mysql> desc test_pri;

添加主键

Alter    table   表名   add    [constraint  约束名]  增加的类型  字段名

mysql> alter table test_pri add constraint ppp primary key (id);

mysql> desc test_pri;

mysql> alter table test_pri add  primary key (id);

mysql> desc test_pri;

多字段联合主键

主键一个表中只能又一个主键   但是可以针对多个字段

mysql> create table test_pri1 (

    -> id int,

    -> name char(10),

    -> primary key(id,name)

    -> );

Query OK, 0 rows affected (0.01 sec)

mysql> desc test_pri1;

唯一约束   unique  要求字段中的数据唯一

非空       not   null   要求非空约束的字段不能有空值

注意:当一个表当中没有主键,如果这个时候,赋予这个字段以非空且唯一的特性,那么这个字段就会变成主键。

Create    table   表名  (

字段   数据类型    unique,

字段    数据类型    not   null

);

mysql> create  table test_un (

    -> id int unique,

    -> name char(10) not null,

    -> phone_num int unique not null

    -> );

Query OK, 0 rows affected (0.01 sec)

mysql> desc test_un;

删除非空

Alter   table  表名   modify   字段  null;

mysql> alter table test_un modify name char(10) null;

mysql> desc test_un;

删除唯一特性

Alter   table  表名  drop  index   字段名;

mysql> alter table test_un drop index id;

mysql> desc test_un;

添加非空和唯一

Alter   table  表名  modify   字段   not null unique;

mysql> alter table test_un modify name char(10) not null unique;

默认值约束    default    当前的字段当中没有插入对应的值,就会使用默认的值

Create  table   表名 (

字段   数据类型   default   “值”

);

mysql> create table test_def (

    -> id int default "10",

    -> name char(10)

    -> );

mysql> insert into test_def(name) values("haha");   #只插入name这个字段的数据

mysql> select * from test_def;

+------+------+

| id   | name |

+------+------+

|   10 | haha |

+------+------+

删除默认值

Alter   table    表名  alter column  字段   drop default;

mysql> alter  table test_def alter column id drop default;

mysql> alter table test_def modify id int default NULL;

mysql> desc test_def;

添加或更改默认值

Alter   table   表名  modify 字段名  default  “值”  

mysql> alter table test_def modify id int default '5';

mysql> desc test_def;

mysql> alter table test_def modify id int default '10';

外键: foreign  key   实现多张表直接数据的完整性和一致性

设定外键连接表的字段  必须是该表的主键

用法

create   table   表1 (

字段1   数据类型   primary key

);

create   table  表2  (

字段2   数据类型

constraint    外键名   foreign  key  (字段2) references   表1 (字段1)

);

mysql> create table class (

    -> id int primary key,

    -> class_name char(10),

    -> t_name char(10)

    -> );

mysql> create table student (

    -> id int,

    -> name char(10),

    -> c_id int,

    -> constraint c_s foreign key(c_id) references class(id)

    -> );

Query OK, 0 rows affected (0.03 sec)

验证

mysql> insert into class values (1,"yiban","zhangsan");

mysql> insert into student values (1,"one",1);

mysql> insert into student values (2,"two",2);

主键中有的数据   外键可以插入   主键中没有的数据   外键不能插入

删除外键

Alter   table   表名  drop   foreign  key 外键名

mysql> alter table student drop foreign key c_s;

添加外键

Alter   table   表名   add  constraint  外键名  key  (外键字段)  references   外表名

mysql> alter table student add constraint c_s foreign key(c_id) references class(id);

自增约束   auto_increment   自增字段会按照顺序写入数值

Create   table  表名(

字段名   数据类型   auto_increment

)auto_increment=1;

mysql> create table test_auto (

    -> id int primary key auto_increment,

    -> name char(10)

    -> )auto_increment=1;

Query OK, 0 rows affected (0.02 sec)

mysql> desc test_auto;

mysql> insert into test_auto(name) values("one");

mysql> insert into test_auto(name) values("two");

mysql> select * from test_auto;

删除自增

Alter  table  表名   modify   column   字段名  first;

mysql> alter table test_auto modify column id int first;

mysql> desc test_auto;

添加自增

Alter  table  表名   modify   column   字段名  auto_increment;

mysql> alter table test_auto modify column id int auto_increment;

主键    非空   唯一    外键   自增

        

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据库从删库到跑路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值