首页 热点专区 小学知识 中学知识 出国留学 考研考公
您的当前位置:首页正文

centos7下安装mysql-5.7.20.md

2024-12-07 来源:要发发知识网

Linux(CentOS7)下安装和配置MySQL5.7.20详细教程,亲测

1.在官网下载MySQL5.7安装包:mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz。

image.png

2.卸载系统自带的Mariadb数据库

[adolph@localhost ~]$ rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@localhost adolph]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
 已另存为 
[root@localhost adolph]# rpm -qa|grep mariadb
[root@localhost adolph]# 
  1. 将mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz上传至centos系统,解压
目前我的电脑目录是 /usr/software/,解压之后修改文件夹名为 mysql-5.7.20
[root@localhost mysql-5.7.20]# cd /usr/software/mysql-5.7.20/
[root@localhost mysql-5.7.20]# tar xzvf ./mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

4.root用户创建mysql用户组和mysql用户

[adolph@localhost ~]$ sudo su root
[sudo] adolph 的密码:
[root@localhost adolph]# groupadd mysql
[root@localhost adolph]# useradd -g mysql mysql

5.修改权限

[root@localhost mysql-5.7.20]# cd /usr/software/mysql-5.7.20/
[root@localhost mysql-5.7.20]# chown -R mysql .
[root@localhost mysql-5.7.20]# chgrp -R mysql .
  1. 创建data目录,作为数据库存储位置
[root@localhost mysql-5.7.20]# cd /usr/software/mysql-5.7.20/
[root@localhost mysql-5.7.20]# mkdir data
  1. 初始化mysql
[root@localhost mysql-5.7.20]# cd /usr/software/mysql-5.7.20/bin
[root@localhost bin]# ./mysqld --initialize --user=mysql --basedir=/usr/software/mysql-5.7.20 --datadir=/usr/software/mysql-5.7.20/data
2017-11-27T07:17:00.136662Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-11-27T07:17:00.324658Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-11-27T07:17:00.360551Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-11-27T07:17:00.424250Z 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: f5dc38c3-d342-11e7-8d67-000c29e14481.
2017-11-27T07:17:00.427610Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-11-27T07:17:00.431421Z 1 [Note] A temporary password is generated for root@localhost: ···········
  1. 启动MySQL,并查看是否已启动成功
[root@localhost mysql-5.7.20]# cd /usr/software/mysql-5.7.20/bin
[root@localhost bin]# ./mysqld_safe --user=mysql &
[1] 33292
[root@localhost bin]# Logging to '/usr/software/mysql-5.7.20/data/localhost.localdomain.err'.
2017-11-27T07:21:52.640665Z mysqld_safe Starting mysqld daemon with databases from /usr/software/mysql-5.7.20/data
2017-11-27T07:27:33.251879Z mysqld_safe mysqld from pid file /usr/software/mysql-5.7.20/data/localhost.localdomain.pid ended

[root@localhost data]# ps aux|grep mysqld
root      33292  0.0  0.0 113264  1640 pts/1    S    15:21  0:00 /bin/sh ./mysqld_safe --user=mysql
mysql    33362  0.2  9.4 1127320 176164 pts/1  Sl  15:21  0:00 /usr/software/mysql-5.7.20/bin/mysqld --basedir=/usr/software/mysql-5.7.20 --datadir=/usr/software/mysql-5.7.20/data --plugin-dir=/usr/software/mysql-5.7.20/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid
root      33576  0.0  0.0 112676  984 pts/2    R+  15:24  0:00 grep --color=auto mysqld
  1. 通过初始密码登录MySQL,并修改密码
[root@localhost data]# /usr/software/mysql-5.7.20/bin/mysqladmin -uroot -p password
Enter password: 
New password: 
Confirm new password: 
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

10.关闭MySQL服务,并查看是否关闭成功

[root@localhost mysql-5.7.20]# cd /usr/software/mysql-5.7.20/bin
[root@localhost bin]# ./mysqladmin -u root -p shutdown
Enter password: 
[root@localhost bin]# ps aux|grep mysqld
root      33612  0.0  0.0 112676  984 pts/2    R+  15:27  0:00 grep --color=auto mysqld
[root@localhost bin]# ps aux|grep mysqld
root      33622  0.0  0.0 112676  984 pts/2    S+  15:28  0:00 gre
  1. 设置开机自启
[root@localhost mysql-5.7.20]# /usr/software/mysql-5.7.20/bin
[root@localhost bin]# cp /usr/software/mysql-5.7.20/support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost bin]# chmod +x /etc/rc.d/init.d/mysqld
[root@localhost bin]# chkconfig --add mysqld
[root@localhost bin]# chkconfig --list mysqld

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关

以后可以使用service命令控制mysql的启动和停止,命令为:service mysqld start和service mysqld stop,有可能执行的时候会报错,如下:

[root@localhost init.d]# service mysqld start
/etc/init.d/mysqld: line 239: my_print_defaults: command not found
/etc/init.d/mysqld: line 259: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

这是因为mysql默认安装在/usr/local目录下,如果安装在/usr/local目录下则会正常启动和关闭,不会报错。但是本次安装在自定义的/data目录下,此时需要修改/etc/init.d/mysqld文件,保存后退出

basedir=/usr/software/mysql-5.7.20
datadir=/usr/software/mysql-5.7.20/data

然后在执行service mysqld start启动命令,OK

[root@localhost init.d]# service mysqld start
Starting MySQL. SUCCESS! 
[root@localhost init.d]# service mysqld stop
Shutting down MySQL.. SUCCESS! 

12.配置全局环境变量

编辑/etc/profile文件
在profile文件底部添加如下两行配置,保存后退出
export MYSQL_HOME=/usr/software/mysql-5.7.20
export PATH=$PATH:$MYSQL_HOME/bin:/$MYSQL_HOME/lib
设置环境变量立即生效
source /etc/profile
--------------------------------------------------------------------------------------
[root@localhost init.d]# vi /etc/profile
……
export MYSQL_HOME=/usr/software/mysql-5.7.20
export PATH=$PATH:$MYSQL_HOME/bin:/$MYSQL_HOME/lib
……
[root@localhost init.d]# source /etc/profile

13.设置远程主机登录
远程登录失败


image.png

mysql> GRANT ALL PRIVILEGES ON . TO 'mysql'@'%' IDENTIFIED BY 'adolph' WITH GRANT OPTION;

其中,mysql是用户名,adolph是密码

[root@localhost init.d]# service mysqld start
Starting MySQL. SUCCESS! 
[root@localhost init.d]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY 'adolph' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

再次远程登录,成功


image.png
显示全文