Linux Centos7下安装Mysql5.7数据库
环境准备
Linux发行版本:Centos7
Mysql数据库版本:mysql-5.7.24
数据库下载
方法一:官网下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
下载完成后使用ftp工具上传到服务器。
方法二:直接通过wget命令在线下载:
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
下载完成后:
[[email protected] apps]$ ls -lrt
total 629816
-rw-rw-r--. 1 admin admin 644930593 Dec 16 11:24 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
数据库安装
- 解压数据库文件
[[email protected] apps]$ tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[[email protected] apps]$ ls -lrt
total 629816
-rw-rw-r--. 1 admin admin 644930593 Dec 16 11:24 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
drwxrwxr-x. 9 admin admin 129 Dec 16 14:16 mysql-5.7.24-linux-glibc2.12-x86_64
- 重命名
[[email protected] apps]$ mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
[[email protected] apps]$ ls -lrt
total 629816
-rw-rw-r--. 1 admin admin 644930593 Dec 16 11:24 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
drwxrwxr-x. 9 admin admin 129 Dec 16 14:16 mysql
- 复制到/usr/local/目录
[[email protected] apps]$ sudo cp -r mysql /usr/local/
[sudo] password for admin:
- 目录结构如下
[[email protected] apps]$ cd /usr/local/mysql
[[email protected] mysql]$ ls -lrt
total 36
drwxr-xr-x. 2 root root 4096 Dec 16 14:20 bin
drwxr-xr-x. 5 root root 230 Dec 16 14:20 lib
drwxr-xr-x. 3 root root 4096 Dec 16 14:20 include
drwxr-xr-x. 28 root root 4096 Dec 16 14:20 share
drwxr-xr-x. 2 root root 55 Dec 16 14:20 docs
-rw-r--r--. 1 root root 2478 Dec 16 14:20 README
drwxr-xr-x. 4 root root 30 Dec 16 14:20 man
-rw-r--r--. 1 root root 17987 Dec 16 14:20 COPYING
drwxr-xr-x. 2 root root 90 Dec 16 14:20 support-files
- 添加mysql用户组和mysql用户
[[email protected] mysql]$ sudo groupadd mysql # 创建用户组
[sudo] password for admin:
[[email protected] mysql]$ sudo useradd -r -g mysql -s /bin/false mysql # 创建用户
-s /bin/false参数指定mysql用户仅拥有所有权,而没有登陆权限
- 修改mysql目录权限
[[email protected] mysql]$ cd /usr/local/mysql/
[[email protected] mysql]$ sudo chown -R mysql:mysql ./
- 配置Mysql服务
将mysql/support-files下的mysql.server 复制到 /etc/init.d/下并自定义为mysql:
[[email protected] support-files]$ sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
打开/etc/init.d/mysql文件
[[email protected] support-files]$ sudo vim /etc/init.d/mysql
查找:
basedir=
datadir=
修改为:
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
- 配置mysql的配置文件
在其他版本的mysql 里面 support-files下有默认的配置文件,而5.7.24这个版本没有,需要自己准备,下面提供一份简单基本配置
在/etc/ 下新建my.cnf ,有些可能会提示已经存在,因为默认装的数据库配置文件也在,直接改名备份
[[email protected] etc]$ pwd
/etc
[[email protected] etc]$ ls -lrt my*
-rw-r--r--. 1 root root 570 Aug 16 2018 my.cnf
my.cnf.d:
total 4
-rw-r--r--. 1 root root 232 Apr 20 2018 mysql-clients.cnf
[[email protected] etc]$ sudo mv my.cnf my.cnf.bak
新建:
[[email protected]lhost etc]$ sudo vim my.cnf
内容为:
[[email protected] etc]$ sudo vim my.cnf
[[email protected] etc]$ cat my.cnf
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
port = 3306
default-character-set=utf8
[mysqld]
# 一般配置选项
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server=utf8
default_storage_engine = InnoDB
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
- 初始化数据库
新建目录:data
[[email protected] mysql]$ cd /usr/local/mysql
[[email protected] mysql]$ sudo mkdir data
[sudo] password for admin:
[[email protected] mysql]$ sudo chown -R mysql:mysql ./data
执行初始化:
[[email protected] mysql]$ cd /usr/local/mysql/bin
[[email protected] bin]$ sudo ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
如果遇到错误:./mysqld: error while loading shared libraries: libnuma.so.1:
cannot open shared object file: No such file or directory
则执行如下安装命令:yum -y install numactl.x86_64
如果遇到错误:./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
则执行如下安装命令:yum -y install libaio
安装完成后重新执行初始化命令
初始化成功后会生成默认的密码在执行记录中,注意复制出来:
2019-12-16T07:09:20.464295Z 1 [Note] A temporary password is generated for [email protected]: e>evo9Te6Qag
- 启动mysql服务
[[email protected] bin]$ sudo service mysql start
[sudo] password for admin:
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
SUCCESS!
启动成功!
- 登录及修改密码
登录
[[email protected] bin]$ ./mysql -uroot -p # 回车后输入之前复制的默认密码:e>evo9Te6Qag
Enter password:
修改密码:
mysql> alter user 'root'@'localhost' identified by '自定义新密码';
刷新权限:
mysql> flush privileges;
- 开放端口(可选)
开启端口:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
重载防火墙:
firewall-cmd --reload
- 设置开机启动(可选)
查看自启服务列表:
chkconfig --list
添加mysql服务到自启服务:
chkconfig --add mysql
设置自启:
chkconfig mysql on
- 设置环境变量(可选)
为了不用在mysql/bin目录下才能连接mysql,将mysql/bin目录设置到环境变量中
[[email protected] bin]$ sudo vim /etc/profile
添加内容为:
# mysql environment
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
更新环境变量:
[[email protected] bin]$ source /etc/profile
安装完成,完美!
上一篇: mysql5.7解压版安装
下一篇: mysql5.7解压版安装