欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS7安装MySql5.7.24

程序员文章站 2022-05-27 13:35:18
...

序言

1.Linux操作系统为CentOS7;
2.数据库为MySql5.7.24,安装包:mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz。

安装MySQL

1、检查是否已安装过mariadb,若有便删除(linux系统自带的)

rpm -qa | grep mariadb
rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

2、检查是否已安装过mysql,若有便删除(linux系统自带的)

rpm -qa | grep mysql
rpm -e –-nodeps mysql-libs-5.1.52.x86_64

3、检查mysql组和用户是否存在,如无创建:

cat /etc/group | grep mysql
cat /etc/passwd |grep mysql
groupadd mysql
useradd -r -g mysql mysql

4、从官网下载mysql安装包到/home,解压后移动到/usr/local/mysql下

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
tar xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql

5、在mysql下添加data目录

mkdir /usr/local/mysql/data

6、更改mysql目录下所有的目录及文件夹所属组合用户

cd /usr/local/
chown -R mysql:mysql mysql/
chmod -R 755 mysql/

7、编译安装并初始化mysql,记住命令行末尾的密码:

[[email protected] local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
2020-08-10T14:05:35.314635Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-10T14:05:36.324813Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-10T14:05:36.442283Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-10T14:05:36.505036Z 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: 904c875e-db12-11ea-b42b-00163e0a6b12.
2020-08-10T14:05:36.507017Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-08-10T14:05:36.507423Z 1 [Note] A temporary password is generated for [email protected]: kFp.WQ7uSt3R

注:若编译安装并初始化mysql报错,如:

[[email protected] local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

解决方案:执行如下命令,安装后重新执行初始化命令即可

yum install -y libaio

8、启动mysql服务

/usr/local/mysql/support-files/mysql.server start

9、做个软连接,重启服务

ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
service mysql restart

10、做个软链接,将安装目录下的mysql 放在/usr/bin 目录下

ln -s /usr/local/mysql/bin/mysql /usr/bin

11、登录msyql,输入密码(密码为步骤7初始化生成的密码)

[[email protected] local]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24

Copyright (c) 2000, 2018, 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.

mysql>

12、修改密码并开放远程

msql>alter user 'root'@'localhost' identified by '123456';
mysql>use mysql;
msyql>update user set user.Host='%' where user.User='root';
mysql>flush privileges;
mysql>quit

13、编辑my.cnf,添加配置文件,配置内容为

[[email protected] local]# vi /usr/local/mysql/my.cnf
[mysqld]
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

14、设置开机自启动

1、将服务文件拷贝到init.d下,并重命名为mysql
[[email protected] local]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
2、赋予可执行权限
[[email protected] local]# chmod +x /etc/init.d/mysqld
3、添加服务
[[email protected] local]# chkconfig --add mysqld
4、显示服务列表
[[email protected] local]# chkconfig --list
5、重启服务器
[[email protected] local]# reboot