Centos7二进制安装Mysql5.7及以上版本

时间:2020-12-15 20:58:52

1.创建mysql用户组和用户

groupadd mysql
useradd -r -g mysql -s /sbin/nolog mysql

2.下载mysql相关版本二进制包,并解压移动至/usr/local目录

wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz 
tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz #解压到当前目录
mv /usr/local/src/mysql-5.7.24-linux-glibc2.12-x86_64/ /usr/local/mysql #移动并重命名

3.在Mysql目录创建data目录,并授予相关权限

mkdir -p /usr/local/mysql/data
chown -R mysql.mysql .
chmod -R 755 /usr/local/mysql

4.卸载自带mysql、mariadb数据

rpm -qa | grep mysql
rpm -qa | grep mariadb
rpm -e --nodeps 包名

5.初始化mysql数据库,会生成一个随机密码,记得保存好。警告提示关系不大,可以忽略

[root@oracle mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2018-11-14T09:05:32.752289Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-11-14T09:05:34.248738Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-11-14T09:05:34.787689Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-11-14T09:05:34.941610Z 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: 7238b05b-e7ec-11e8-b680-3417ebcfddea.
2018-11-14T09:05:34.980838Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-11-14T09:05:34.981293Z 1 [Note] A temporary password is generated for root@localhost: Lk5jgsJ2nG#7

6.配置/etc/my.cnf文件,至关重要。数据库能不能正常启动就看这个配置文件了。如果配置不对,会给你搞一堆的错误出来....很是脑阔痛

[root@oracle mysql]# vim /etc/my.cnf
[root@oracle mysql]# cat /etc/my.cnf
[mysqld]
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid

#
# include all files from the config directory
#
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
default_storage_engine = InnoDB
user=mysql
tmpdir=/tmp
[root@oracle mysql]# 

7.启动mysql数据库

ln -vs /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #建立软连接
/etc/init.d/mysqld start

8.登陆数据库

ln -vs /usr/local/mysql/bin/mysql /usr/bin/ #建立软链接,也就是windows说的快捷方式
mysql -uroot -p密码 #用初始化生产的随机密码登陆数据库做相应的授权操作

附加

总结一下二进制安装Mysql5.7数据库所遇到的问题

1.Cetos7操作系统不能解压带gz后缀的包,换了多少个参数都没用。不知道是哪里的问题,暂时未找到解决办法,有遇到的网友可以留言告知下。错误如下所示

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

2.前面也说到配置文件的问题,这个真的是至关重要,就因为配置文件里面有个两个datadir导致一直出现如下错误,如果mysql.sock之前已经存在了请删除在启动Mysql,不然也会报下面的错误

[root@oracle data]# /etc/init.d/mysqld start
Starting MySQL..... ERROR! The server quit without updating PID file (/var/lib/mysql/oracle.pid).

3.登陆数据库之后出现的问题,如下所示

mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

这个问题这个主要是由一个参数控制的 default_password_lifetime,有兴趣的朋友可以去看看官方的解释

我是从一个博客中看到这个的。资料来源

解决方法如下

alter user user() identified by "密码";

4.mysql -uroot -p不能使用,这个是由于没有建立软链接导致;上文有说。

大概就这么多,如有遗漏和不足请大家留言,我会及时更正文档!