Centos---MySQL数据库部署

MySQL数据库部署

    • 一、环境
    • 二、YUM在线安装
    • 三、RPM离线安装
    • 四、通用二进制安装
    • 五、源码安装

一、环境

基于Centos 7.4 系统
查看系统环境

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

安装常用的软件包

yum install -y vim bash-completion lrzsz wget net-tools psmisc tree  dos2unix 

系统配置

关闭防火墙
[root@localhost ~]# systemctl stop firewalld
禁用开机自启防火墙
[root@localhost ~]# systemctl disable firewalld
关闭selinux
[root@localhost ~]# setenforce 0
禁用开机自启selinux
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled

二、YUM在线安装

下载并安装MySQL的官方yum源
进入官网
Centos---MySQL数据库部署_第1张图片
Centos---MySQL数据库部署_第2张图片
Centos---MySQL数据库部署_第3张图片
Centos---MySQL数据库部署_第4张图片
下载MySQL Yum Repository

[root@localhost yum.repos.d]# wget -c https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm

安装yum源

[root@localhost ~]# yum localinstall mysql80-community-release-el7-5.noarch.rpm 

选择发行版本

	禁用8.0:yum-config-manager --disable mysql80-community
	启用5.7:yum-config-manager --enable mysql57-community	
	该命令在centos默认是没有安装
	下载提供该命令的软件包
	[root@localhost ~]# yum provides */yum-config-manager
	验证
	[root@localhost yum.repos.d]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64       MySQL Connectors Community           230
mysql-tools-community/x86_64            MySQL Tools Community                138
mysql57-community/x86_64                MySQL 5.7 Community Server           564

在线安装

[root@localhost ~]# yum install mysql-community-server

启动数据库

[root@localhost ~]# systemctl enable --now mysqld

验证

查看状态
[root@localhost ~]# systemctl status mysqld
active (running)
查看端口
[root@localhost ~]# netstat -lnupt | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      1153/mysqld
查看进程
[root@localhost ~]# ps -ef | grep mysqld
root       4374   1190  0 04:08 pts/0    00:00:00 grep --color=auto mysqld

查看密码

[root@localhost yum.repos.d]# grep 'temporary password' /var/log/mysqld.log
2022-03-27T04:02:32.180765Z 1 [Note] A temporary password is generated for root@localhost: 3?qeP#1xLs0O
[root@localhost yum.repos.d]# awk '/temporary password/ {print $NF}' /var/log/mysqld.log 
3?qeP#1xLs0O

登录MySQL并修改密码

[root@localhost yum.repos.d]# mysql -uroot -p3?qeP#1xLs0O
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 2
Server version: 5.7.37

Copyright (c) 2000, 2022, 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 'MySQL@123';
Query OK, 0 rows affected (0.01 sec)

三、RPM离线安装

下载软件压缩包
网址:https://mirrors.huaweicloud.com

[root@localhost ~]# wget -c https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar

解压查看

[root@localhost ~]# tar xf mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar 
[root@localhost ~]# ll mysql-community*
-rw-r--r--. 1 7155 31415  26670472 Dec 15 21:01 mysql-community-client-5.7.37-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415    318884 Dec 15 21:01 mysql-community-common-5.7.37-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   4122424 Dec 15 21:01 mysql-community-devel-5.7.37-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415  47760004 Dec 15 21:02 mysql-community-embedded-5.7.37-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415  23316972 Dec 15 21:02 mysql-community-embedded-compat-5.7.37-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 132204460 Dec 15 21:02 mysql-community-embedded-devel-5.7.37-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   2474780 Dec 15 21:02 mysql-community-libs-5.7.37-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   1264836 Dec 15 21:02 mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 182274360 Dec 15 21:02 mysql-community-server-5.7.37-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 125477564 Dec 15 21:03 mysql-community-test-5.7.37-1.el7.x86_64.rpm

安装

首先查看有没有mariadb-libs软件包
[root@localhost ~]# rpm -qa | grep mariadb*
mariadb-libs-5.5.56-2.el7.x86_64
卸载mariadb软件包
[root@localhost ~]# yum remove mariadb-libs.x86_64 
安装:
[root@localhost ~]# rpm -ivh mysql-community-client-5.7.37-1.el7.x86_64.rpm  mysql-community-common-5.7.37-1.el7.x86_64.rpm  mysql-community-libs-5.7.37-1.el7.x86_64.rpm  mysql-community-server-5.7.37-1.el7.x86_64.rpm 
mysql-community-client   客户端
mysql-community-common   服务端和客户端的公共文件
mysql-community-serve	 服务端
mysql-community-libs 	 共享库

