zoukankan      html  css  js  c++  java
  • MySQL介绍及安装(一)

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

      1.1:关系型数据库

        关系型数据库是把复杂的数据结构归结为简单的二元关系(即二维表格的形式),在关系型数据库中,对数据的操作几乎全部建立在一个或多个关系表格上的,通过对这些关联的表格分类、合并、连接或者选取等运算来实现数据的管理。我们常用的MySQL和oracle、MariaDB、SQL server都是关系型数据库。

      1.2:非关系型数据库

        非关系型数据库被称为"NoSQL",NoSQL的产生并不是要彻底的否定关系型数据库,而是作为传统关系型数据库的一个有效的补充,NoSQL数据库在特定的场景下可以发挥很难想象的高效率和高性能,NoSQL典型的产品memcached(纯内存)、redis(持久化缓存)、mongodb、Hbase等等。

      1.3:非关系型数据库的种类

        ① 键值对(key-value)存储数据库(memcached、redis)

        ② 列存储(Column-oriented)数据库(Cassandra、HBase)

        ③ 面向文档(Document-Oriented)数据库(mongodb)

        ④ 图形(Graph)数据库(我也不知道)

    二、MySQL介绍

      MySQL是一种关系型数据库管理系统,关系型数据库的特点是将数据保存在不同的表中,在将这些表放入不同的数据库中,而不是将所有的数据统一放在一个大的仓库,这样的设计增加了MySQL的读取速度,灵活性可管理性也得到了大的提高,访问MySQL数据库最常用的标准语言是SQL结构化查询语。

      选择MySQL数据库的几个原因

      ① MySQL性能卓越,服务稳定,很少宕机

      ② MySQL开发源代码无版权约束,自主性及成本低

      ③ 社区用户活跃

      ④ MySQL体积小,安装使用简单,利于维护

      ⑤ MySQL口碑好,流行企业架构LAMP LNMP

      ⑥ MySQL支持多操作系统,提供多种API,支持多语言开发

    三、如何选择MySQL数据库版本

       ① 稳定版:选择开源社区办的稳定版GA版本

      ② 产品线:可以选择5.5或者5.7 互联网现在主流5.5 其次是5.1和5.6或者5.7 最好不要选择5.5之前的,因为这是个大坑

      ③ 选择MySQL数据库GA版本发布6月以后的

      ④ 要考虑开发人员开发的程序是否兼容

      ⑤ 还有就是根据实际情况而定,活灵活现

    四、多种安装方式及什么时候使用什么方式

      PS:所有的方式我们用的MySQL的版本是MySQL5.7的版本

    4.1:yum安装的方式(适合对数据库要求不太高的场合,例如并发不大、公司内部、zabbix、自己的博客)

        ① 下载MySQL5.7的源

    [root@rsync131 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

        ② 安装yum仓库

    [root@rsync131 ~]# yum localinstall -y mysql57-community-release-el7-7.noarch.rpm

        ③ 安装MySQL5.7

    [root@rsync131 ~]# yum install -y mysql-community-server

        ④ 启动MySQL服务

    [root@rsync131 ~]# systemctl start mysqld.service

        ⑤ 查看随机生成的密码

    [root@rsync131 ~]# grep 'temporary password' /var/log/mysqld.log
    2018-09-08T10:59:31.829660Z 1 [Note] A temporary password is generated for root@localhost: dKG,g/3l7*uI    # 这个就是随机的密码

        ⑥ 修改密码

    [root@rsync131 ~]# mysql -uroot -pdKG,g/3l7*uI
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.23
    
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
    Query OK, 0 rows affected (0.00 sec)                # 第一种方式
    
    mysql> set password for 'root'@'localhost'=password('MyNewPass4!');
    Query OK, 0 rows affected, 1 warning (0.00 sec)     # 第二种方式
    
    mysql>flush privileges;

        ⑦ 修改密码策略

        使用初始随机密码登录后MySQL会强制要求修改密码,否则无法正常使用,(密码必须包含小写、大写字母及特殊字符),这个策略是可以修改的

    # 查看策略相关信息
    mysql> show variable'%password%'
        -> ;
    +---------------------------------------+--------+
    | Variable_name                         | Value  |
    +---------------------------------------+--------+
    | default_password_lifetime             | 0      |
    | disconnect_on_expired_password        | ON     |
    | log_builtin_as_identified_by_password | OFF    |
    | mysql_native_password_proxy_users     | OFF    |
    | old_passwords                         | 0      |
    | report_password                       |        |
    | sha256_password_proxy_users           | OFF    |
    | validate_password_check_user_name     | OFF    |
    | validate_password_dictionary_file     |        |
    | validate_password_length              | 8      |
    | validate_password_mixed_case_count    | 1      |
    | validate_password_number_count        | 1      |
    | validate_password_policy              | MEDIUM |
    | validate_password_special_char_count  | 1      |
    +---------------------------------------+--------+
    14 rows in set (0.00 sec)
    
    # 修改方法
    在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略
    # 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件
    validate_password_policy=0
    如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:
    validate_password = off
    
    重新启动mysql服务使配置生效:
    systemctl restart mysqld

        ⑧ 远程连接授权

    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;
    Query OK, 0 rows affected, 1 warning (0.00 sec)

        ⑨ 设置防火墙

    开通端口(默认3306):
    
    firewall-cmd --add-port=3306/tcp
    
    /sbin/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

        ⑩ 文件默认路径

    默认配置文件路径:
    
    配置文件:/etc/my.cnf 
    日志文件:/var/log//var/log/mysqld.log 
    服务启动脚本:/usr/lib/systemd/system/mysqld.service 
    socket文件:/var/run/mysqld/mysqld.pid

    4.2:编译安装的方式(在真正的场景下用的比较多的最好的方式)

        ① 安装所需要的包及下载MySQL源码包

    [root@rsync131 ~]# yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
    [root@rsync131 ~]# wget http://www.mysql.com/Downloads/MySQL-5.7/mysql-5.7.23.tar.gz

        ② 下载和解压boost库

    [root@rsync131 ~]# wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
    
    [root@rsync131 ~]# tar xf boost_1_59_0.tar.gz -C /usr/local/
    [root@rsync131 ~]# cd /usr/local/
    [root@rsync131 local]# mv boost_1_59_0 boost

        ③ 创建运行用户

    useradd -M -s /sbin/nologin mysql  # 创建用户mysql,不创建家目录,不允许登陆系统

        ④ 解压、配置、安装

    [root@rsync131 ~]# tar xf mysql-5.7.23.tar.gz -C /opt/
    [root@rsync131 ~]# cd /opt/mysql-5.7.23/
    [root@rsync131 ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=/usr/local/boost -DWITH_SYSTEMD=1                                             # 配置
    [root@rsync131 ~]# make && make install            # 安装
    
    
    
    # ==================================cmake解释=====================================
    
    cmake \
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \                         	# 指定mysql数据库安装目录
    -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \               		# 连接文件位置
    -DSYSCONFDIR=/etc \                                                 # 指定配置文件目录
    -DSYSTEMD_PID_DIR=/usr/local/mysql \                                # 进程文件目录
    -DDEFAULT_CHARSET=utf8  \                                           # 指定默认使用的字符集编码
    -DDEFAULT_COLLATION=utf8_general_ci \                               # 指定默认使用的字符集校对规则
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \                         			# 存储引擎
    -DWITH_ARCHIVE_STORAGE_ENGINE=1 \                            		# 存储引擎
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \                     			# 存储引擎
    -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \                 				# 存储引擎
    -DMYSQL_DATADIR=/usr/local/mysql/data \                             # 数据库文件
    -DWITH_BOOST=/usr/local/boost \                                     # 指定Boost库的位置,mysql5.7必须添加该参数
    -DWITH_SYSTEMD=1                                                    # 使系统支持MySQL数据库

        ⑤ 修改数据库目录权限

    [root@rsync131 mysql-5.7.23]# chown -R mysql:mysql /usr/local/mysql/

        ⑥ 设置配置文件

    mysql 5.7 版本和以前的有所不同,如果配置文件不做修改,则服务启动失败
    
    [root@rsync131 mysql-5.7.23]# vim /etc/my.cnf
    [client]
    port = 3306
    default-character-set=utf8
    socket = /usr/local/mysql/mysql.sock
    [mysql]
    port = 3306
    default-character-set=utf8
    socket = /usr/local/mysql/mysql.sock
    [mysqld]
    user = mysql
    basedir = /usr/local/mysql
    datadir = /usr/local/mysql/data
    port = 3306
    character_set_server=utf8
    pid-file = /usr/local/mysql/mysqld.pid
    socket = /usr/local/mysql/mysql.sock
    server-id = 1
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
    
    [root@rsync131 mysql-5.7.23]# chown mysql:mysql /etc/my.cnf   //修改配置文件的权限

        ⑦ 设置环境变量及初始化数据库

    echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
    echo 'export PATH' >> /etc/profile
    source /etc/profile   //使写入生效
    
    
    # 初始化数据库
    cd /usr/local/mysql/
    bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data    
    
    
    
    ===================解释================
    cd /usr/local/mysql/
    bin/mysqld \
    --initialize-insecure \         # 生成初始化密码(5.7版本才有),实际会生成空密码
    --user=mysql \                  # 指定管理用户
    --basedir=/usr/local/mysql \    # 指定工作目录
    --datadir=/usr/local/mysql/data # 指定数据文件目录

        ⑧ 添加系统服务、开机自启

    cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
    systemctl daemon-reload    //刷新识别mysqld.service服务
    systemctl enable mysqld    //加入系统自启动
    systemctl start mysqld     //启动服务
    netstat -anpt | grep 3306  

        ⑨ 修改密码及远程授权

    # 修改密码
    [root@rsync131 mysql]# mysqladmin -u root -p password "abc123"
    Enter password: abc123
    
    # 登录
    [root@rsync131 mysql]# mysql -u root -pabc23
    
    
    # 设置远程授权
    grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
    //第一个“*”代表所有数据库,第二“*”代表所有表,赋予root权限 “%”代表所有服务器终端,可设为IP地址 密码为“abc123”

        ⑩ 设置防火墙

    开通端口(默认3306):
    
    firewall-cmd --add-port=3306/tcp
    
    /sbin/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
    

    4.3:二进制安装的方式(在真正的场景下用的比较多的第二好的方式)

        ① 下载二进制包

    [root@rsync131 ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz

        ② 卸载系统自带的MariaDB

    [root@rsync131 ~]# rpm -qa | grep mariadb
    mariadb-libs-5.5.56-2.el7.x86_64
    [root@rsync131 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
    [root@rsync131 ~]# rpm -qa | grep mariadb
    [root@rsync131 ~]#

        ③ 创建MySQL用户

    [root@rsync131 ~]# useradd -M -s /sbin/nologin mysql

        ④ 解压安装包,并且重命名

    [root@rsync131 ~]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
    [root@rsync131 ~]# cd /usr/local/
    [root@rsync131 local]# mv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql/

        ⑤ 将mysql添加为系统服务及设置开机自启

    [root@rsync131 local]# cp mysql/support-files/mysql.server /etc/init.d/mysql
    [root@rsync131 local]# chkconfig --add mysql

        ⑥ 初始化数据库

    [root@rsync131 bin]# cd /usr/local/mysql/
    [root@rsync131 mysql]# mkdir data -p     # 数据存放目录
    
    [root@rsync131 mmysql]# yum install -y libaio   # 必须安装不然会报下面的错误
    错误信息
    ./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
    
    # 初始化
    [root@rsync131 mmysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
    2018-09-08T14:15:21.330311Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2018-09-08T14:15:21.488188Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2018-09-08T14:15:21.511408Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2018-09-08T14:15:21.565037Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9f0919fe-b371-11e8-92a6-000c292579e0.
    2018-09-08T14:15:21.565691Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2018-09-08T14:15:21.566064Z 1 [Note] A temporary password is generated for root@localhost: sQ3LjJkq1Z(z    # 数据库的初始化密码

        ⑦ 启动mysql服务,使用临时密码登录mysql并修改登录密码

    [root@rsync131 mysql]# systemctl start mysql
    [root@rsync131 mysql]# ps -ef | grep mysql
    root       1686      1  0 22:16 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/rsync131.pid
    mysql      1771   1686  0 22:16 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=rsync131.err --pid-file=/usr/local/mysql/data/rsync131.pid
    root       1823   1475  0 22:20 pts/0    00:00:00 grep --color=auto mysql
    
    [root@rsync131 mysql]# ./bin/mysql -u root -p
    Enter password: sQ3LjJkq1Z(z   # 初始化密码
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.23
    
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');   # 修改密码
    Query OK, 0 rows affected, 1 warning (0.00 sec)

        ⑧ 设置任何远程主机都可以访问数据库

    [root@rsync131 mysql]# ./bin/mysql -u root -p123456   # 修改后的密码
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.23 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> grant all privileges on *.* to 'root' @'%' identified by '123456';  # 设置权限
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql>  flush privileges;     # 刷新权限
    Query OK, 0 rows affected (0.00 sec)
    
    mysql>

        ⑨ 设置防火墙

    开通端口(默认3306):
    
    firewall-cmd --add-port=3306/tcp
    
    /sbin/iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
    

      

    作者:朱敬志

    -------------------------------------------

    个性签名:在逆境中要看到生活的美,在希望中别忘记不断奋斗

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

    万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!

    支付宝 微信

    也可以关注我的微信公众号,不定时更新技术文章(kubernetes,Devops,Python)等

    微信公众号
  • 相关阅读:
    使用python-gitlab包在gitlab上自动创建分组
    navicat连接远程mysql失败
    Python shell介绍
    Python任务流taskflow
    Python导包问题
    demo27-数组去重
    demo26-通过循环输出数据到页面显示
    demo25-for循环
    Visual Studio 2005 快捷键汇总
    三维立体图片效果
  • 原文地址:https://www.cnblogs.com/zhujingzhi/p/9609861.html
Copyright © 2011-2022 走看看