MySQL安装(CentOS)

简介:

在CentOS上安装MySQL

安装环境及版本:

系统环境:CentOS Linux release 7.5.1804 (Core)
MySQL版本:mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

一、准备

检查系统中是否存在使用rpm安装的mysql或者mariadb,如果有需要先删除后再编译安装。

是否已经安装mysql:rpm -qa | grep mysql
是否已经安装mariadb:rpm -qa | grep mariadb
rpm -e xxx #一般使用此命令即可卸载成功
rpm -e –nodeps xxx #卸载不成功时使用此命令强制卸载

[root@localhost bin]# rpm -qa | grep mysql
[root@localhost bin]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@localhost bin]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
[root@localhost bin]# 
[root@localhost bin]# rpm -qa | grep mariadb

二、安装

2.1 安装依赖包

命令:yum -y install unzip wget

[root@localhost ~]# yum -y install unzip wget
Loaded plugins: fastestmirror
Determining fastest mirrors
................
Installed:
  wget.x86_64 0:1.14-18.el7_6.1                                                                                                                                                                                                               

Updated:
  unzip.x86_64 0:6.0-21.el7                                                                                                                                                                                                                   

Complete!

命令:yum -y install gcc gcc-c++ autoconf libtool

[root@localhost ~]# yum -y install gcc gcc-c++ autoconf libtool
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
.....
Updated:
  gcc.x86_64 0:4.8.5-39.el7                                                                                           gcc-c++.x86_64 0:4.8.5-39.el7                                                                                          

Dependency Updated:
  cpp.x86_64 0:4.8.5-39.el7                   gcc-gfortran.x86_64 0:4.8.5-39.el7     libgcc.x86_64 0:4.8.5-39.el7              libgfortran.x86_64 0:4.8.5-39.el7     libgomp.x86_64 0:4.8.5-39.el7     libquadmath.x86_64 0:4.8.5-39.el7    
  libquadmath-devel.x86_64 0:4.8.5-39.el7     libstdc++.x86_64 0:4.8.5-39.el7        libstdc++-devel.x86_64 0:4.8.5-39.el7    

Complete!

命令:yum install -y libaio-*

[root@localhost bin]# yum install -y libaio-*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
...................
  Installing : libaio-devel-0.3.109-13.el7.x86_64                                                                                                                                                                                         1/1 
  Verifying  : libaio-devel-0.3.109-13.el7.x86_64                                                                                                                                                                                         1/1 

Installed:
  libaio-devel.x86_64 0:0.3.109-13.el7                                                                                                                                                                                                        

Complete!

2.2 安装

进入文件目录:cd /usr/local/src/
下载安装文件:wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/mysql/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

[root@localhost src]# wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/mysql/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
--2020-07-02 20:37:55--  http://zy-res.oss-cn-hangzhou.aliyuncs.com/mysql/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
Resolving zy-res.oss-cn-hangzhou.aliyuncs.com (zy-res.oss-cn-hangzhou.aliyuncs.com)... 118.31.219.222
Connecting to zy-res.oss-cn-hangzhou.aliyuncs.com (zy-res.oss-cn-hangzhou.aliyuncs.com)|118.31.219.222|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 654007697 (624M) [application/octet-stream]
Saving to: ‘mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz’

100%[====================================================================================================================================================================================================>] 654,007,697 2.04MB/s   in 5m 3s  

2020-07-02 20:42:58 (2.06 MB/s) - ‘mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz’ saved [654007697/654007697]

解压缩:tar zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

[root@localhost src]# tar zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mysql-5.7.17-linux-glibc2.5-x86_64/bin/myisam_ftdump
mysql-5.7.17-linux-glibc2.5-x86_64/bin/myisamchk
……………
mysql-5.7.17-linux-glibc2.5-x86_64/support-files/mysql.server
mysql-5.7.17-linux-glibc2.5-x86_64/docs/INFO_BIN
mysql-5.7.17-linux-glibc2.5-x86_64/docs/INFO_SRC

