MySQL介绍及安装

Posted 一本正经的搞事情

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的源

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

    ② 安装yum仓库

[[email protected] ~]# yum localinstall -y mysql57-community-release-el7-7.noarch.rpm

    ③ 安装MySQL5.7

[[email protected] ~]# yum install -y mysql-community-server

    ④ 启动MySQL服务

[[email protected] ~]# systemctl start mysqld.service

    ⑤ 查看随机生成的密码

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

    ⑥ 修改密码

[[email protected] ~]# 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源码包

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

    ② 下载和解压boost库

[[email protected] ~]# wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

[[email protected] ~]# tar xf boost_1_59_0.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/
[[email protected] local]# mv boost_1_59_0 boost

    ③ 创建运行用户

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

    ④ 解压、配置、安装

[[email protected] ~]# tar xf mysql-5.7.23.tar.gz -C /opt/
[[email protected] ~]# cd /opt/mysql-5.7.23/
[[email protected] ~]# 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                                             # 配置
[[email protected] ~]# 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数据库

    ⑤ 修改数据库目录权限

[[email protected] mysql-5.7.23]# chown -R mysql:mysql /usr/local/mysql/

    ⑥ 设置配置文件

mysql 5.7 版本和以前的有所不同,如果配置文件不做修改,则服务启动失败

[[email protected] 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

[[email protected] 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  

    ⑨ 修改密码及远程授权

# 修改密码
[[email protected] mysql]# mysqladmin -u root -p password "abc123"
Enter password: abc123

# 登录
[[email protected] 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:二进制安装的方式(在真正的场景下用的比较多的第二好的方式)

    ① 下载二进制包

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

    ② 卸载系统自带的MariaDB

[[email protected] ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[[email protected] ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
[[email protected] ~]# rpm -qa | grep mariadb
[[email protected] ~]#

    ③ 创建MySQL用户

[[email protected] ~]# useradd -M -s /sbin/nologin mysql

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

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

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

[[email protected] local]# cp mysql/support-files/mysql.server /etc/init.d/mysql
[[email protected] local]# chkconfig --add mysql

    ⑥ 初始化数据库

[[email protected] bin]# cd /usr/local/mysql/
[[email protected] mysql]# mkdir data -p     # 数据存放目录

[[email protected] 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

# 初始化
[[email protected] 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 [email protected]: sQ3LjJkq1Z(z    # 数据库的初始化密码

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

[[email protected] mysql]# systemctl start mysql
[[email protected] 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

[[email protected] 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)

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

[[email protected] 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

 

  

 

以上是关于MySQL介绍及安装的主要内容,如果未能解决你的问题,请参考以下文章

SQL介绍及MySql的安装

二MySQL 介绍及 MySQL 安装与配置

MySQL多实例介绍及安装

MySQL数据库多实例介绍及安装

LAMP架构介绍及mysql安装

mysql介绍及安装