启动

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

获取数据库临时密码

[root@localhost ~]# awk '/temporary password/ {print $NF}' /var/log/mysqld.log 
,XR<UiqKw1lk

登录数据库

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.37

Copyright (c) 2000, 2022, 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 'MySQL@123';
Query OK, 0 rows affected (0.00 sec)

四、通用二进制安装

下载glibc版本的MySQL

[root@localhost ~]# wget -c https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/37-linux-glibc2.12-x86_64.tar.gz

创建用户和组

以RPM方式安装,都会创建用户mysql 
所以二进制安装时需要自己创建mysql 组和用户
[root@localhost ~]# groupadd mysql -r -g 306
(创建系统组,指定组的ID)
[root@localhost ~]# useradd -r -g mysql -s /bin/false -u 306 mysql
(创建系统用户,指定用户的基本组,无权访问系统上交互式shell)

安装并初始化MySQL

解压文件到/usr/local/
[root@localhost ~]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /uar.gz -C /usr/local/
创建软链接-u显示创建的过程
[root@localhost ~]# ln -sv /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64/ /usr/local/mysql
‘/usr/local/mysql’ -> ‘/usr/local/mysql-5.7.37-linux-glibc2.12-x86_64/’
修改/usr/local/mysql目录下所有文件的所属组和所属用户
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql/*
查看
[root@localhost ~]# ll /usr/local/mysql/
total 272
drwxr-xr-x.  2 mysql mysql   4096 Mar 28 05:02 bin
drwxr-xr-x.  2 mysql mysql     55 Mar 28 05:02 docs
drwxr-xr-x.  3 mysql mysql   4096 Mar 28 05:02 include
drwxr-xr-x.  5 mysql mysql    230 Mar 28 05:02 lib
-rw-r--r--.  1 mysql mysql 259253 Nov 29 15:09 LICENSE
drwxr-xr-x.  4 mysql mysql     30 Mar 28 05:02 man
-rw-r--r--.  1 mysql mysql    566 Nov 29 15:09 README
drwxr-xr-x. 28 mysql mysql   4096 Mar 28 05:02 share
drwxr-xr-x.  2 mysql mysql     90 Mar 28 05:02 support-files

将MySQL的bin目录加入PATH环境变量,编辑/etc/profile.d/mysql.sh

[root@localhost ~]# vim /etc/profile.d/mysql.sh
添加如下内容:
export PATH=$PATH:/usr/local/mysql/bin
(下次登录有效)
现在立刻生效:
[root@localhost ~]# source /etc/profile.d/mysql.sh

初始化

源码方式需要手工初始化
[root@localhost ~]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
(记住临时密码)
>iCg_heoE6iW

为MySQL提供主配置文件

从另一台已经安装好MySQL的linux主机中复制一份配置文件,将文件复制到/etc目录下
[root@localhost ~]# scp 192.168.6.16:/etc/my.cnf /etc
The authenticity of host '192.168.6.16 (192.168.6.16)' can't be established.
ECDSA key fingerprint is SHA256:EKgdDUYXiLBQKJBSAzsEVXqStUwk/B0M/3NmcJ1jztA.
ECDSA key fingerprint is MD5:d6:bd:d9:99:6e:65:11:7e:12:5a:af:48:01:4b:14:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.6.16' (ECDSA) to the list of known hosts.
[email protected]'s password: 
my.cnf                                   100%  960   820.6KB/s   00:00 
修改主配置文件
[root@localhost ~]# vim /etc/my.cnf
修改内容:
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
pid-file=mysqld.pid

为MySQL提供sysv服务脚本

[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server  /etc/in
it.d/mysqld
查看该文件是否有执行权限
[root@localhost ~]# ll /etc/init.d/mysqld
-rwxr-xr-x. 1 root root 10576 Mar 28 06:35 /etc/init.d/mysqld
若没有:
[root@localhost mysql]# chmod +x /etc/init.d/mysqld

启动MySQL

添加系统服务
[root@localhost ~]# chkconfig --add mysqld
开机自启动
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# systemctl start mysqld
查看MySQL状态
[root@localhost ~]# systemctl status mysqld
   Active: active (running) since Mon 2022-03-28 06:37:01 EDT; 10s ago

登录MySQL并修改密码

mysql> ALTER USER root@localhost identified by 'MySQL@123';
Query OK, 0 rows affected (0.00 sec)

五、源码安装

下载软件压缩包
网站地址mirrors.huaweicloud.com(使用国内镜像站点下载速度会比官网上下载速度快)
Centos---MySQL数据库部署_第5张图片
Centos---MySQL数据库部署_第6张图片
Centos---MySQL数据库部署_第7张图片
Centos---MySQL数据库部署_第8张图片

[root@localhost ~]# wget -c https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-boost-5.7.37.tar.gz

安装开发工具

[root@localhost ~]# yum install make gcc gcc-c++ cmake bison-devel ncurses-devel -y

解压
指定解压路径/usr/local/src/

[root@localhost ~]#tar xf mysql-boost-5.7.37.tar.gz  -C /usr/local/src/
切换到解压路径下
[root@localhost ~]# cd /usr/local/src/mysql-5.7.37/

创建用户和组

创建mysql系统组,指定组编号为306
[root@localhost ~]# groupadd -r -g 306 mysql
创建mysql系统用户,指定mysql用户的基本组,用户编号,无权访问系统上交互式shell
[root@localhost ~]# useradd -r -g mysql -u 306 -s /bin/false mysql

使用cmake编译MySQL
(注意:如果详情里此前编译所生成的文件重新编译,需要使用如下两个命令:make clean;rm -f CMakeCache.txt)

cmake -DWITH_BOOST=/usr/local/src/mysql-5.7.37/boost \	
-DWITH_SSL=system \	
-DWITH_ZLIB=system \	
-DSYSCONFDIR=/etc \	
-DMYSQL_DATADIR=/usr/local/mysql/data 
编译后报错
报错原因:
1、缺少openssl-devel
Cannot find appropriate system libraries for WITH_SSL=system.
Make sure you have specified a supported SSL version. 
Valid options are : 
system (use the OS openssl library), 
yes (synonym for system), 
</path/to/custom/openssl/installation>
解决方法:
[root@localhost mysql-5.7.37]# yum install openssl-devel -y
[root@localhost mysql-5.7.37]# make clean
make: *** No rule to make target `clean'.  Stop
[root@localhost mysql-5.7.37]#rm -f CMakeCache.txt 
再次编译:
[root@localhost mysql-5.7.37]#cmake -DWITH_BOOST=/usr/local/src/mysql-5.7.37/boost \	
-DWITH_SSL=system \	
-DWITH_ZLIB=system \	
-DSYSCONFDIR=/etc \	
-DMYSQL_DATADIR=/usr/local/mysql/data  
编译安装
[root@localhost mysql-5.7.14]# make && make install

修改环境变量,让系统可以直接使用mysql的相关命令

[root@localhost mysql-5.7.37]# vim /etc/profile.d/mysql.sh
编辑的内容:
export PATH=$PATH:/usr/loacl/mysql/bin
编辑文件立刻生效
[root@localhost mysql-5.7.37]# source /etc/profile.d/mysql.sh

初始化

mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
记住临时密码:
 LK.depOjb5If

为MySQL提供主配置文件

从以安装MySQL的linux主机上复制一份配置文件到etc目录下
[root@localhost ~]# scp 192.168.6.14:/etc/my.cnf  /etc
编辑配置为文件
[root@localhost ~]# vim /etc/my.cnf
编辑内容:
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
pid-file=mysqld.pid

提供sysv服务脚本

[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server   /etc/init.d/mysqld
查看文件是否有执行权限
[root@localhost ~]# ll /etc/init.d/mysqld
-rwxr-xr-x. 1 root root 10576 Mar 29 07:25 /etc/init.d/mysqld
添加系统服务
[root@localhost ~]# chkconfig --add mysqld
开机自启动
[root@localhost ~]# chkconfig mysqld on
启动MySQL服务
[root@localhost ~]# systemctl start mysqld
查看MySQL的状态
[root@localhost ~]# systemctl status mysqld

登录MySQL

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37

Copyright (c) 2000, 2022, 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 'MySQL@123';
Query OK, 0 rows affected (0.00 sec)

你可能感兴趣的:(mysql,数据库,database)