新建安装目录: mkdir -p /usr/local/mysql
将解压缩后的文件移到安装目录: mv mysql-5.7.17-linux-glibc2.5-x86_64/* /usr/local/mysql/

[root@localhost src]# mkdir -p /usr/local/mysql
[root@localhost src]# mv mysql-5.7.17-linux-glibc2.5-x86_64/* /usr/local/mysql/
[root@localhost src]# ll /usr/local/mysql/
total 36
drwxr-xr-x.  2 root root   4096 Jul  2 20:44 bin
-rw-r--r--.  1 7161 31415 17987 Nov 28  2016 COPYING
drwxr-xr-x.  2 root root     55 Jul  2 20:44 docs
drwxr-xr-x.  3 root root   4096 Jul  2 20:44 include
drwxr-xr-x.  5 root root    229 Jul  2 20:44 lib
drwxr-xr-x.  4 root root     30 Jul  2 20:44 man
-rw-r--r--.  1 7161 31415  2478 Nov 28  2016 README
drwxr-xr-x. 28 root root   4096 Jul  2 20:44 share
drwxr-xr-x.  2 root root    112 Jul  2 20:44 support-files

2.3 建立mysql组和用户

建立用户:groupadd mysql
该用户不可登录服务器:useradd -g mysql -s /sbin/nologin mysql

[root@localhost src]# groupadd mysql
[root@localhost src]# useradd -g mysql -s /sbin/nologin mysql

2.4 初始化MySQL数据库

命令:/usr/local/mysql/bin/mysqld –initialize-insecure –datadir=/usr/local/mysql/data/ –user=mysql

[root@localhost src]# /usr/local/mysql/bin/mysqld --initialize-insecure --datadir=/usr/local/mysql/data/ --user=mysql
2020-07-03T00:48:35.723842Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-03T00:48:36.140467Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-07-03T00:48:36.206847Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-07-03T00:48:36.282295Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ed871cd9-bcc6-11ea-ab90-000c29d9f915.
2020-07-03T00:48:36.304558Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-07-03T00:48:36.305343Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

2.5 更改安装目录属性

将安装目录改为mysql可以访问:
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/data/

[root@localhost src]# cd /usr/local/
[root@localhost local]# ll
total 0
drwxr-xr-x. 13 root root 152 Jul  2 19:46 apache
drwxr-xr-x.  6 root root  58 Jul  2 19:28 apr
drwxr-xr-x.  5 root root  43 Jul  2 19:35 apr-util
drwxr-xr-x.  2 root root   6 Apr 11  2018 bin
drwxr-xr-x.  2 root root   6 Apr 11  2018 etc
drwxr-xr-x.  2 root root   6 Apr 11  2018 games
drwxr-xr-x.  2 root root   6 Apr 11  2018 include
drwxr-xr-x.  2 root root   6 Apr 11  2018 lib
drwxr-xr-x.  2 root root   6 Apr 11  2018 lib64
drwxr-xr-x.  2 root root   6 Apr 11  2018 libexec
drwxr-xr-x. 10 root root 141 Jul  2 20:48 mysql
drwxr-xr-x.  6 root root  56 Jul  2 19:39 pcre
drwxr-xr-x.  2 root root   6 Apr 11  2018 sbin
drwxr-xr-x.  5 root root  49 Apr 23  2019 share
drwxr-xr-x.  7 root root 277 Jul  2 20:44 src
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql/data/
[root@localhost local]# ll
total 0
drwxr-xr-x. 13 root  root  152 Jul  2 19:46 apache
drwxr-xr-x.  6 root  root   58 Jul  2 19:28 apr
drwxr-xr-x.  5 root  root   43 Jul  2 19:35 apr-util
drwxr-xr-x.  2 root  root    6 Apr 11  2018 bin
drwxr-xr-x.  2 root  root    6 Apr 11  2018 etc
drwxr-xr-x.  2 root  root    6 Apr 11  2018 games
drwxr-xr-x.  2 root  root    6 Apr 11  2018 include
drwxr-xr-x.  2 root  root    6 Apr 11  2018 lib
drwxr-xr-x.  2 root  root    6 Apr 11  2018 lib64
drwxr-xr-x.  2 root  root    6 Apr 11  2018 libexec
drwxr-xr-x. 10 mysql mysql 141 Jul  2 20:48 mysql
drwxr-xr-x.  6 root  root   56 Jul  2 19:39 pcre
drwxr-xr-x.  2 root  root    6 Apr 11  2018 sbin
drwxr-xr-x.  5 root  root   49 Apr 23  2019 share
drwxr-xr-x.  7 root  root  277 Jul  2 20:44 src

2.6 设置开机自启动

将mysql.server文件拷贝到启动运行目录:cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@localhost support-files]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost support-files]# ll /etc/init.d
lrwxrwxrwx. 1 root root 11 Apr 23  2019 /etc/init.d -> rc.d/init.d

添加可执行权限:chmod +x /etc/init.d/mysqld

[root@localhost support-files]# chmod +x /etc/init.d/mysqld 
[root@localhost support-files]# ll /etc/init.d
lrwxrwxrwx. 1 root root 11 Apr 23  2019 /etc/init.d -> rc.d/init.d

修改vi /etc/rc.d/rc.local,增加启动命令

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

/etc/init.d/mysqld start

2.7 设置环境变量

编辑root环境变量:vi /root/.bash_profile
在PATH路径后添加::/usr/local/mysql/bin:/usr/local/mysql/lib

[root@localhost support-files]# vi /root/.bash_profile 
[root@localhost support-files]# cat /root/.bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib

export PATH

使环境变量生效:source /root/.bash_profile

2.8 启动数据库

直接运行开机自启动设置的命令即可:/etc/init.d/mysqld start

[root@localhost support-files]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
. SUCCESS! 

查看3306端口使用启用:netstat -tnlp

[root@localhost support-files]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1058/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1271/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      50127/mysqld        
tcp6       0      0 :::80                   :::*                    LISTEN      49814/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      1058/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1271/master 

2.9 修改root密码

初始化后mysql为空密码可直接登录,为了保证安全性需要修改mysql的root用户密码,命令:mysqladmin -uroot password ‘******’

[root@localhost support-files]# mysqladmin -uroot password 'pyrk-0811'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

2.10 设置远程连接

命令:grant all privileges on . to ‘root’@’%’ identified by ‘pyrk-0811’ with grant option;

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.65-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.

MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by 'pyrk-0811' with grant option;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

2.11 其他参数的修改

最大连接数,编码格式等,可参考《Mariadb安装》

三、验证登录

命令登录:mysql -uroot -p

[root@localhost support-files]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17 MySQL Community Server (GPL)

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.

还可以通过客户端登录

发表回复