Linux安装MySQL
源码安装
从5.5开始使用cmake编译源码安装MySQL数据库。
开始我们需要去官网下载源:https://downloads.mysql.com/archives/community/
在虚拟器上创建一个/soft目录,将下载好的文件传送到/soft目录下,虚拟器的内存要大一点否则会卡。
若是Redhat的虚拟器在安装之前需要安装yum源 ,CentOS的虚拟器则不需要,在/etc/yum.repos.d/目录下创建一个CentOS-8.repo的文件,然后编辑CentOS-8.repo文件,将一下内容复制进去:
#CentOS-Base.repo
#The mirror system uses the connecting IP address of the client and the
#update status of each mirror to pick mirrors that are updated to and
#geographically close to the client. You should use this for CentOS updates
#unless you are manually picking other mirrors.
#If the mirrorlist= does not work for you, as a fall back you can try the
#remarked out baseurl= line instead.
[base]
name=CentOS-
r
e
l
e
a
s
e
v
e
r
−
B
a
s
e
−
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
f
a
i
l
o
v
e
r
m
e
t
h
o
d
=
p
r
i
o
r
i
t
y
b
a
s
e
u
r
l
=
h
t
t
p
s
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
/
c
e
n
t
o
s
/
releasever - Base - mirrors.aliyun.com failovermethod=priority baseurl=https://mirrors.aliyun.com/centos/
releasever−Base−mirrors.aliyun.comfailovermethod=prioritybaseurl=https://mirrors.aliyun.com/centos/releasever/BaseOS/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.aliyuncs.com/centos/
basearch/os/http://mirrors.aliyuncs.com/centos/releasever/BaseOS/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
c
l
o
u
d
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.cloud.aliyuncs.com/centos/
basearch/os/http://mirrors.cloud.aliyuncs.com/centos/releasever/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
#additional packages that may be useful
[extras]
name=CentOS-
r
e
l
e
a
s
e
v
e
r
−
E
x
t
r
a
s
−
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
f
a
i
l
o
v
e
r
m
e
t
h
o
d
=
p
r
i
o
r
i
t
y
b
a
s
e
u
r
l
=
h
t
t
p
s
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
/
c
e
n
t
o
s
/
releasever - Extras - mirrors.aliyun.com failovermethod=priority baseurl=https://mirrors.aliyun.com/centos/
releasever−Extras−mirrors.aliyun.comfailovermethod=prioritybaseurl=https://mirrors.aliyun.com/centos/releasever/extras/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.aliyuncs.com/centos/
basearch/os/http://mirrors.aliyuncs.com/centos/releasever/extras/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
c
l
o
u
d
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.cloud.aliyuncs.com/centos/
basearch/os/http://mirrors.cloud.aliyuncs.com/centos/releasever/extras/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-
r
e
l
e
a
s
e
v
e
r
−
P
l
u
s
−
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
f
a
i
l
o
v
e
r
m
e
t
h
o
d
=
p
r
i
o
r
i
t
y
b
a
s
e
u
r
l
=
h
t
t
p
s
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
/
c
e
n
t
o
s
/
releasever - Plus - mirrors.aliyun.com failovermethod=priority baseurl=https://mirrors.aliyun.com/centos/
releasever−Plus−mirrors.aliyun.comfailovermethod=prioritybaseurl=https://mirrors.aliyun.com/centos/releasever/centosplus/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.aliyuncs.com/centos/
basearch/os/http://mirrors.aliyuncs.com/centos/releasever/centosplus/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
c
l
o
u
d
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.cloud.aliyuncs.com/centos/
basearch/os/http://mirrors.cloud.aliyuncs.com/centos/releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[PowerTools]
name=CentOS-
r
e
l
e
a
s
e
v
e
r
−
P
o
w
e
r
T
o
o
l
s
−
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
f
a
i
l
o
v
e
r
m
e
t
h
o
d
=
p
r
i
o
r
i
t
y
b
a
s
e
u
r
l
=
h
t
t
p
s
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
/
c
e
n
t
o
s
/
releasever - PowerTools - mirrors.aliyun.com failovermethod=priority baseurl=https://mirrors.aliyun.com/centos/
releasever−PowerTools−mirrors.aliyun.comfailovermethod=prioritybaseurl=https://mirrors.aliyun.com/centos/releasever/PowerTools/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.aliyuncs.com/centos/
basearch/os/http://mirrors.aliyuncs.com/centos/releasever/PowerTools/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
c
l
o
u
d
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.cloud.aliyuncs.com/centos/
basearch/os/http://mirrors.cloud.aliyuncs.com/centos/releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[AppStream]
name=CentOS-
r
e
l
e
a
s
e
v
e
r
−
A
p
p
S
t
r
e
a
m
−
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
f
a
i
l
o
v
e
r
m
e
t
h
o
d
=
p
r
i
o
r
i
t
y
b
a
s
e
u
r
l
=
h
t
t
p
s
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
.
c
o
m
/
c
e
n
t
o
s
/
releasever - AppStream - mirrors.aliyun.com failovermethod=priority baseurl=https://mirrors.aliyun.com/centos/
releasever−AppStream−mirrors.aliyun.comfailovermethod=prioritybaseurl=https://mirrors.aliyun.com/centos/releasever/AppStream/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.aliyuncs.com/centos/
basearch/os/http://mirrors.aliyuncs.com/centos/releasever/AppStream/
b
a
s
e
a
r
c
h
/
o
s
/
h
t
t
p
:
/
/
m
i
r
r
o
r
s
.
c
l
o
u
d
.
a
l
i
y
u
n
c
s
.
c
o
m
/
c
e
n
t
o
s
/
basearch/os/ http://mirrors.cloud.aliyuncs.com/centos/
basearch/os/http://mirrors.cloud.aliyuncs.com/centos/releasever/AppStream/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
安装完yum源后就开始进行源码安装:
1.安装开发工具和开发包
2.编译安装MySQL
[root@localhost ~]# tar xf mysql-boost-5.7.14.tar.gz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/mysql-5.7.14/
创建用户和组
输入以下代码:cmake -DCMAKE_INSTALL_PREFIX=/soft/mysql
-DMYSQL_DATADIR=/soft/mysql/data -DSYSCONFDIR=/etc/my.cnf
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1
-DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0
-DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
-DWITH_BOOST=/soft/mysql-5.7.14/boost/
编译安装
3.初始化 会给一个初始密码
4.为mysql提供主配置文件
[root@bogon mysql-5.7.14]# cd /usr/local/mysql/
[root@bogon mysql]# \cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
然后编辑/etc/my.cnf文件,添加如下行:
[mysqld]
datadir = /usr/local/mysql/data
文件里#开头是注释,要把datadir前面的#去掉才能生效
5.为mysql提供sysv服务脚本
将/usr/local/mysql/support-files/下的mysql.sever文件复制到/etc/init.d/mysqld中,然后给予这个文件执行的权限
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
6、修改PATH环境变量,让系统可以直接使用mysql的相关命令
编辑/etc/profile.d/mysql.sh文件,将export PATH=
P
A
T
H
:
/
u
s
r
/
l
o
c
a
l
/
m
y
s
q
l
/
b
i
n
写
入
到
文
件
中
,
然
后
启
用
该
文
件
v
i
m
/
e
t
c
/
p
r
o
f
i
l
e
.
d
/
m
y
s
q
l
.
s
h
e
x
p
o
r
t
P
A
T
H
=
PATH:/usr/local/mysql/bin写入到文件中,然后启用该文件 vim /etc/profile.d/mysql.sh export PATH=
PATH:/usr/local/mysql/bin写入到文件中,然后启用该文件vim/etc/profile.d/mysql.shexportPATH=PATH:/usr/local/mysql/bin
source mysql.sh
7.启动MySQL:
systemctl start mysqld
rpm包安装
在官网下载需要安装的离线包: mysql-5.7.14-1.el7.x86_64.rpm-bundle.tar
将文件传输到虚拟器中
解压查看:tar xf mysql-5.7.14-1.el7.x86_64.rpm-bundle.tar
安装mysql-community-common
其次安装client libs,server,在安装这些之前我们需要用yum安装其他东西
然后安装
重启服务器改密码
systemctl start mysqld
systemctl enable mysqld
systemctl is-active mysqld.service
mysql -u root -p
二进制安装
下载mysql安装包,选择这个版本mysql-5.7.14-linux-glibc2.5-x86_64.tar
新建用户以安全方式运行进程
groupadd -r -g 306 mysql
useradd -g 306 -r -u 306 mysql
安装mysql
初始化,初始化时会给一个初始密码一定要记住
[root@localhost local]# chown -R mysql.mysql mysql/*
mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql –
datadir=/usr/local/mysql/data
为mysql提供主配置文件
添加文件存放位置
datadir = /usr/local/mysql/data
为mysql提供sysv服务脚本
启动mysql
#chkconfig --add mysqld --添加为系统服务
#chkconfig mysqld on --开机自启动
systemctl start mysqld
将mysql的bin目录加入PATH环境变量,编辑/etc/profile文件
#cd /etc/profile.d/
#vim mysql.sh
添加 export PATH=$PATH:/usr/local/mysql/bin
#source mysql.sh