数据库(Database)是按照数据结构来组织、存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代之后,数据管理再也不仅仅是存储和管理数据,而转变成用户所须要的各类数据管理的方式。数据库有不少种类型,从最简单的存储有各类数据的表格到可以进行海量数据存储的大型数据库系统都在各个方面获得了普遍的应用。
主流的数据库有:sqlserver,mysql,Oracle、SQLite、Access、MS SQL Server等。mysql
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品。MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件。linux
MySQL主流分支有Oracle官方版本的==MySQL、Percona Server、MariaDB==。sql
官网地址:https://www.mysql.com/
分为如下版本数据库
MySQL Community Server 是开源免费的,这也是咱们一般用的MySQL的版本。
主要为社区版(Community Servert)和商业版(ENterprise Edition),这两个版本又分为4个版本发布,分别是Alpha(开发公司内部测试,稳定性差)、Beta(BUG较少,提供用户测试)、RC(正式版发布以前的版本)和GA(正式版,生产环境基本用这个版本)。
商业版比社区版更稳定,且不遵照GPL协议,能够享受7*24小时技术支持和打补丁等服务。缓存
产品线 | 说明 |
---|---|
5.0.xx到5.1.xx | 早期产品,只修复漏洞,不增长新功能。5.1及之前默认引擎是MyISAM。 |
5.4.xx到5.7.xx | 主流版本,经常使用5.5和5.6。5.5.5及之后默认引擎是Innodb。 |
6.0.xx到7.1.xx | 集群版本。 |
8.0.xx | 新发布版本。 |
MySQL命名规则,例如5.5.40:安全
官网地址:https://www.percona.com/software/mysql-database/percona-server服务器
Percona Server是MySQL重要的分支之一,它基于InnoDB存储引擎的基础上,提高了性能和易管理性。数据结构
官网地址:https://mariadb.org/
MariaDB是由MySQL创始人Monty建立的,是一款高度兼容的MySQL产品,主要由开源社区维护,采用GPL受权许可。oracle
# 系统为CentOS7.6版本。 [root@test ~]#cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
关闭SElinux和防火墙socket
systemctl stop firewalld systemctl disable firewalld sed -i 's#^SELINUX=.*#SELINUX=disabled#g' /etc/sysconfig/selinux setenforce 0
修改最大打开进程数和文件句柄数
# 查看当前服务器最大打开进程数和文件句柄数 [root@test ~]#ulimit -a|egrep 'open files|max user' open files (-n) 1024 max user processes (-u) 7191
若是open files设置不合理,而当前服务器的链接过多或者表过多时,就有可能会出现打不开表或者访问不了表的现象,默认状况下,最大句柄数为1024个,表示单进程最多能够访问1024个文件句柄。若是超过默认值,则会出现文件句柄超限的错误“too many open files”。
max user processes参数的用途是,有时候咱们可能会跑多实例,可是发现建立不了新的链接,报出“resource temporarily unavailable”的错误,表示没有足够的资源。
# 修改/etc/systemd/system.conf文件中的相关参数,与CentOS6修改的方式略有不一样 sed -i '/^#DefaultLimitNOFILE=/aDefaultLimitNOFILE=65535' /etc/systemd/system.conf sed -i '/^#DefaultLimitNPROC=/aDefaultLimitNPROC=65535' /etc/systemd/system.conf sed -i 's#nproc 4096#nproc 65535#' /etc/security/limits.d/20-nproc.conf # 须要重启才能生效 reboot
本次5.6版本使用二进制方式安装,固然也能够yum安装。
# 建立用户 useradd -s /sbin/nologin -M mysql # basedir放在/usr/local目录下 cd /usr/local # 解压二进制包 tar zxvf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz # 作个软连接,方便往后升级 ln -s mysql-5.6.40-linux-glibc2.12-x86_64 mysql chown -R mysql.mysql mysql # 建立数据目录 mkdir -p /data/mysql chown mysql.mysql -R /data/mysql
在启动MySQL实例的过程当中,会按照/etc/my.cnf-->/etc/mysql/my.cnf-->/usr/local/mysql/my.cnf-->~/.my.cnf这样的一个优先级别的顺序去读取参数文件。若是想指定默认的参数文件,须要配合--defaults-file参数。
通用配置参考:
[client] port = 3306 socket = /tmp/mysql.sock default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] character-set-server=utf8 port = 3306 socket = /tmp/mysql.sock basedir = /usr/local/mysql/ datadir = /data/mysql/ default-storage-engine=INNODB max_connections=200
cd /usr/local/mysql/scripts/ # 开始初始化,若出现两个OK,则说明初始化成功 ./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --defaults-file=/etc/my.cnf --user=mysql # 若出现如下错误: FATAL ERROR: please install the following Perl modules before executing ./mysql_install_db: Data::Dumper # 解决方法 yum -y install autoconf
# 复制启动脚本到/etc/rc.d/init.d目录下 cp ../support-files/mysql.server /etc/rc.d/init.d/mysqld chmod +x /etc/rc.d/init.d/mysqld # 加入系统服务 chkconfig --add mysqld # 将mysql相关命令连接到/bin目录下 ln -s /usr/local/mysql/bin/* /bin/ # 启动mysql service mysqld start
# 删除test库和无用帐号 mysql -uroot -e"delete from mysql.user where host='::1';" mysql -uroot -e"delete from mysql.user where user='';" mysql -uroot -e"delete from mysql.user where host='';" mysql -uroot -e"drop database test;"
mysqladmin -uroot password
5.7版本使用yum安装。
建立文件/etc/yum.repos.d/mysql57.repo:
# Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
更新缓存:
yum makecache fast
yum -y install mysql mysql-server
systemctl start mysqld
注意:MySQL5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,而且长度不能少于8位。不然会提示ERROR 1819
[root@test ~]# grep "temporary" /var/log/mysqld.log 2018-10-23T02:19:47.328365Z 1 [Note] A temporary password is generated for root@localhost: jr8phgkQT1-4 2018-10-23T02:19:50.816992Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables [root@test ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 324 Server version: 5.7.24 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 global validate_password_policy=0; Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_length=6; Query OK, 0 rows affected (0.00 sec) mysql> set password for 'root'@'localhost'=password('DtDream01'); Query OK, 0 rows affected, 1 warning (0.00 sec)
修改/etc/my.cnf:
[client] port = 3306 socket = /tmp/mysql.sock default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] #skip-grant-tables character-set-server=utf8 port = 3306 socket = /tmp/mysql.sock datadir = /data/mysql/ default-storage-engine=INNODB max_connections=200 # 关闭密码过时功能 default_password_lifetime=0 # 关闭密码检测 validate_password=off # 不区分大小写 lower_case_table_names=1