1. 简介

1.数据库的好处:
1.1 可以持久化数据到本地;
1.2 结构化查询

2.常见概念
行业趋势:https://db-engines.com/en/ranking
DB:数据库,存储数据的容器;
DBMS:数据库管理系统,又称为数据库软件或数据库产品,用于创建或管理DB;
SQL:结构化查询语言,用于和数据库通信的语言,不是某个数据库软件特有的,而是几乎所有主流数据库软件通用的语言;
RDBMS (关系型数据库管理系统) : Oracle ,MySQL ,PG,MSSQL
NoSQL (not only sql):
键值对: Redis ,memcached
文档: MongoDB
NewSQL: TiDB,Spanner ,AliSQL(RDS+DRDS) ,OB ,PolarDB

3.数据库存储数据的特点
3.1.数据存放到表中,然后表再放到库中;
3.2.一个库中可以有多张表,每张表具有唯一的表名来标识自己;
3.3.表中有一个或多个列,列又称为“字段”,相当于Java中的属性
3.4.表中的每一行数据相当于Java中的对象

2.Mysql5.7二进制部署

1)下载
二进制包下载地址:https://downloads.mysql.com/archives/community/

2)把安装包放到服务器上

  1. [root@server_10.1.1.11 ~]#mkdir -p /application
  2. [root@server_10.1.1.11 ~]#cd /application
  3. [root@server_10.1.1.11 /application]#ls
  4. mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
  5. [root@server_10.1.1.11 /application]#tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C ./
  6. [root@server_10.1.1.11 /application]#ls
  7. mysql-5.7.33-linux-glibc2.12-x86_64
  8. [root@server_10.1.1.11 /application]#mv mysql-5.7.33-linux-glibc2.12-x86_64 mysql
  9. [root@server_10.1.1.11 /application]#ls
  10. mysql
  11. [root@server_10.1.1.11 /application]#rpm -qa | grep mariadb
  12. mariadb-libs-5.5.60-1.el7_5.x86_64
  13. [root@server_10.1.1.11 /application]#rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

3)创建MySQL用户和组

  1. [root@server_10.1.1.11 /application]#groupadd mysql
  2. [root@server_10.1.1.11 /application]#useradd mysql -M -g mysql -s /sbin/nologin

4)添加环境变量

  1. [root@server_10.1.1.11 /application]#tail -1 /etc/profile
  2. export PATH=/application/mysql/bin:$PATH

5) 创建数据、日志、pid目录,改变用户属主和属组

  1. [root@server_10.1.1.11 /application]#mkdir -p /data/mysql/data
  2. [root@server_10.1.1.11 /application]#chown -R mysql.mysql /application/mysql
  3. [root@server_10.1.1.11 /application]#chown -R mysql.mysql /data/mysql/data
  4. [root@server_10.1.1.11 /data/mysql/data]#mkdir -p /var/log/mysql
  5. [root@server_10.1.1.11 /data/mysql/data]#chown -R mysql.mysql /var/log/mysql

6)安装依赖包

  1. [root@server_10.1.1.11 /application]#yum install libaio-devel -y

7)初始化数据库(无初始密码)

  1. [root@server_10.1.1.11 /application]#mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data

8)准备基础配置文件

  1. cat >/etc/my.cnf <<EOF
  2. [mysql]
  3. socket = /tmp/mysql.sock
  4. [mysqld]
  5. user = mysql
  6. port = 3306
  7. datadir = /data/mysql/data
  8. basedir = /application/mysql
  9. socket = /tmp/mysql.sock
  10. bind-address = 0.0.0.0
  11. character-set-server = utf8mb4
  12. collation-server = utf8mb4_general_ci
  13. log-error = /var/log/mysqld.log
  14. max_connections = 10240
  15. open_files_limit = 65535
  16. innodb_buffer_pool_size = 3G
  17. innodb_flush_log_at_trx_commit = 2
  18. innodb_log_file_size = 256M
  19. EOF

9)配置启动服务

  1. cat >/etc/systemd/system/mysqld.service <<EOF
  2. [Unit]
  3. Description=MySQL Server
  4. Documentation=man:mysqld(8)
  5. Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
  6. After=network.target
  7. After=syslog.target
  8. [Install]
  9. WantedBy=multi-user.target
  10. [Service]
  11. User=mysql
  12. Group=mysql
  13. ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
  14. LimitNOFILE = 5000
  15. EOF

10)启动,验证服务

  1. [root@server_10.1.1.11 /application]#systemctl start mysqld
  2. [root@server_10.1.1.11 /application]#systemctl status mysqld

3.Mysql5.7初始化密码

1)关闭数据库

  1. [root@db01 ~]# systemctl stop mysqld
  2. Shutting down MySQL.. SUCCESS!

2) 启动数据库到维护模式

  1. --skip-grant-tables #跳过授权表
  2. --skip-networking #跳过远程登录
  3. [root@db01 ~]# mysqld_safe --skip-grant-tables --skip-networking &

3) 登录并修改密码

  1. mysql> alter user root@'localhost' identified by '1';
  2. ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
  3. mysql> flush privileges;
  4. mysql> alter user root@'localhost' identified by '1';
  5. Query OK, 0 rows affected (0.01 sec)

4) 关闭数据库,正常启动验证