MySQL5.7安装--二进制方式安装
操作系统版本:redhat 6.7 64位
[root@mysql ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.7 (Santiago)
数据库版本:MySQL5.7
下载地址:https://downloads.mysql.com/archives/community/
我下载的包为:mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
(一)安装前的准备
(1)查看服务器上是否已经安装了MySQL。RedHat Linux系统安装时,如果选择了MySQL数据库,那么服务器上会存在MySQL数据库,需要先卸载。
# 查看是否存在mysql安装包 [root@mysql etc]# rpm -qa| grep mysql mysql-community-common-5.7.21-1.el7.x86_64 # 如果存在,需要先卸载 [root@mysql etc]# rpm -e mysql-community-common-5.7.21-1.el7.x86_64
(2)查看服务器上是否存在mariadb数据库。mariadb是MySQL的一个分支,需要卸载
[root@mysql mysql]# rpm -qa |grep mariadb
(3) 删除/etc/my.cnf文件。该文件类似Oracle的参数文件
[root@mysql mysql]# rm /etc/my.cnf rm: cannot remove `/etc/my.cnf': No such file or directory
(4)依赖包安装
MySQL对libaio 库有依赖性。如果未在本地安装该库,则数据目录初始化和随后的服务器启动步骤将失败
# search for info [root@mysql mysql]# yum search libaio # install library [root@mysql mysql]# yum install libaio
对于MySQL 5.7.19和更高版本:通用Linux版本中增加了对非统一内存访问(NUMA)的支持,该版本现在对libnuma库具有依赖性 。
# search for info [root@mysql mysql]# yum search libnuma # install library [root@mysql mysql]# yum install libnuma
(5)创建mysql用户来管理数据库
[root@mysql mysql]# cat /etc/passwd|grep mysql mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash [root@mysql mysql]# cat /etc/group |grep mysql mysql:x:27:
如果没有,则创建用户组和用户
[root@mysql mysql]# groupadd mysql
[root@mysql mysql]# useradd -g mysql mysql
修改mysql用户密码:
[root@mysql mysql]# passwd mysql Changing password for user mysql. New password: BAD PASSWORD: it is too short BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully.
(二)安装MySQL数据库
mysql安装路径:/usr/local/mysql/
数据存放位置::/usr/local/mysql/data
(1)解压MySQL安装包
[root@mysql mysql]# tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
(2)将解压后的文件拷贝到安装路径下
[root@mysql mysql]# mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql/
(3)修改文件的权限
[root@mysql mysql]# cd /usr/local/ [root@mysql local]# ls bin etc games include lib lib64 libexec mysql sbin share src [root@mysql local]# chown -R mysql mysql/ [root@mysql local]# chgrp -R mysql mysql/
(4)创建存放data的文件夹
[root@mysql mysql]# mkdir data [root@mysql mysql]# chown -R mysql:mysql data
(5)在/etc下创建mysql的参数文件my.cnf
[root@mysql etc]# vim my.cnf
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [mysqld] skip-name-resolve # 设置3306端口 port = 3306 # 设置mysql的安装目录 basedir=/usr/local/mysql # 设置mysql数据库的数据的存放目录 datadir=/usr/local/mysql/data # 允许最大连接数 max_connections=200 # 服务端使用的字符集默认为8比特编码的latin1字符集 character-set-server=utf8 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB lower_case_table_names=1 max_allowed_packet=16M
(6)安装数据库
[root@mysql bin]# pwd /usr/local/mysql/bin [root@mysql bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data 2019-04-20 22:17:08 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2019-04-20 22:17:14 [WARNING] The bootstrap log isn't empty: 2019-04-20 22:17:14 [WARNING] 2019-04-20T14:17:08.662276Z 0 [Warning] --bootstrap is deprecated. Please consider using 2019-04-20T14:17:08.663008Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000) 2019-04-20T14:17:08.663018Z 0 [Warning] Changed limits: table_open_cache: 407 (requested 2000)
2020年3月28日补充:
在MySQL5.7中,使用“mysql_install_db”脚本来初始化数据库已经不被推荐,建议使用如下方式:
/usr/local/mysql/bin/mysqld --defaults-file=/mysql/3306/my.cnf --initialize --basedir=/usr/local/mysql/ --datadir=/mysql/3306/data
(6)设置启动项
[root@mysql mysql]# ls bin COPYING data docs include lib man README share support-files [root@mysql mysql]# cd ./support-files/ mysqld_multi.server mysql-log-rotate mysql.server [root@mysql mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld [root@mysql mysql]# chown 777 /etc/my.cnf [root@mysql mysql]# chmod a+x /etc/init.d/mysqld
(7)开启数据库
[root@mysql mysql]# /etc/init.d/mysqld start
Starting MySQL. [ OK ]
查看数据库状态
[root@mysql mysql]# service mysqld status MySQL running (39674) [ OK ] # 或者 [root@mysql mysql]# ps -ef|grep mysql mysql 39674 1 0 22:53 pts/0 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=mysql.err --pid-file=/usr/local/mysql/data/mysql.pid --port=3306 root 40719 3339 0 23:13 pts/0 00:00:00 grep mysql
(8)修改数据库的初始root密码
(8.1)查看初始密码
[root@mysql mysql]# cat /root/.mysql_secret # Password set for user 'root@localhost' at 2019-04-20 22:17:08 bkro9k_?=jrM
(8.2)使用初始密码登录数据库,修改初始密码为123456
[root@mysql mysql]# 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.24 mysql> set password=password('123456'); Query OK, 0 rows affected, 1 warning (0.00 sec)
(9)确认数据库状态,运行正常
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
(10)环境变量设置
在安装之后,如果我们想要调用到MySQL安装基本目录下面的MySQL工具,还需设定环境变量
[root@mysql mysql]# cat /etc/profile export PATH=/usr/local/mysql/bin:$PATH #使环境变量生效 [root@mysql mysql]# source /etc/profile
安装完成。