Linux mysql 架构 服务

mysql     关系型数据库
mariadb     yum安装
mysql =  mariadb  一个软件     操作方式   
源码/rpm       yum
数据库
关系型数据库    存储方式是以表结构的形式存储的
非关系型数据库     存储是以键值对的形式存储
mysql5.7    
安装:
两种安装方式
1.rpm安装 
rpm
rpm  安装   顺序
common--libs--client--devel--sever
[root@localhost ~]# yum -y remove mariadb-libs
15  rpm -ivh mysql-community-common-5.7.33-1.el7.x86_64.rpm 
   16  rpm -ivh mysql-community-libs-5.7.33-1.el7.x86_64.rpm 
   17  rpm -ivh mysql-community-libs-compat-5.7.33-1.el7.x86_64.rpm 
   18  rpm -ivh mysql-community-client-5.7.33-1.el7.x86_64.rpm 
   19  rpm -ivh mysql-community-devel-5.7.33-1.el7.x86_64.rpm 
   20  rpm -ivh mysql-community-server-5.7.33-1.el7.x86_64.rpm 
mysql 的特点:
1.关系型数据库    sql语句进行管理
2.开源的     跨平台     linux   window
3.支持主从集群部署
4.支持多种连接方式    lamp   php   java  python
5.支持备份   索引    事务
6.支持多种数据类型    int   char
mysql.com    小海豚
#启动服务
[root@localhost ~]# systemctl start mysqld   #开启
[root@localhost ~]# systemctl stop mysqld   #关闭
[root@localhost ~]# systemctl enable mysqld   #加入开机自启
[root@localhost ~]# systemctl disable mysqld   #移除开机自启
#5.7mysql如果没有密码是没有办法登录     mysql有默认密码    如果命令行中出现密码  不可以使用上键
[root@localhost ~]# cat /var/log/mysqld.log | grep password
2021-11-01T05:55:11.226445Z 1 [Note] A temporary password is generated for root@localhost: b%KCKgEwV6nE
[root@localhost ~]# mysql -uroot -p'b%KCKgEwV6nE'
mysql> set password=password('1234.Com');  #更改密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
[root@localhost ~]# mysql -uroot -p'1234.Com'
查看数据库中的变量
查看密码策略的变量
mysql> show variables like "%password%";
| validate_password_length               | 8        
| validate_password_number_count         | 1     
| validate_password_mixed_case_count     | 1  
| validate_password_policy               | MEDIUM      #严格模式
| validate_password_special_char_count   | 1      
mysql> set global validate_password_length=4;  #密码长度
Query OK, 0 rows affected (0.02 sec)
mysql> set global validate_password_policy=0;   密码策略为宽容模式
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_mixed_case_count=0;    #取消大小混合
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_special_char_count=0;   #特殊字符0个
mysql> set password=password("12345");
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> exit
Bye
[root@localhost ~]# mysql -uroot -p12345
库  --  表
mysql   user   存放用户信息   密码  授权    
mysql> show databases;  #查看库
mysql> use mysql   #进入mysql库
mysql> show tables;   #查看表
mysql> create database test;   #创建库
mysql> drop database test;    #删除库
创建一个abc库   使用abc这个库
mysql> create database abc;
mysql> use abc;
创建表
create   table  表明  (
字段一    数据类型   [完整约束条件],
字段二    数据类型   [完整约束条件],
字段三   数据类型   [完整约束条件],

数据类型:  指的是数据的种类    例如   数据   小数   浮点数    字符串   时间等
整数的数据类型   INT,   BIT,   BOOL ,  TINY   INT,  SAMLL INT,   BIG INT
浮点数:  float  bouble
字符串:char   varchar    text   
日期:  date   datetime    year    
https://www.cnblogs.com/-xlp/p/8617760.html  有关数据类型的文档
字段---表头
数据类型----表头下的数据是什么类型   数字  字符
什么叫做完整约束条件
就是对于字端的限制,要求用户对该数据符合特点的要求  如果不能满足完整性约束条件数据库系统将不会执行用户写入的操作   目的为了保证数据的完整性
1.  主键     要求主键的字段非空且唯一
一个当中只能有一个主键
create  table   表名
字段   数据类型   primary  key
);
mysql> create table test_pri (
    -> id int,
    -> name char(10) primary key
    -> );
mysql> desc test_pri;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| name  | char(10) | NO   | PRI | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.03 sec)
表当中插入数据
insert  into   表名   values(数据);
验证唯一性
mysql> insert into test_pri values (1,"lala");
mysql> insert into test_pri values (2,"lala");
ERROR 1062 (23000): Duplicate entry 'lala' for key 'PRIMARY'
不能为空
mysql> insert into test_pri values (2,NULL);
ERROR 1048 (23000): Column 'name' cannot be null
删除主键
alter   table   表名   add/drop   主键
mysql> alter  table test_pri drop primary key;
mysql> desc test_pri;
添加主键
mysql> alter table test_pri add primary key(name);
mysql> desc test_pri;

多字段联合主键
一个表当中只能有一个主键
create  table  表名 (
   字段,
   primary  key(字段1,字段2)

mysql> create table test_pri1 (
    -> id int,
    -> name char(10),
    -> primary key(id,name)
    -> );
Query OK, 0 rows affected (0.00 sec)
mysql> desc test_pri1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | NO   | PRI | NULL    |       |
| name  | char(10) | NO   | PRI | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

唯一约束    unique   要求为止约束的字段数据唯一
非空约束    not  null  要求费控约束的字段不能有空值
在表当中如果没有主键    对于一个字段设置了非空且唯一的特性,那么这个字段会被作为主键
create   table   表名 (
字段   数据类型    unique,
字段   数据类型     not  null,
字段  数据类型    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 phone_num int null;
mysql> desc test_un;
删除唯一
alter   table   表名   drop  index   字段名;
mysql> alter table test_un drop index phone_num;
mysql> desc test_un;
添加非空唯一
alter   table  表名  modify   字段   not  null   unique;
mysql> alter table test_un modify phone_num int not null unique;

默认值约束   default    当字段中没有数据写入  则使用默认设置的值
create  table  表名 (
字段  数据类型   default  "值";

mysql> create table test_def (
    -> id int default "10",
    -> name char(10)
    -> );
Query OK, 0 rows affected (0.00 sec)
mysql> desc test_def;
mysql> insert into test_def(name) values("lala");
mysql> select * from test_def;
+------+------+
| id   | name |
+------+------+
|   10 | lala |
删除默认值
alter   table   表名  alter   column  字段名   drop  default;
  mysql> alter table test_def alter column id drop default;
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 '6';

外键
实现多张表之间数据的完整性和一致性
设定外键链接表的字段   必须是主键
用法 
create  table   表一 (
字段1  数据类型    primary  key
);
create  table  表二  (
字段1  数据类型
constraint  外键名  foreign  key  (字段名) references  表一  (字段名)

mysql> create table class (
    -> id int primary key,
    -> class_name char(10),
    -> t_name char(10)
    -> );
Query OK, 0 rows affected (0.00 sec)
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.00 sec)
mysql> insert into class values (1,"yiban","zhangsan");
Query OK, 1 row affected (0.01 sec)
mysql> insert into student values (1,"aa",1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values (1,"bb",2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`abc`.`student`, CONSTRAINT `c_s` FOREIGN KEY (`c_id`) REFERENCES `class` (`id`))
删除外键
alter  table  表名  drop  foreign  key  外键名;
mysql> alter table student drop foreign key c_s;
添加外键
alter  table  表名   add   constraint   外键名  key  (外键字段)  refenrences  外表名  主键字段
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.00 sec)
mysql> desc test_auto;
mysql> insert into test_auto(name) values('aa');
mysql> insert into test_auto(name) values('bb');
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;
mysql> desc test_auto;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
LINUX重启MYSQL的命令 . 分类: Linux 2010-06-25 10:21 6367人阅读 评论(0) 收藏 举报 如何启动/停止/重启MySQL 一、启动方式 1、使用 service 启动:service mysqld start 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start 3、使用 safe_mysqld 启动:safe_mysqld& 二、停止 1、使用 service 启动:service mysqld stop 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop 3、 mysqladmin shutdown 三、重启 1、使用 service 启动:service mysqld restart 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart 提问 编辑摘要 如何启动/停止/重启MySQL 一、启动方式 1、使用 service 启动:service mysqld start 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start 3、使用 safe_mysqld 启动:safe_mysqld& 二、停止 1、使用 service 启动:service mysqld stop 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop 3、 mysqladmin shutdown 三、重启 1、使用 service 启动:service mysqld restart 2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart 刚开始学 mysql时都是用redhat自带的。启动是什么 /rc.d/init.d/ start 这很简单,但是后来越学越多,系统自带的 mysql,有的是版本太低,有的是与 自己想要装的web服务需要的低版本的mysql 后来自己学着以tar的方式安装 mysql,我的mysql装在/usr/local/mysql目录下启 动碰到过很多问题。最常见的是: ERROR 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111) 解决办法: [root@test mysql]# /usr/local/mysql/bin/mysqladmin -u root / > -S /var/lib/mysql/mysql.sock password 'your.passwd' 或者做个连接 ln -s /var/lib/mysql/mysql.sock /tmp 其实提示找不到 /tmp/mysql.sock有时也并不是/tmp目录下没这个文件,是启动 命令不对,我碰到过 常见的几种启动方式,自己也不是记得很清楚,如果你确定tmp下有mysql.sock这 个文件不妨试试另外的几个命令 /usr/local/mysql/bin/mysql -u root -p /usr/local/mysql/bin/mysqld --user=mysql& /usr/local/mysql/bin/mysqld --user=root& /usr/local/mysql/bin/mysqld_safe --user=root& /usr/local/mysql/bin/mysqld_safe --user=mysql& /usr/local/mysql/bin/safe_mysqld--uer=root&(注意 safe_mysqld与mysqld_safe是不同的,&表示mysql在后台运行)我的就会报错了 STOPPING server from pid file /usr/local/mysql/data/localhost.localdomain.pid 060304 11:46:21 mysqld ended 这是权限问题,我的mysql目录属于root用户,也属于root群组,改用 mysqld_safe启动就没问题了, 大家只要注意这几个 mysql,safe_mysqld,mysqld_safe,mysqld,mysqladmin.多试 几次 其实有时mysql已经正常启动了,查看mysql是否启动命令 ps -aux | grep mysqld 会看到如下类似内容 mysql 6394 0.0 1.5 10528 992 pts/3 S 16:16 0:00 /usr/local/mysql/ mysql 6395 0.0 1.5 10528 992 pts/3 S 16:16 0:00 /usr/local/mysql/ mysql 6396 0.0 1.5 10528 992 pts/3 S 16:16 0:00 /usr/local/mysql/ root 6422 0.0 1.1 2408 732 pts/3 S 16:20 0:00 grep mysql 查看mysql是否在监听端口命令 netstat -tl | grep mysql 会看到如下类似内容 tcp 0 0 *:mysql *:* LISTEN

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

数据库从删库到跑路

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

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

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

打赏作者

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

抵扣说明:

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

余额充值