您的位置:首页 > 数据库

数据库了解

2019-07-23 16:10 50 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_44717011/article/details/96994774

一、数据库
是用户存放数据、访问数据、操作数据的存储仓库,用户的各种数据被有组织的存放在数据库中。可以随时被有权限的用户查询、统计、添加、删除、和修改。
是长期存储在计算机内的,有组织、可共享的数据集合。
数据库系统是由普通的文件系统发展而来的。数据库系统具有较高的数据独立性,即不依赖于特定的数据库应用程序;数据库系统的数据冗余小,可以节省数据的存储空间;另外数据库系统还很容易实现多个用户的数据共享。

数据库管理系统:
数据库系统成熟的标志就是数据库管理系统的出现。
数据库管理系统(DataBase Managerment System,简称DBMS)是管理数据库的一个软件,它充当所有数据的知识库,并对它的存储、安全、一致性、并发操作、恢复和访问负责。是对数据库的一种完整和统一的管理和控制机制。
数据库管理系统不仅让我们能够实现对数据的快速检索和维护,还为数据的安全性、完整性、并发控制和数据恢复提供了保证。数据库管理系统的核心是一个用来存储大量数据的数据库。
DBMS是所有数据的知识库,并对数据的存储、安全、一致性、并发操作、恢复和访问负责。
DBMS有一个数据字典(有时被称为系统表),用于贮存它拥有的每个事物的相关信息,例如名
字、结构、位置和类型,这种关于数据的数据也被称为元数据(metadata)。
二、mysql
2.1、特性:
2.1.1 MySQL是一个小型关系数据库管理系统,目前MySQL被广泛地应用在Internet上的中小型网站中。由于体积小、速度快、总体拥有成本低,尤其是开放源代码这一特点,许多中小型网站。为了降低网站总体拥有成本而选择了MySQL作为网站数据库。
2.1.2 使用C和C++编写,并使用了多种编译器进行测试,保证源代码的可移植性。
2.1.3 支持AIX、BSDi、FreeBSD、HP-UX、Linux、Mac OS、Novell Netware、NetBSD、OpenBSD、OS/2 Wrap、Solaris、SunOS、Windows等多种操作系统。
2.1.4 为多种编程语言提供了API。这些编程语言包括C、C++、C#、Delphi、Eiffel、Java、Perl、PHP、Python、Ruby和Tcl等。
2.1.5 支持多线程,充分利用CPU资源,支持多用户。
2.1.6 优化的SQL查询算法,有效地提高查询速度。
2.1.7 既能够作为一个单独的应用程序应用在客户端服务器网络环境中,也能够作为一个库而嵌入到其他的软件中。
2.1.8 提供多语言支持,常见的编码如中文的GB 2312、BIG5,日文的Shift_JIS等都可以用作数据表名和数据列名。
2.1.9 提供TCP/IP、ODBC和JDBC等多种数据库连接途径。
2.1.10 提供用于管理、检查、优化数据库操作的管理工具(mysql,mysqldump,mysqladmin)。
2.1.11 可以处理拥有上千万条记录的大型数据库。
2.2、版本介绍:
Alpha版:开发版,公司内部使用
Beta版:完成开发后,用户体验版
RC版:生产环境发布之前的一个小版本或称候选版
GA版:正式发布版本
2.3、mysql优点:
MySQL性能卓越,服务稳定,很少出现异常宕机;
MySQL开放源代码且无版权制约,自主性及使用成本低;
MySQL历史悠久,社区及用户非常活跃,遇到问题,可以寻求帮助;
MySQL软件体积小,安装使用简单,并且易于维护,安装维护成本低;
MySQL品牌口碑效应,使得企业无需考虑就直接用之,LAMP流程结构;
MySQL支持多种操作系统,提供多种API接口,支持多种开放语言,特别对流行的PHP语言有很好的支持;
2.4、mysql获取:
http://www.mysql.com/从官方网站下载安装包从官方网站下载源代码包
从官方网站下载二进制包
从发行版本光盘中获取安装包
三、mysql安装部署
3.1 安装包获取:
https://dev.mysql.com/downloads/mysql/

3.2 rpm包安装:
3.2.1 卸载Mariadb

rpm -e mariadb-libs postfix

3.2.2 安装

