操作系统版本: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 mysqlmysql-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.cnfrm: cannot remove `/etc/my.cnf‘: No such file or directory
(4)创建mysql用户来管理数据库
[root@mysql mysql]# cat /etc/passwd|grep mysqlmysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash[root@mysql mysql]# cat /etc/group |grep mysqlmysql:x:27:
如果没有,则创建用户组和用户
[root@mysql mysql]# groupadd mysql[root@mysql mysql]# useradd -g mysql mysql
修改mysql用户密码:
[root@mysql mysql]# passwd mysqlChanging password for user mysql.New password: BAD PASSWORD: it is too shortBAD PASSWORD: is too simpleRetype 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]# lsbin 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=1max_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/data2019-04-20 22:17:08 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize2019-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)
(6)设置启动项[root@mysql mysql]# lsbin COPYING data docs include lib man README share support-files[root@mysql mysql]# cd ./support-files/mysqlmysqld_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 startStarting MySQL. [ OK ]
查看数据库状态[root@mysql mysql]# service mysqld statusMySQL running (39674) [ OK ]# 或者[root@mysql mysql]# ps -ef|grep mysqlmysql 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=3306root 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 2Server version: 5.7.24mysql> 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)安装完成。