数据的分类:
分层结构由IBM在20世纪60年代开发,并在早期大型机DBMS中使用。记录的关系形成了一个树状模
型。这种结构简单,但缺乏灵活性,因为这种关系仅限于一对多关系。
代表数据库:IBM IMS(信息管理系统)
1964年通用电气GE公司的 Charles Bachman 成功地开发出世界上第一个网状数据库IDS(集成数据存
储),IDS 具有数据模式和日志的特征,只能在GE主机运行
Relational Database Management System,关系模型最初由IBM公司的英国计算机科学家埃德加·科德
(Edgar F. Codd)于1969年描述,1974年,IBM开始开发系统R,这是一个开发RDBMS原型的研究项目。
然而,第一个商业上可用的RDBMS是甲骨文,于1979年由关系软件(现为甲骨文公司)发布
https://db-engines.com/en/ranking
开发工程师 CURD (Create,Update,Read,Delete)
数据库规范化,又称数据库或资料库的正规化、标准化,是数据库设计中的一系列原理和技术,以减少数据库中数据冗余,增进数据的一致性。关系模型的发明者埃德加·科德最早提出这一概念,并于1970年代初定义了第一范式、第二范式和第三范式的概念
设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,不同的规范要求被称为不同范式,各种范式呈递次规范,越高的范式数据库冗余越小
目前关系数据库有六种范式:第一范式(1NF)、第二范式(2NF)、第三范式(3NF)、巴德斯科范式(BCNF)、第四范式(4NF)和第五范式(5NF,又称完美范式)。满足最低要求的范式是第一范式(1NF)。在第一范式的基础上进一步满足更多规范要求的称为第二范式(2NF),其余范式以次类推。一般数据库只需满足第三范式(3NF)即可
规则是死的,人是活的,所以范式是否必须遵守,要看业务需要而定
掌握范式的目的是为了在合适的场景下违反范式
无重复的列,每一列都是不可分割的基本数据项,同一列中不能有多个值,即实体中的某个属性不能有多个值或者不能有重复的属性,确保每一列的原子性。除去同类型的字段,就是无重复的列。
说明:第一范式(1NF)是对关系模式的基本要求,不满足第一范式(1NF)的数据库就不是关系数据库。
第二范式必须先满足第一范式,属性完全依赖于主键,要求表中的每个行必须可以被唯一地区分,通常为表加上每行的唯一标识PK,非PK的字段需要与整个PK有直接相关性,即非PK的字段不能依赖于部分主键。
满足第三范式必须先满足第二范式属性,非主键属性不依赖于其它非主键属性。第三范式要求一个数据表中不包含已在其它表中已包含的非主关键字信息,非PK的字段间不能有从属关系。
SQL:Structure Query Language,结构化查询语言是1974年由Boyce和Chamberlin提出的一个通用的、功能极强的关系性数据库语言
SQL解释器:将SQL语句解释成机器语言
数据存储协议:应用层协议,C/S
客户端程序接口
应用编程接口
1979年:TcX公司 Monty Widenius,Unireg
1996年:发布MySQL1.0,Solaris版本,Linux版本
1999年:MySQL AB公司,瑞典
2003年:MySQL 5.0版本,提供视图、存储过程等功能
2008年:Sun公司 以10亿美元收购MySQL
2009年:Oracle公司以 75 亿美元收购 sun 公司
2009年:Monty成立MariaDB
https://www.mysql.com/
http://mariadb.org/
https://www.percona.com/
https://dev.mysql.com/doc/
https://mariadb.com/kb/en/
https://www.percona.com/software/mysql-database/percona-server
MySQL:5.1 --> 5.5 --> 5.6 --> 5.7 -->8.0
MariaDB:5.1 -->5.5 -->10.0–> 10.1 --> 10.2 --> 10.3 --> 10.4 --> 10.5
MySQL主流5.6,5.7版本,8.0也有企业在使用。
MySQL被Sun收购后,搞了个过渡的6.0版本,没多久就下线了,后来被Oracle收购后,终于迎来了像样的
5.6版本,之后就是5.7、8.0版本。由于6.0版本号已被用过,7.x系列版本专用于NDB Cluster,因而新
版本号从8.0开始。
MyISAM ==> Aria
InnoDB ==> XtraDB
MySQL server端端口号为3306
CentOS 安装光盘
项目官方:https://downloads.mariadb.org/mariadb/repositories/
国内镜像:https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/
CentOS 8:安装光盘直接提供
[root@centos8 ~]#yum install mysql-server #安装
[root@centos8 ~]#systemctl enable --now mysqld #第一次启动时,会自动把系统数据初始化放在所在目录里
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@centos8 ~]#ls /var/lib/mysql #数据库存放(系统数据和用户数据)所在路径
auto.cnf ib_buffer_pool mysql.ibd private_key.pem
binlog.000001 ibdata1 mysql.sock public_key.pem
binlog.index ib_logfile0 mysql.sock.lock server-cert.pem
ca-key.pem ib_logfile1 mysql_upgrade_info server-key.pem
ca.pem ibtmp1 mysqlx.sock sys
client-cert.pem '#innodb_temp' mysqlx.sock.lock undo_001
client-key.pem mysql performance_schema undo_002
CentOS 7:安装光盘直接提供
范例: CentOS 7 安装MySQL5.7
[root@centos7 ~]#vim /etc/yum.repos.d/mysql.repo #建立仓库
[mysql]
name=mysql5.7
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
gpgcheck=0
-------
[root@centos7 ~]#yum install -y mysql-community-server.x86_64 #安装
[root@centos7 ~]#systemctl enable --now mysqld #启动服务
-------
[root@centos7 ~]#mysql #centos7会默认生成一个密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@centos7 ~]#grep password /var/log/mysqld.log
2021-01-28T09:11:01.698455Z 1 [Note] A temporary password is generated for root@localhost:JUI4NslG5C:o
2021-01-28T09:15:37.741193Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
----
#使用默认密码连接,更改密码
----
[root@centos7 ~]#mysql -uroot -p'JUI4NslG5C:o'
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.33
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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 'Magedu0!';
Query OK, 0 rows affected (0.00 sec) #密码需设置复杂一些,简单密码不允许
->
方法二:
[root@centos7 ~]#mysqladmin -uroot -p'JUI4NslG5C:o' password 'Magedu0!'
范例: CentOS 7 安装Mariadb
参考网站信息,配置yum源
https://mariadb.org/download/#mariadb-repositories
[root@centos7 ~]#vim /etc/yum.repos.d/mariadb.repo
# MariaDB 10.5 CentOS repository list - created 2021-01-28 09:39 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
baseurl = https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.5/centos7-amd64
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
[root@centos7 ~]#yum install -y MariaDB-server.x86_64
[root@centos7 ~]#systemctl enable --now mariadb.service
CentOS 6:
范例: Ubuntu 安装 MySQL
[root@ubuntu1804 ~]#apt install mysql-server
[root@ubuntu1804 ~]#systemctl status mysql.service
运行脚本:mysql_secure_installation
设置数据库管理员root口令
禁止root远程登录
删除anonymous用户帐号
删除test数据库
范例: 针对MySQL5.6前版本进行安全加固
1、安装mysql5.6
[root@localhost ~]# yum install -y mysql-community-server.x86_64
2、启动服务
[root@localhost ~]# systemctl enable --now mysqld
3、运行脚本
[root@localhost ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y #是否设置root密码?
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
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? [Y/n] y #是否删除匿名账户?
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y 不允许根用户远程登录?
... 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.
Remove test database and access to it? [Y/n] y #删除测试数据库并访问它
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- 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? [Y/n] y #立即重新加载权限表
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
-----------
问:为什么要对MySQL旧版本进行加固?
[root@localhost ~]# mysql
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
+------+-----------+
4 rows in set (0.00 sec)
答:不需输入密码,且匿名账户可以登录
[root@localhost ~]# mysql -uxxx
Welcome to the MySQL monitor.
MyISAM存储引擎的管理工具:
mysql用户账号由两部分组成:
USERNAME'@'HOST'
说明:
HOST限制此用户可通过哪些远程主机连接mysql服务器
支持使用通配符:
% 匹配任意长度的任意字符,相当于shell中*, 示例: 172.16.0.0/255.255.0.0 或 172.16.%.%
_ 匹配任意单个字符,相当于shell中?
mysql> \h, help
mysql> \u,use
mysql> \s,status
mysql> \!,system
#示例:
mysql>SELECT VERSION();
mysql -uUSERNAME -pPASSWORD < /path/somefile.sql
cat /path/somefile.sql | mysql -uUSERNAME -pPASSWORD
mysql>source /path/from/somefile.sql
mysql [OPTIONS] [database]
mysql客户端常用选项:
-A, --no-auto-rehash 禁止补全
-u, --user= 用户名,默认为root
-h, --host= 服务器主机,默认为localhost
-p, --passowrd= 用户密码,建议使用-p,默认为空密码
-P, --port= 服务器端口
-S, --socket= 指定连接socket文件路径
-D, --database= 指定默认数据库
-C, --compress 启用压缩
-e “SQL“ 执行SQL命令
-V, --version 显示版本
-v --verbose 显示详细信息
--print-defaults 获取程序默认使用的配置
登录系统:
#默认空密码登录
mysql -uroot -p
运行mysql命令:
mysql>use mysql
mysql>select user(); #查看当前用户
mysql>SELECT User,Host,Password FROM user;
范例:mysql的配置文件,修改提示符
#查看mysql版本
[root@centos8 ~]#mysql -V
mysql Ver 15.1 Distrib 10.3.11-MariaDB, for Linux (x86_64) using readline 5.1
#临时修改mysql提示符
[root@centos8 ~]#mysql -uroot -pcentos --prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"
#临时修改mysql提示符
[root@centos8 ~]#export MYSQL_PS1="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"
#持久修改mysql提示符
[root@centos8 ~]#vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"
[root@centos8 ~]#mysql --print-defaults -v
mysql would have been started with the following arguments:
--prompt=\r:\m:\s(\u@\h) [\d]>\_ -v
[root@centos8 ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.11-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
10:29:30(root@localhost) [(none)]> 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
10:29:34(root@localhost) [mysql]> exit
范例:配置所有MySQL 客户端的自动登录
[root@centos8 ~]#vim /etc/my.cnf
[client]
user=root
password=Magedu0!
[mysql]
prompt=(\\u@\\h) [\\d]>\\_
mysqladmin 命令格式
mysqladmin [OPTIONS] command command....
范例:
#查看mysql服务是否正常,如果正常提示mysqld is alive
mysqladmin -uroot -p'password' ping
#关闭mysql服务,但mysqladmin命令无法开启
mysqladmin -uroot -p'password' shutdown
#创建数据库testdb
mysqladmin -uroot -p'password' create testdb
#删除数据库testdb
mysqladmin -uroot -p'password' drop testdb
#修改root密码
mysqladmin -uroot -p'old-password' password 'magedu'
#日志滚动,生成新文件/var/lib/mysql/mariadb-bin.00000N
mysqladmin -uroot -p'password' flush-logs
服务器端(mysqld):工作特性有多种配置方式
1、命令行选项:
2、配置文件:类ini格式,集中式的配置,能够为mysql的各应用程序提供配置信息
服务器端配置文件:
/etc/my.cnf #Global选项
/etc/mysql/my.cnf #Global选项
~/.my.cnf #User-specific 选项
配置文件格式:
[mysqld]
[mysqld_safe]
[mysqld_multi]
[mysql]
[mysqldump]
[server]
[client]
格式:
parameter = value
说明:
_和- 相同
1,ON,TRUE意义相同, 0,OFF,FALSE意义相同,无区分大小写
服务器监听的两种 socket 地址:
只侦听本地客户端, 所有客户端和服务器的交互都通过一个socket文件实现,socket的配置存放
在/var/lib/mysql/mysql.sock) 可在/etc/my.cnf修改。
范例:
[root@centos7 ~]#vim /etc/my.cnf
[mysqld]
skip-networking=1 #忽略网络功能
groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -d /data/mysql mysql
#可选做,后面的脚本mysql_install_db可自动生成此目录
mkdir /data/mysql
chown mysql:mysql /data/mysql
tar xf mysql-VERSION-linux-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv mysql-VERSION mysql
chown -R root:root /usr/local/mysql/
cd /usr/local/mysql
cp -b support-files/my-default.cnf /etc/my.cnf
vim /etc/my.cnf
#mysql语句块中添加以下三个选项
[mysqld]
datadir = /data/mysql
innodb_file_per_table = on #在mariadb5.5以上版的是默认值,可不加
skip_name_resolve = on #禁止主机名解析,建议使用
yum -y install perl perl-devel
yum -y install libaio*
cd /usr/local/mysql/
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
[root@centos8 mysql]#ls /data/mysql/ -l
total 110604
-rw-rw---- 1 mysql mysql 12582912 Jun 1 16:44 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Jun 1 16:44 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Jun 1 16:44 ib_logfile1
drwx------ 2 mysql mysql 4096 Jun 1 16:44 mysql
drwx------ 2 mysql mysql 4096 Jun 1 16:44 performance_schema
drwx------ 2 mysql mysql 4096 Jun 1 16:44 test
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
#如果有对应的service 文件可以执行下面
cp /usr/local/mysql/support-files/systemd/mariadb.service
/usr/lib/systemd/system/
systemctl daemon-reload
systemctl enable --now mariadb
echo 'PATH=/user/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
/user/local/mysql/bin/mysql_secure_installation
[root@centos8 ~]#vim install_mysql5.6.sh
#!/bin/bash
DIR=`pwd`
NAME="mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz"
FULL_NAME=${DIR}/${NAME}
DATA_DIR="/data/mysql"
yum install -y libaio perl-Data-Dumper
if [ -f ${FULL_NAME} ];then
echo "安装文件存在"
else
echo "安装文件不存在"
exit 3
fi
if [ -h /usr/local/mysql ];then
echo "Mysql 已经安装"
exit 3
else
tar xvf ${FULL_NAME} -C /usr/local/src
ln -sv /usr/local/src/mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql
if id mysql;then
echo "mysql 用户已经存在,跳过创建用户过程"
else
useradd -r -s /sbin/nologin mysql
fi
if id mysql;then
chown -R mysql.mysql /usr/local/mysql/*
if [ ! -d /data/mysql ];then
mkdir -pv /data/mysql && chown -R mysql.mysql /data -R
/usr/local/mysql/scripts/mysql_install_db --user=mysql --
datadir=/data/mysql --basedir=/usr/local/mysql/
cp /usr/local/src/mysql-5.6.47-linux-glibc2.12-x86_64/supportfiles/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
cp ${DIR}/my.cnf /etc/my.cnf
ln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql
/etc/init.d/mysqld start
chkconfig --add mysqld
else
echo "MySQL数据目录已经存在,"
exit 3
fi
fi
fi
[root@centos8 ~]#cat /etc/my.cnf
[mysqld]
socket=/data/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
[client]
port=3306
socket=/data/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock
[root@centos8 ~]#ls
install_mysql5.6.sh my.cnf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
#!/bin/bash
. /etc/init.d/functions
DIR=`pwd`
MYSQL_VERSION=5.6.51
NAME="mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64.tar.gz"
FULL_NAME=${DIR}/${NAME}
URL=http://mirrors.163.com/mysql/Downloads/MySQL-5.6
DATA_DIR="/data/mysql"
rpm -q wget || yum -y -q install wget
wget $URL/$NAME || {
action "下载失败,异常退出" false;exit 10; }
yum install -y -q libaio perl-Data-Dumper autoconf
if [ -f ${FULL_NAME} ];then
action "安装文件存在"
else
action "安装文件不存在" false
exit 3
fi
if [ -e /usr/local/mysql ];then
action "Mysql 已经安装" false
exit 3
else
tar xf ${FULL_NAME} -C /usr/local/src
ln -sv /usr/local/src/mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64
/usr/local/mysql
if id mysql;then
action "mysql 用户已经存在,跳过创建用户过程"
else
useradd -r -s /sbin/nologin mysql
fi
if id mysql;then
chown -R mysql.mysql /usr/local/mysql/*
if [ ! -d /data/mysql ];then
mkdir -pv /data/mysql && chown -R mysql.mysql /data
/usr/local/mysql/scripts/mysql_install_db --user=mysql --
datadir=/data/mysql --basedir=/usr/local/mysql/
cp /usr/local/src/mysql-${MYSQL_VERSION}-linux-glibc2.12-
x86_64/support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
cat > /etc/my.cnf <<-'EOF'
[mysqld]
socket=/data/mysql/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
[client]
port=3306
socket=/data/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock
EOF
ln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql
/etc/init.d/mysqld start
chkconfig --add mysqld
else
action "MySQL数据目录已经存在" false
exit 3
fi
fi
fi
yum -y install libaio numactl-libs
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linuxglibc2.12-x86_64.tar.gz
tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local/
ln -s mysql-5.7.31-linux-glibc2.12-x86_64/ mysql
chown -R root.root /usr/local/mysql/
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
cp /etc/my.cnf{
,.bak}
vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
mysqld --initialize --user=mysql --datadir=/data/mysql
...省略...
2019-07-04T13:03:54.258140Z 1 [Note] A temporary password is generated for
root@localhost: LufavlMka6,! #注意生成root的初始密码
grep password /data/mysql/mysql.log
2019-12-26T13:31:30.458826Z 1 [Note] A temporary password is generated for
root@localhost: LufavlMka6,!
awk '/temporary password/{print $NF}' /data/mysql/mysql.log
LufavlMka6,!
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
mysqladmin -uroot -p'LufavlMka6,!' password magedu
#修改前面生成的随机密码为指定密码
mysqladmin -uroot -p'LufavlMka6,!' password magedu
#修改前面生成的空密码为指定密码
mysqladmin -uroot password magedu
mysql -uroot -pmagedu
#!/bin/bash
. /etc/init.d/functions
SRC_DIR=`pwd`
#MYSQL='mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz'
MYSQL='mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz'
COLOR='echo -e \E[01;31m'
END='\E[0m'
MYSQL_ROOT_PASSWORD=magedu
check (){
if [ $UID -ne 0 ]; then
action "当前用户不是root,安装失败" false
exit 1
fi
cd $SRC_DIR
if [ ! -e $MYSQL ];then
$COLOR"缺少${MYSQL}文件"$END
$COLOR"请将相关软件放在${SRC_DIR}目录下"$END
exit
elif [ -e /usr/local/mysql ];then
action "数据库已存在,安装失败" false
exit
else
return
fi
}
install_mysql(){
$COLOR"开始安装MySQL数据库..."$END
yum -y -q install libaio numactl-libs &> /dev/null
cd $SRC_DIR
tar xf $MYSQL -C /usr/local/
MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
chown -R root.root /usr/local/mysql/
id mysql &> /dev/null || {
useradd -s /sbin/nologin -r mysql ; action "创建
mysql用户"; }
echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
[ -d /data ] || mkdir /data
mysqld --initialize --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
[ $? -ne 0 ] && {
$COLOR"数据库启动失败,退出!"$END;exit; }
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}'
/data/mysql/mysql.log`
mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD
&>/dev/null
action "数据库安装完成"
}
check
install_mysql
[root@centos7 ~]#ccat install_online_mysql5.7or8.0_for_centos.sh
#!/bin/bash
. /etc/init.d/functions
SRC_DIR=`pwd`
MYSQL='mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz'
URL=http://mirrors.163.com/mysql/Downloads/MySQL-5.7
#MYSQL='mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz'
#URL=http://mirrors.163.com/mysql/Downloads/MySQL-8.0
COLOR='echo -e \E[01;31m'
END='\E[0m'
MYSQL_ROOT_PASSWORD=magedu
check (){
if [ $UID -ne 0 ]; then
action "当前用户不是root,安装失败" false
exit 1
fi
cd $SRC_DIR
rpm -q wget || yum -y -q install wget
wget $URL/$MYSQL
if [ ! -e $MYSQL ];then
$COLOR"缺少${MYSQL}文件"$END
$COLOR"请将相关软件放在${SRC_DIR}目录下"$END
exit
elif [ -e /usr/local/mysql ];then
action "数据库已存在,安装失败" false
exit
else
return
fi
}
install_mysql(){
$COLOR"开始安装MySQL数据库..."$END
yum -y -q install libaio numactl-libs libaio &> /dev/null
cd $SRC_DIR
tar xf $MYSQL -C /usr/local/
MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
chown -R root.root /usr/local/mysql/
id mysql &> /dev/null || {
useradd -s /sbin/nologin -r mysql ; action "创建
mysql用户"; }
echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=`hostname -I|cut -d. -f4`
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
mysqld --initialize --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
[ $? -ne 0 ] && {
$COLOR"数据库启动失败,退出!"$END;exit; }
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}'
/data/mysql/mysql.log`
mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD
&>/dev/null
action "数据库安装完成"
}
check
install_mysql
[root@centos7 ~]#cd /usr/local/src
[root@centos7 src]#yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel perl-Data-Dumper
useradd -r -s /sbin/nologin -d /data/mysql mysql #创建一个mysql的用户,数据库文件存放在/data/mysql下
mkdir /data/mysql
chown -R mysql.mysql /data/mysql #把所属组设置成mysql
编译安装说明
利用cmake编译,而利用传统方法,cmake的重要特性之一是其独立于源码(out-of-source)的编译功能,
即编译工作可以在另一个指定的目录中而非源码目录中进行,这可以保证源码目录不受任何一次编译的
影响,因此在同一个源码树上可以进行多次不同的编译,如针对于不同平台编译
编译选项:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
[root@centos7 ~]#ls
anaconda-ks.cfg mysql-5.6.51.tar.gz
[root@centos7 ~]#tar xvf mysql-5.6.51.tar.gz -C /usr/local/src
[root@centos7 ~]#cd /usr/local/src
[root@centos7 ~]#cd mysql-5.6.51/
cmake . \
-DCMAKE_INSTALL_PREFIX=/apps/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/ \
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make && make install
提示:如果出错,执行rm -f CMakeCache.txt
echo 'PATH=/apps/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
或者用软链接实现
ln -s /usr/local/mysql/bin/mysql /usr/local/bin
cd /apps/mysql/ #进入该目录下
scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
cp -b /apps/mysql/support-files/my-default.cnf /etc/my.cnf
#针对旧版本或mariadb-10.2.18.tar.gz
cp /apps/mysql/support-files/my-huge.cnf /etc/my.cnf
cp /apps/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
mysql_secure_installation
范例:
[root@ubuntu1804 ~]#docker run --name mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.30
本案例适用于以版本
mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
CentOS8 二进制安装MySQL8.0,并实现三个实例
一台系统CentOS 8.X主机
关闭SElinux
关闭防火墙
时间同步
[root@centos8 ~]#wget http://mirrors.163.com/mysql/Downloads/MySQL-8.0/mysql-8.0.23-linux-glibc2.12-x86_64.tar
#解压
[root@centos8 ~]#tar xf mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
#创建软链接
[root@centos8 ~]#ln -s /usr/local/mysql-8.0.23-linux-glibc2.12-x86_64/ /usr/local/mysql
[root@centos8 ~]#useradd -r -s /sbin/nologin mysql
#更改所有者,所属组
[root@centos8 ~]#chown -R mysql.mysql /usr/local/mysql/
[root@centos8 ~]#echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos8 ~]#. /etc/profile.d/mysql.sh
#或用软链接实现
[root@centos8 ~]#ln -s /usr/local/mysql/bin/mysql /usr/local/bin
[root@centos8 ~]#cd /usr/local
[root@centos8 local]#mkdir -p /mysql/{3306,3307,3308}
[root@centos8 local]#chown -R mysql.mysql /mysql/
[root@centos8 local]#ll /mysql/
total 0
drwxr-xr-x 2 mysql mysql 6 Jan 28 13:09 3306
drwxr-xr-x 2 mysql mysql 6 Jan 28 13:09 3307
drwxr-xr-x 2 mysql mysql 6 Jan 28 13:09 3308
[root@centos8 local]#tree /mysql
/mysql
├── 3306
├── 3307
└── 3308
3 directories, 0 files
#先安装包,否则会出错
[root@centos8 local]#yum -yq install libaio
#针对每个实例初始化,生成空密码
[root@centos8 local]#mysqld --initialize-insecure --user=mysql --datadir=/mysql/3306
[root@centos8 local]#mysqld --initialize-insecure --user=mysql --datadir=/mysql/3307
[root@centos8 local]#mysqld --initialize-insecure --user=mysql --datadir=/mysql/3308
[root@centos8 local]#tree /mysql/ -d
/mysql/
├── 3306
│ ├── #innodb_temp
│ ├── mysql
│ ├── performance_schema
│ └── sys
├── 3307
│ ├── #innodb_temp
│ ├── mysql
│ ├── performance_schema
│ └── sys
└── 3308
├── #innodb_temp
├── mysql
├── performance_schema
└── sys
15 directories
[root@centos8 ~]#file `which mysqld_multi`
/usr/local/mysql/bin/mysqld_multi: Perl script text executable
#每个实例中对应mysqlx_port行MySQL8.0版本需要分别指定,MySQL5.7无需指定
[root@centos8 ~]#cat /etc/my.cnf
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
[mysqld3306]
datadir = /mysql/3306
port = 3306
mysqlx_port = 33060
socket &