[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -g mysql mysql
[root@localhost ~]# mkdir mysql
[root@localhost ~]# tar xf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar -C mysql
[root@localhost ~]# cd mysql
[root@localhost ~]# 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

3.2.3 启动

[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:

[root@localhost ~]# grep "temporary password" /var/log/mysqld.log
[root@localhost ~]# grep "temporary password" /var/log/mysqld.log
2019-07-20T06:51:49.543657Z 5 [Note] [MY-010454] [Server] A 	temporary password is generated for root@localhost: UMbbis?rO3e8
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.Your 	MySQL connection id is 8
Server version: 8.0.16
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. 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'ABC123.com'; #大写,小写,数
字,特殊符号
Query OK, 0 rows affected (0.01 sec)

3.3 通用二进制安装:
3.3.1 下载glibc版本的Mysql
http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-8.0/
3.3.2 安装系统依赖包

[root@localhost ~]# yum -y install make gcc-c++ cmake bison-devel ncurses-devel readline-devel libaio-devel perl libaio wget lrzsz vim libnuma* bzip2 xz

3.3.3 关闭selinux

[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@localhost ~]# setenforce 0

3.3.4 修改系统限制参数

[root@localhost ~]# cat >> /etc/security/limits.conf <<EOF
#
###custom
#
* soft nofile 20480
* hard nofile 65535
* soft nproc 20480
* hard nproc 65535
EOF

3.3.5 修改内核参数

cat >>/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

3.3.6 mysql的安装配置
解压安装包

[root@localhost ~]# tar xJf mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz -C /opt/

做软连接到/usr/bin

[root@localhost opt]# cd /usr/local/
[root@localhost local]# ln -s /opt/mysql-8.0.15-linux-glibc2.12-x86_64 mysql

创建用户

[root@localhost local]# groupadd mysql
[root@localhost local]# useradd -g mysql mysql -d /home/mysql -s /sbin/nologin

修改目录权限

[root@localhost local]# chown -R mysql.mysql mysql/*

初始化数据库

[root@localhost mysql]# bin/mysqld --initialize --user=mysql -- basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2019-07-19T05:34:49.071877Z 0 [System] [MY-013169] [Server] /opt/mysql-8.0.15- linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.15) initializing of server in progress as process 17125
2019-07-19T05:34:51.884095Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 7OFD=f#e?f8P
2019-07-19T05:34:54.559988Z 0 [System] [MY-013170] [Server] /opt/mysql-8.0.15- linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.15) initializing of server has completed

创建配置文件,配置文件若存在,需要先注释。

if [ -f /etc/my.cnf ]; then;mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%m`.bak;fi

修改配置文件

[root@localhost profile.d]# 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 = innodblog_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服务脚本

# cp support-files/mysql.server /etc/rc.d/init.d/mysqld # chmod +x /etc/rc.d/init.d/mysqld

启动mysql

chkconfig --add mysqld --添加为系统服务
# chkconfig mysqld on --开机自启动
# systemctl start mysqld

将mysql的bin目录加入PATH环境变量,编辑/etc/profile文件 使用mysql客户端命令

[root@stutest mysql]# mysql
-bash: mysql: command not found

由于PATH环境变量中没有添加,则进行添加:

cd /etc/profile.d/
vim mysql.sh

添加如下内容

export PATH=$PATH:/usr/local/mysql/bin
source mysql.sh

3.3.7 数据库登录并修改密码

[root@localhost profile.d]# mysql -uroot -p
alter user 'root'@'localhost' identified by 'ABC123.com';

四、数据库操作基础
4.1 初识SQL语言:
SQL(Structured Query Language 即结构化查询语言)
在关系型数据库上执行数据操作、数据检索以及数据维护的标准语言。
使用SQL语句,程序员和数据库管理员可以完成如下的任务
改变数据库的结构
更改系统的安全设置
增加用户对数据库或表的许可权限
在数据库中检索需要的信息
对数据库的信息进行更新
SQL语言主要用于存取数据、查询数据、更新数据和管理关系数据库系统,SQL语言由IBM开发。SQL
语言分为3种类型:
DDL语句 数据库定义语言: 数据库、表、视图、索引、存储过程,例如CREATE DROP ALTERDML语句 数据库操纵语言: 插入数据INSERT、删除数据DELETE、更新数据UPDATE、查询数据
SELECT
DCL语句 数据库控制语言: 例如控制用户的访问权限GRANT、REVOKE
SQL语句的书写规范
在数据库系统中,SQL语句不区分大小写(建议用大写) 。但字符串常量区分大小写。
SQL语句可单行或多行书写,以“;”结尾。
关键词不能跨多行或简写。
用空格和缩进来提高语句的可读性。
子句通常位于独立行,便于编辑,提高可读性。
SELECT * FROM tb_table
注释:
SQL标准:/**/。
多行注释 “–”
单行注释MySQL注释:“#”
4.2 系统数据库:
information_schema: 虚拟库,主要存储了系统中的一些数据库对象的信息,例如用户表信息、列信息、权限信息、字符信息等
performance_schema: 主要存储数据库服务器的性能参数
mysql: 授权库,主要存储系统用户的权限信息
test: MySQL数据库系统自动创建的测试数据库
创建需要的业务主库…
4.3 忘记MySQL密码:
4.3.1 恢复密码
停止服务

[root@localhost /]# systemctl stop mysqld

修改配置文件

#socket=/var/lib/mysql/mysql.sock
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks

重起服务

[root@localhost /]# systemctl start mysqld

登入数据库

[root@localhost /]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, 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>

切换当前数据库

mysql> use mysql
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed
mysql>

修改密码

mysql> update user set authentication_string=password('123456') where user='root' and Host = 'localhost';
Query OK, 0 rows affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 0 Warnings: 1

Mysql8.0以上版本

mysql> flush privileges;
mysql> alter user 'root'@'localhost'IDENTIFIED BY 'newpassword';

退出数据库并停止服务

mysql> exit
Bye
[root@localhost /]# systemctl stop mysqld

修改配置文件

#socket=/var/lib/mysql/mysql.sock
#skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks

重起服务登入系统

[root@localhost /]# systemctl restart mysqld
[root@localhost /]# mysql -uroot -p'ABC123com'
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 10
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: