• Mysql编译安装和yum安装


    一、msql数据库介绍

    1、什么是sql

    sql代表结构化查询语言,sql是用于访问数据库的标椎化语言

    sql包含三个部分

     DDL数据定义语言包含定义数据库及其对象的语言,例如表,视图,触发器,存储过程等

    DML数据操作语言包含允许数据更新和查询数据的语句(这里的查询语言可以分为 DQL)

    DCL 数据控制语言允许授予用户权限访问数据库中的特定数据;

    2.mysql是什么?

    mysql是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于Oracle旗下,mysql是最流行的关系型的数据库之一,在web应用方面,MySQL是最好的RDBMS应用软件之一,关系数据库将数据保存在不同的表中,而不是将数据放在一个大仓库,这样就增加了速度并提高数据的灵活性。

    MySQL的官方网址: MySQL ,MySQL的社区版本下载地址为: MySQL :: Download MySQL Community Server

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

    关系型数据库

    基本概念:关系型数据库依据关系模型来创建数据库。

       所谓关系模型就是一对一,一对多,多对多的关系模型,关系模型就是指二维数据表格模     型,因而一个关系型数据库就是由二维表及其之间的联系组成的一个数据组织。

    优点:

    1.易于维护:使用的都是表结构,格式一致

    2.使用方便:sql语言通用,可用于复杂查询

    3.复杂操作:支持sql,可用于一个表以及多个表之间非常复杂的查询

    4.学习成本低。

    缺点:

    1.读写性能差,尤其是海量数据高效率读写

    2.固定的表结构,灵活度欠缺

    3.高并发读写需求,传统关系型数据库来说,硬盘I/O是一个很大的瓶颈

    关系型数据库:Oralce Mysql DB2 PostgreSQL SqlServer

    非关系型数据库

    基本概念:非关系型数据库主要是基于“非关系模型”的数据库(由于关系型太大,所以一般用“非关系型”来表示其他类型的数据库)

    优点:

    1.格式灵活,存储数据的格式可以是key,value形式,文档形式,图片形式等等

    2.速度快:可以使用硬盘或者内存作为载体,而关系型数据只能使用硬盘

    3.成本低:数据库部署简单,基本都是开源软件

    缺点

    1.不提供sql支持,学习和使用成本高;

    2.无事务处理

     非关系型数据库:Redis MongDB Memcache

    二、yum部署安装MySQL

    1.关闭防火墙和selinux

    [root@localhost ~]#systemctl stop firewalld && setenforce 0

    2.找yum源的rpm安装包

    复制链接

    3.下载yum源的rpm安装包

    [root@localhost ~]# yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-10.noarch.rpm

    4.把安装5.7的源打开,关闭安装8.0的源

    方法一:(永久关闭)

    1. [root@localhost ~]# vim /etc/yum.repos.d/mysql-community.repo #打开配置文件
    2. # Enable to use MySQL 5.7
    3. [mysql57-community]
    4. name=MySQL 5.7 Community Server
    5. baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
    6. enabled=1 #将0改为1,代表打开这个源
    7. gpgcheck=1
    8. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
    9. file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    10. [mysql80-community]
    11. name=MySQL 8.0 Community Server
    12. baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
    13. enabled=0 #将1改为0,代表关闭这个源
    14. gpgcheck=1
    15. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
    16. file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

    方法二:(永久关闭)

    1. [root@localhost ~]# yum -y install yum-utils
    2. [root@localhost ~]# yum repolist |grep mysql
    3. mysql-connectors-community/x86_64 MySQL Connectors Community 227
    4. mysql-tools-community/x86_64 MySQL Tools Community 100
    5. mysql80-community/x86_64 MySQL 8.0 Community Server 426
    6. [root@localhost ~]# yum-config-manager --disable mysql80-community
    7. [root@localhost ~]# yum-config-manager --enable mysql57-community

    方法三:(临时关闭)

    [root@localhost ~]#yum install -y mysql-community-server --enablerepo mysql57-community --disablerepo mysql80-community

    5.安装mysql必要安装包

    1. [root@localhost ~]# yum -y install mysql-community-server
    2. [root@localhost ~]# yum -y groupinstall "Development Tools"

    6.启动mysql

    [root@localhost ~]# systemctl start  mysqld

    8.从日志中过滤初始密码

    1. [root@localhost ~]# grep "password" /var/log/mysqld.log
    2. 2023-09-25T13:06:14.510313Z 1 [Note] A temporary password is generated for root@localhost: Qo9b-0AXzdIt

    9.登录数据库

    1. [root@localhost ~]# mysql -p"Qo9b-0AXzdIt" #用日志中的密码登录
    2. mysql: [Warning] Using a password on the command line interface can be insecure.
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 2
    5. Server version: 5.7.43
    6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    11. mysql>

    10.更改mysql密码

     (1)命令行修改密码

    1. [root@localhost ~]# mysqladmin -p"Qo9b-0AXzdIt" password "123" #-p与旧密码之间没有空格,新密码与password之间有空格
    2. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
    3. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
    4. mysqladmin: unable to change password; error: 'Your password does not satisfy the current policy requirements' #密码强度不够
    5. [root@localhost ~]# vim /etc/my.cnf #修改配置文件
    6. validate-password=OFF #关闭mysql密码强度策略,生产环境切勿尝试,首次启动不可关闭
    7. [root@localhost ~]# systemctl restart mysqld #重启mysql
    8. [root@localhost ~]# mysqladmin -p"Qo9b-0AXzdIt" password "123" #再次修改密码
    9. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
    10. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

    (2)mysql里面修改密码

    1. [root@localhost ~]# mysql -p123
    2. mysql: [Warning] Using a password on the command line interface can be insecure.
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 4
    5. Server version: 5.7.43 MySQL Community Server (GPL)
    6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    11. mysql> alter user 'root'@'localhost' identified by "123";

     扩展:

    MySQL忘记密码后登录并重新设置密码_火腿炒馕的博客-CSDN博客

    三、源码编译安装mysql

    1.安装mysql的依赖包

    1. [root@mysql_source ~]# yum -y groupinstall "Development Tools"
    2. [root@mysql_source ~]# yum -y install ncurses ncurses-devel openssl-devel bison libgcrypt gcc gcc-c++ make cmake

    2.下载mysql的压缩包

     [root@mysql_source ~]wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.24.tar.gz

    3.在系统中添加运行mysqld的用户,自定义的数据库目录及其他目录

    1. [root@mysql_source ~]# groupadd mysql
    2. [root@mysql_source ~]# useradd -M -g mysql -s /sbin/nologin mysql
    3. [root@mysql_source ~]# mkdir -p /usr/local/{data,mysql,log,tmp}
    4. [root@mysql_source ~]# chown -R mysql:mysql /usr/local/{data,mysql,log,tmp}

    4.解压并自动化构建系统文件

    1. [root@mysql_source ~]# tar xf mysql-boost-5.7.24.tar.gz
    2. [root@mysql_source ~]# cd mysql-5.7.24
    3. [root@mysql_source mysql-5.7.24]# cmake . \
    4. -DWITH_BOOST=boost/boost_1_59_0/ \
    5. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    6. -DSYSCONFDIR=/etc \
    7. -DMYSQL_DATADIR=/usr/local/data \
    8. -DINSTALL_MANDIR=/usr/share/man \
    9. -DMYSQL_TCP_PORT=3306 \
    10. -DMYSQL_UNIX_ADDR=/usr/local/tmp/mysql.sock \
    11. -DDEFAULT_CHARSET=utf8 \
    12. -DEXTRA_CHARSETS=all \
    13. -DDEFAULT_COLLATION=utf8_general_ci \
    14. -DWITH_SSL=system \
    15. -DWITH_EMBEDDED_SERVER=1 \
    16. -DENABLED_LOCAL_INFILE=1 \
    17. -DWITH_INNOBASE_STORAGE_ENGINE=1

    参数解释:

    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \   安装目录
    -DSYSCONFDIR=/etc \   配置文件存放 (默认可以不安装配置文件)
    -DMYSQL_DATADIR=/usr/local/mysql/data \   数据目录   错误日志文件也会在这个目录
    -DINSTALL_MANDIR=/usr/share/man \     帮助文档 
    -DMYSQL_TCP_PORT=3306 \     默认端口
    -DMYSQL_UNIX_ADDR=/usr/local/tmp/mysql.sock \  sock文件位置,用来做网络通信的,客户端连接服务器的时候用
    -DDEFAULT_CHARSET=utf8 \    默认字符集。字符集的支持,可以调
    -DEXTRA_CHARSETS=all \   扩展的字符集支持所有的
    -DDEFAULT_COLLATION=utf8_general_ci \  支持的
    -DWITH_READLINE=1 \    上下翻历史命令
    -DWITH_SSL=system \    使用私钥和证书登陆(公钥)  可以加密。 适用与长连接。坏处:速度慢
    -DWITH_EMBEDDED_SERVER=1 \   嵌入式数据库
    -DENABLED_LOCAL_INFILE=1 \    从本地倒入数据,不是备份和恢复。
    -DWITH_INNOBASE_STORAGE_ENGINE=1  默认的存储引擎,支持外键

    5.编译安装

    1. [root@mysql_source mysql-5.7.24]# make -j 3 #-j 指定cpu的个数,lscpu查看CPU个数
    2. [root@mysql_source mysql-5.7.24]# echo $? #测试上一步是否正确
    3. 0
    4. [root@mysql_source mysql-5.7.24]# make install #安装

    6.初始化MySQL

        (1)提升mysql命令为系统级别命令

    1. [root@mysql_source ~]# echo 'export PATH=$PATH:/usr/local/mysql/bin'>>/etc/profile
    2. [root@mysql_source ~]# source /etc/profile #重新加载系统环境变量文件/etc/profile,以便使修改后的环境变量生效

          (2)拷贝默认文件至/etc/my.cnf中

    1. [root@mysql_source mysql]# chown -R mysql.mysql /usr/local/mysql #修改属主和属组
    2. [root@mysql_source ~]# cd /usr/local/mysql/mysql-test/include
    3. [root@mysql_source include]# cp /etc/{my.cnf,my.cnf.bak} #拷贝文件
    4. [root@mysql_source include]# vim /etc/my.cnf #修改配置文件
    5. [mysqld]
    6. basedir = /usr/local/mysql #安装目录
    7. datadir = /usr/local/data #数据存放目录
    8. tmpdir = /usr/local/tmp #/tmp缓存目录
    9. socket = /usr/local/tmp/mysql.sock #指定socket文件的位置
    10. pid_file = /usr/local/tmp/mysqld.pid #指定pid文件的位置
    11. log_error = /usr/local/log/mysql_error.log #错误日志的位置
    12. slow_query_log_file = /usr/local/log/slow_warn.log #慢日志查询
    13. server_id = 1 #server-id=??
    14. user = mysql #指定用户
    15. port = 3306 #指定端口
    16. bind-address = 0.0.0.0 #监听地址(允许所以ip访问)
    17. character-set-server = utf8 #字符集
    18. default_storage_engine = InnoDB #引擎

       (3)执行数据库服务初始化操作

    1. [root@mysql_source ~]# cd /usr/local/mysql
    2. [root@mysql-server mysql]# mysqld --defaults-file=/etc/my.cnf --initialize --user='mysql'
         (4)启动mysqld服务
    1. [root@mysql_source mysql]# mysqld_safe --defaults-file=/etc/my.cnf & #启动进程并把它挂在后台运行
    2. [1] 25705
    3. 2019-8-18T09:19:35.334751Z mysqld_safe Logging to '/usr/local/log/mysql_error.log'.
    4. 2019-8-18T09:19:35.379829Z mysqld_safe Starting mysqld daemon with databases from /usr/local/data

       (5)配置mysqld的服务管理工具(便与启动)

    1. [root@mysql_source mysql]# cd /usr/local/mysql/support-files
    2. [root@mysql_source support-files]# cp mysql.server /etc/init.d/mysqld #拷贝启动脚本到/etc/init.d/目录下,并改名mysqld
    3. [root@mysql_source support-files]# systemctl daemon-reload #重新加载系统服务
    4. [root@mysql_source support-files]# pkill mysqld #关闭之前启动的mysql
    5. [root@mysql_source support-files]# systemctl start mysqld #用systemctl的方式启动mysqld
    6. [root@mysql_source support-files]# systemctl enable mysqld #设置开机自启动

    7.登录mysql

    1. [root@mysql_source mysql]# grep "password" /usr/local/log/mysql_error.log #过滤数据库的初始密码
    2. 2019-8-18T09:18:34.214401Z 1 [Note] A temporary password is generated for root@localhost: ejhszb2:m3wJ
    3. [root@localhost ~]#mysql -p"ejhszb2:m3wJ" #登录mysql
    4. mysql: [Warning] Using a password on the command line interface can be insecure.
    5. Welcome to the MySQL monitor. Commands end with ; or \g.
    6. Your MySQL connection id is 2
    7. Server version: 5.7.24 Source distribution
    8. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    9. Oracle is a registered trademark of Oracle Corporation and/or its
    10. affiliates. Other names may be trademarks of their respective
    11. owners.
    12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    13. mysql> alter user 'root'@'localhost' identified by "123"; #更改密码,方法一
    14. Query OK, 0 rows affected (0.00 sec)
    15. 更改密码 方法二:
    16. [root@mysql_source ~]# mysqladmin -p'old_passwd' password "new_passwd"

  • 相关阅读:
    贪心算法实现背包问题(背包可拆分)
    《复盘:对过去的事情做思维演练》读书笔记
    java中怎么获取字符的ASCII码
    网络安全(黑客)自学笔记
    Python 爬虫—scrapy
    计算机毕设题目设计与实现(论文+源码)_kaic
    总结一下前后端分离业务流程
    国内外传输大文件有哪些好用又便宜的文件传输工具?
    嵌入式分享合集27
    【斯坦福大学公开课CS224W——图机器学习】一、图机器学习中的传统方法(1)
  • 原文地址:https://blog.csdn.net/2301_78315274/article/details/133279195