介绍:
数据库是一个长期存储在计算机内的,有组织的,有共享的,统一团里的数据集合,是按数据结构来存储和管理数据的计算机软件系统
存放数据,访问数据,操作数据的存储仓库,可以查询,统计,添加,删除和修改
数据库的特点:
数据共享,较少数据冗余;特定的数据类型;较高的数据独立性;统一的数据控制
数据类型:
数据类型决定数据在计算机中的存储格式,代表不同的信息类型:整数;浮点数;精确小数;二进制;日期时间;字符串;
数据库管理系统:用于管理数据库的软件,授权,提供数据的操作机制,维护数据的安全性,完整性和可靠性
数据库系统的数据冗余小,可以节省数据的存储空间
很容易实现多个数据的共享
对数据库的操作语言叫sql(Structured Query Language),结构化查询语言
sql语言书写规范
数据库系统软不区分大小写,建议大写。但字符串常量区分大小写
可单行或多行。以;结尾
关键词不能跨多行或简写
用空格和缩进来提高语句的可读性
子句通常位于独立行,方便编辑,提高可读性
sql语言包含以下四个部分
数据定义语言(DDL):DROP , CREATE , ALTER等语句
数据操作语言(DML):INSERT(插入),UPDATE(修改),DELETE(删除)等语句
数据查询语言(DQL):SELECT语句
数据控制语言(DCL):GRANT, REVOKE,COMMIT,ROLLBACK等语句
小型关系数据库管理系统,体积小,速度快,总体成本低,开放源代码
特性:
使用C和C++编写,并使用了多种编辑器测试,保证源代码的可移植性
支持多种AIX、BSDi、FreeBSD、HP-UX、Linux、Mac OS、Novell Netware、NetBSD、OpenBSD、OS/2 Wrap、Solaris、SunOS、Windows操作系统
为多种编程语言提供了API。(C、C++、C#、Delphi、Eiffel、Java、Perl、PHP、Python、Ruby和Tcl)
支持多线程,充分利用CPU资源,支持多用户
优化sql算法,有效提高查询速度
可以是单独的一个应用程序,也可以作为一个库嵌入到其他软件中
支持多种语言
提供TCL/IP,ODBC和JDBC等多种数据库连接
提供管理,检查,优化数据库操作的管理工具
版本介绍
Alpha:开发版
Bata:体验版
RC :发布前的小版本(候选版)
GA:稳定版(正式发布版)
包的获取方式:
官网:https://www.mysql.com/
yum包:https://dev.mysql.com/downloads/repo/yum/
rpm包:https://dev.mysql.com/downloads/mysql/
二进制:http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-8.0/
# ls
mysql80-community-release-el7-3.noarch (1).rpm
mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar
# yum install mysql80-community-release-el7-3.noarch (1).rpm
# mkdir mysql
# tar xvf mysql-8.0.20-1.el7.x86_64.rpm-bundle.tar -C mysql
# cd mysql
# yum localinstall mysql-community-client-8.0.20-1.el7.x86_64.rpm mysql-community-embedded-compat-8.0.20-1.el7.x86_64.rpm mysql-community-server-8.0.20-1.el7.x86_64.rpm mysql-community-common-8.0.20-1.el7.x86_64.rpm mysql-community-libs-8.0.20-1.el7.x86_64.rpm mysql-community-devel-8.0.20-1.el7.x86_64.rpm mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm
# 安装完成后
# systemctl start mysqld
# grep password /var/log/mysqld.log
# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: --->上面grep得到的密码
The existing password for the user account root has expired. Please set a new password.
New password: --->设置新的root密码
Re-enter new password: --->重复新的root密码
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration of the component. Using existing password for root.
Estimated strength of the password: 100 #是否重置root密码
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no
... skipping.
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
# 是否移除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : yes
Success.
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
# 是否不允许root远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : yes Success.
By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
# 是否删除test数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : yes
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
# 是否刷新权限
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yes Success.
All done!
# 安装完毕
# 卸载Mariadb
rpm -e mariadb-libs postfix
# 创建用户,设置权限等
# groupadd mysql
# useradd -g mysql mysql
# mkdir mysql
# tar Jxf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar -C mysql
# cd mysql
# yum localinstall mysql-community-client-8.0.16-2.el7.x86_64.rpm mysql-community-server-8.0.16-2.el7.x86_64.rpm mysql-community-libs-8.0.16-2.el7.x86_64.rpm mysql-community-common-8.0.16-2.el7.x86_64.rpm
# yum -y install make gcc-c++
# 关闭selinux
# vim /etc/sysconfig/selinux
SELINUX=disabled
# 修改系统限制参数
# cat >> /etc/security/limits.conf <>/etc/sysctl.conf <<"EOF"
vm.swappiness=0
#增加tcp支持的队列数
net.ipv4.tcp_max_syn_backlog = 65535
#减少断开连接时,资源回收
net.ipv4.tcp_max_tw_buckets = 8000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 10
#改变本地的端口范围
net.ipv4.ip_local_port_range = 1024 65535
#允许更多的连接进入队列
net.ipv4.tcp_max_syn_backlog = 4096
#对于只在本地使用的数据库服务器
net.ipv4.tcp_fin_timeout = 30
#端口监听队列
net.core.somaxconn=65535
#接受数据的速率
net.core.netdev_max_backlog=65535
net.core.wmem_default=87380
net.core.wmem_max=16777216
net.core.rmem_default=87380
net.core.rmem_max=16777216
EOF
# sysctl -p #--->重新加载配置文件
#安装
# tar Jxf /opt/mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz #文件较大,需要时间,可以加v参数看过程
#做软连接
# ln -s /opt/mysql-8.0.18-linux-glibc2.12-x86_64 /usr/local/mysql
# groupadd mysql
# useradd -g mysql mysql -d /home/mysql -s /sbin/nologin
# 修改目录权限
# chown -R mysql:mysql mysql/*
# cd /usr/local/mysql/
# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# 注意此处的 root@localhost: 你的密码 (记住此处密码,后面修改需要用)
# 创建配置文件---> 因为centos7自带mariadb数据库,先备份
# mv /etc/my.cnf /etc/my.cnf.`date "+%Y%m%d%H%M%S"`
# vim /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
server_id=10
port = 3306
user = mysql
character-set-server = utf8
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/mysqld.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
#####====================================[innodb]==============================
innodb_buffer_pool_size = 1024M
innodb_file_per_table = 1
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 2
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 30
innodb_data_file_path=ibdata1:10M:autoextend
#####====================================[log]==============================
log_error = /var/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /var/log/mysql-slow.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
为mysql提供sysv服务脚本
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
启动mysql
# chkconfig --add mysqld --添加为系统服务
# chkconfig mysqld on --开机自启动
# systemctl start mysqld
配置环境变量
# 此时直接mysql是没有这个命令的,需要我们加入全局变量
# 加入全局变量
# cd /etc/profile.d/
# vim mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
# source mysql.sh
修改数据库密码
# 先登录,注意刚才复制的密码
# mysql -uroot -p
# 此处直接粘贴刚才复制的密码,避免出错
# alter user 'root'@'localhost' identified by 'ABC123.com'; #修改密码,登录密码后尽快更改密码,因为初次密码很容易忘记
忘记密码,修改配置文件
# 启动服务
# systemctl stop mysqld
# 忘记密码无法登录时
# vim /etc/my.cnf
[mysqld]
skip-grant-tables #mysqld后面加入此项
# 重启服务
# systemctl start mysqld
# mysql -uroot #此时为免密登录
mysql> flush privileges; #修改密码参数flush privileges
mysql> alter user ‘root’@'localhost’IDENTIFIED BY ‘你的密码’; #记住此处设置的密码
# 退出并停止服务
# systemctl stop mysqld
# 修改配置文件
# 把刚才那个注掉(/etc/my.cnf ---> #skip-grant-tables)
# 重启服务
------------------------------------------------------------------------------------------------------- 返回目录