• MySQL (一) 安装、部署等


    2019-04-08  18:57:47

    数据库是存储数据的仓库。

    MySQL 是一个关系型数据库:是建立在关系模型基础上的数据库。

    一、Mysql的安装

      rpm -qa | grep -i mysql 查看是否有安装数据库

      service mysqld stop  停止数据库

      yum erase xxx   卸载数据库

      去官网下载数据库的 rpm 安装包,rz 上传到linux中进行安装,按以下顺序进行安装,几个包有依赖:

      common --> libs --> clients --> server

      rpm -ivh xxxx.rpm 进行安装

      service mysqld start/ restart  启动mysql

      mysql -u root -p 输入密码,进入mysql

        出现问题1:linux RPM格式安装mysql出现 错误:依赖检测失败: mariadb-libs 被 mysql-community-libs-8.0.13-1.el7.x86_64 取代

        解决办法:一个命令:yum remove mysql-libs解决

              清除之前安装过的依赖即可

        出现问题2:error: Failed dependencies:

            libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.19-1.el6.x86_64
            libnuma.so.1(libnuma_1.1)(64bit) is needed by mysql-community-server-5.7.19-1.el6.x86_64
            libnuma.so.1(libnuma_1.2)(64bit) is needed by mysql-community-server-5.7.19-1.el6.x86_64
     
        解决方法:$ yum install -y numactl
     
        出现问题3:service mysqld restart 报错 Redirecting to /bin/systemctl restart mysqld.service
     
        解决办法:使用如下命令操作mysql: 

              systemctl restart mysqld.service 
              systemctl start mysqld.service 
              systemctl stop mysqld.service

              猜测可能是mysql版本问题

        出现问题4: mysql 8.0 以上正确修改 root 用户密码

        解决办法:grep 'temporary password' /var/log/mysqld.log    查看mysql root用户初始密码

               mysql -u root -p   输入初始密码,进入mysql

               alter user'root'@'lcoalhost' IDENTIFIED BY 'Root_1994';    修改root密码

               ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY'Root_1994';    设置ROOT用户的native_password密码

              Flush privileges;  刷新权限,即可远程登录mysql (Navicat, MYSQLyog等)

        出现问题5:mysql 5.7 修改root用户密码           

         解决办法:操作系统为centos7 64

                1、修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1

                  这一行配置让 mysqld 启动时不对密码进行验证

                2、重启 mysqld 服务:systemctl restart mysqld

                3、使用 root 用户登录到 mysql:mysql -u root 

                4、切换到mysql数据库,更新 user 表:

                  update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root';

                  在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string

                5、退出 mysql,编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1 的内容

                6、重启 mysqld 服务,再用新密码登录即可

                另外,MySQL 5.7 在初始安装后(CentOS7 操作系统)会生成随机初始密码,并在 /var/log/mysqld.log 中有记录,可以通过 cat 命令查看,找 password 关键字

                找到密码后,在本机以初始密码登录,并且(也只能)通过 alter user 'root'@'localhost' identified by 'root' 命令,修改 root 用户的密码为 root,然后退出,重新以root用户和刚设置的密码进行登录即可。

        参考链接:https://blog.csdn.net/ypp91zr/article/details/84801204

             https://www.cnblogs.com/frankielf0921/p/7258137.html

             https://blog.csdn.net/qq_36133698/article/details/81194417

             https://blog.csdn.net/yi247630676/article/details/80352655

             https://www.cnblogs.com/FlyingPuPu/p/7783735.html

             https://blog.csdn.net/HaHa_Sir/article/details/80552663

             https://www.cnblogs.com/beyang/p/9979384.html

    二、库的操作(查看、创建、删除数据库)

      mysql语法要求:每句话结尾一定要用英文分号;

              再回车执行该语句。 

      show databases; 查看数据库

      create database host_config;  新建数据库(注意:数据库的命名尽量用下划线)

      drop database host_config;  删除数据库

    三、表的操作(创建表、修改表字段、删除表)

      use argus;  进入argus数据库

      show tables;  查看该库的表

      create table host (id int(100) primary key,ip varchar(30) not null,host_name varchar(30) not null);  创建表

        注意:建表时,varchar一定要指定字符长度,否则会语法错误,建表失败。

        创建表要点:字段名、数据类型、约束

              数据类型:int / bigint    整型   当整数值超过int数据范围时才使用bigint

                   varchar / char  字符型  varchar:可变长度 char:固定长度

                   float       浮点型

                   date       

                   number 

                约束:primary key  主键约束,具有唯一性

                    not null    非空约束

                   default 1   默认值约束

                   foreign key   外键约束 

      desc host;  查看表结构

      alter table host rename host1;  修改表名(rename)

      alter table host1 change id id1 int(105);  修改表字段(change) 注意:一定要加数据类型,不然会修改失败

      alter table host1 add class int(3) not null first;   增加表字段(在第一个字段前 first)

      alter table host1 add course int(3) not null after class;  增加表字段(在一个字段后 after class)

      alter table host1 add (Chinese int(3),English int(3));    增加两个表字段(在最后)

      alter table host1 modify English int(3) first;  修改某字段到第一位;(注意:一定要加上数据类型,不然会报语法错误,修改失败)

      alter table host1 drop class;  删除某字段   

      alter table host1 change id id1 int(100) auto_increment;  修改某字段为自增长 (注意:自增长一定要像这样改才能修改成功,且自增长字段应该和主键字段一致,否则会报语法错误,修改失败)

      alter table host1 drop primary key;  删除主键约束

      drop table host1;  删除表

     

    四、drop ,truncate ,delete的区别(删除数据)

    关键字 用法 区别 释放空间 回滚 应用范围  
    drop drop table host1; 删除表结构(定义)、表数据 释放空间     DLL(data define language)
    truncate truncate host1; 快速删除大量数据,删除速度快 释放空间   table DLL(data define language)
    delete delete from host1; delete from host1 where ...; 一行一行的删除表数据,删除慢 不释放空间 可回滚 table,view DML(data maintain Language)

      

      参考链接:https://www.cnblogs.com/zhizhao/p/7825469.html

    五、增加数据,修改数据,查询数据

     1.增加一条数据

      insert into host (id,ip,name) values (1,'100.65.225.21','master');

     2.增加多条数据

      insert into host (id,ip,name) values (2,'100.65.225.22','slave1'),(3,'100.65.225.24','slave2');

     3.修改一条数据

      update host set name='master1' where id=1;

      修改多条数据

      update host set id=id+1;

     4.查询数据

      select * from host;  查看所有数据

      select ip from host;  查看某一列的数据

      select ip,name from host;  查看某两列数据

      select * from host ORDER BY id desc;  按某列降序排列查看;(降序查看)

      select * from host ORDER BY id asc;  按某列升序排列查看;(升序查看)

      select * from host where id<>2;

      select * from host where id!=2;

      select * from host where id not in(2); 查看id不等于2的数据;

      select distinct (name) from student where score!=101;   查询分数不等于101且名字不重复的结果(distinct去重查询);

      select distinc (name),score from student where score=100 or score=99;  (or查询);

      select distinc (name),score from student where score!=100 and score!=99;  (and查询);

      select * from student2 where class=1 and English=98 and math=77;  显示1班英语成绩为98,数学成绩为77namenumber信息

      select count(name) from student where age between 24 and 31;   (统计...之和,between and 查询)

      select count(name) from student where age >=25 or age <=31;   (统计...之和,如果有重复的名字也不会去重,仍然按照2个来计算)

      select * from host where id limit 0,5;    查看,从第1行开始,共计5行的记录;(limit查询)

      select * from student limit 0,3;    查看,从第1行开始,共计3行的记录;(limit查询)

      select sex as "性别",count(sex) as "各性别总人数" from student group by sex;   (group by分组统计查询,并起别名)

      select * from student where class is null;    查询班级信息为空的数据;(为空查询)

      select * from student where class is not null;   查询班级信息不为空的数据;(非空查询)

      select * from student where name='xiaobai' or 1=1; 安全性测试,sql注入;(若代码未过滤,则会查询出所有数据)

      select * from student2 where class="1603" order by Chinese desc;  查询出1603班成绩并且按语文成绩排序(降序)

      select * from student2 where (class=1603 or class="1602") and Chinese<80 and math<80;

      select * from student2 where class in (1603,1602) and Chinese<80 and math<80;   查询1602班与1603班,语文成绩与数学成绩都小于80namenumber信息

    1.where 不能放在GROUP BY 后面

    where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数比如SUM(),AVG()等,使用where条件显示特定的行。

    2.having 是跟GROUP BY 连在一起用的,放在GROUP BY 后面,此时的作用相当于WHERE

         having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。 

      

      

    3.1如何给普通的数据库用户赋予相应的权限(如增删改查):

    1.使用root账号登录,输入root用户的密码:******

    2.进入mysql数据库:use mysql;

    3.查询mysql数据库服务器已经创建了哪些用户:select host,user from user;

    3.创建用户但是未授权(方法一)

    insert into user (Host,User,Password)values('localhost','chen',password('123456'));创建用户

    创建用户后需要刷新下:flush privileges

    4.创建用户后进行授权(方法二)

    grant select,update,delete on *.* to ‘chen’@'localhost' identified by '123123' with grant option;

    4.创建用户、同时授权(方法二)(%表示只可以远程登录,localhost只能本机登陆)

    grant all privileges on *.* to ‘root’@'%' identified by '123123';

    flush privileges

    show grants for 'chen'@'%'; //查看数据库的指定授权用户的权限

    revoke all on *.* from sss@localhost ;//取消所有权限

    delete from user where user='zhongguo'and host='localhost';删除用户

    update user set password=password("test") where user='root';

      

     

  • 相关阅读:
    SpringMVC中请求路径参数使用正则表达式
    SpringBoot单元测试示例2
    数据结构与算法之——八大排序算法
    linux学习之centos(二):虚拟网络三种连接方式和SecureCRT的使用
    linux学习之centos(一):在VMware虚拟机中安装centos6.5
    网易云课堂学习之VS相关
    emplace_back减少内存拷贝和移动
    Lepus经历收获杂谈(一)——confirm features的小工具
    MDM平台学习笔记
    四大开源协议:BSD、Apache、GPL、LGPL
  • 原文地址:https://www.cnblogs.com/Agnes1994/p/10674475.html
Copyright © 2020-2023  润新知