1.从mysql官网下载二进制安装包([https://dev.mysql.com/downloads/mysql/])

1-mysql.jpg

2.检查是否已经安装了mysql或者是MariaDB,如果已经安装,则先把安装的卸载

[root@localhost software]# rpm -qa | grep mysql
[root@localhost software]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@localhost software]# yum -y remove mariadb-libs-5.5.56-2.el7.x86_64
3.通过winSCP或者是Xftp等工具将安装包上传到CentOS上,并解压
[root@localhost software]# ll
总用量 629816
drwxr-xr-x. 9 root root       129 12月 25 16:49 mysql-5.7.24-linux-glibc2.12-x86_64
-rw-r--r--. 1 root root 644930593 12月 24 17:15 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@localhost software]# 
4.重命名mysql安装目录
[root@localhost software]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
[root@localhost software]# ll
总用量 629816
drwxr-xr-x. 9 root root       129 12月 25 16:49 mysql
-rw-r--r--. 1 root root 644930593 12月 24 17:15 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@localhost software]# 
5.添加新的用户组和新的用户,用来管理mysql,提高安全性(非必要的,不过mysql官网安装步骤推荐这样做,这个步骤可以省略)
[root@localhost software]# groupadd mysql
[root@localhost software]# useradd -r -g mysql -s /bin/false mysql
[root@localhost software]# 
6.新建mysql的data目录
[root@localhost software]# cd mysql
[root@localhost mysql]# mkdir data
[root@localhost mysql]# ll
总用量 36
drwxr-xr-x.  2 root root   4096 12月 25 16:49 bin
-rw-r--r--.  1 7161 31415 17987 10月  4 13:48 COPYING
drwxr-xr-x.  2 root root      6 12月 25 16:57 data
drwxr-xr-x.  2 root root     55 12月 25 16:49 docs
drwxr-xr-x.  3 root root   4096 12月 25 16:48 include
drwxr-xr-x.  5 root root    230 12月 25 16:49 lib
drwxr-xr-x.  4 root root     30 12月 25 16:49 man
-rw-r--r--.  1 7161 31415  2478 10月  4 13:48 README
drwxr-xr-x. 28 root root   4096 12月 25 16:49 share
drwxr-xr-x.  2 root root     90 12月 25 16:49 support-files
[root@localhost mysql]#
7.修改mysql目录用户为刚刚新建的mysql组中的mysql用户
[root@localhost mysql]# chown -R mysql:mysql ./
8.初始化安装mysql数据库
[root@localhost mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/software/mysql --datadir=/usr/local/software/mysql/data --initialize
2018-12-25T09:00:17.116437Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-12-25T09:00:21.961838Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-12-25T09:00:22.747386Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-12-25T09:00:22.860663Z 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: 8324a8be-0823-11e9-b13d-000c29ca91c6.
2018-12-25T09:00:22.862806Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-12-25T09:00:22.867377Z 1 [Note] A temporary password is generated for root@localhost: Q&ZsMY#sV3du
[root@localhost mysql]#
9.修改my.cnf配置文件,通过mysql官网可以知道,从版本5.7.18开始,mysql免安装版二进制包中就不包含该文件了,即不需要my.cnf文件也可以正常运行;my.cnf文件中配置的选项会在命令行启动mysql的时候作为参数进行启动,为了后面搭建mysql主从环境方便,下面可以添加了一个简单的my.cnf文件作为实例(如果只是单纯的搭建一个mysql实例,可以直接忽略此步骤),使用vim /etc/my.cnf命令,如果在该目录上不存在则会新建
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/software/mysql
datadir=/usr/local/software/mysql/data
socket=/usr/local/software/mysql/mysql.sock
#设置忽略大小写(简单来说就是sql语句是否严格),默认库名表名保存为小写, 不区分大小写
lower_case_table_names = 1
# 开启ip绑定
bind-address = 0.0.0.0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/software/mysql/data/mysqld.pid
#指定客户端连接mysql时的socket通信文件路径
[client]
socket=/usr/local/software/mysql/mysql.sock
default-character-set=utf8
10.将mysql添加至开机启动
[root@localhost mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# 

修改mysqld,使用vim /etc/init.d/mysqld 命令 修改以下代码部分

2-mysql.png

basedir=/usr/local/software/mysql
datadir=/usr/local/software/mysql/data

设置开机启动

[root@localhost mysql]# chkconfig --add mysqld
11.到这一步,mysql已经算是安装完毕了,接下来使用以下命令启动mysql
[root@localhost mysql]# service mysqld start
Starting MySQL.Logging to '/usr/local/software/mysql/data/localhost.localdomain.err'.
. SUCCESS! 
[root@localhost mysql]#
12.为了可以在任意目录上都可以使用mysql命令登录mysql,将mysql安装目录配置到环境变量中,在/etc/profile文件的末尾添加以下代码

3-mysql.png

export PATH=$PATH:/usr/local/software/mysql/bin

使配置文件的配置立即生效

[root@localhost mysql]# source /etc/profile
13.重启mysql服务,并且使用mysql的root用户登录mysql,密码在第8步最后一行有
[root@localhost mysql]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@localhost 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

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> 
14.修改root用户的密码为root,并且刷新
mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
15.此时mysql数据库只能在本机上使用mysql命令进行登录,还无法使用navicat等数据库可视化工具进行远程登录,下面设置允许root用户远程连接数据库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set user.Host='%' where user.User='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@localhost mysql]# 

为了方便试验,此处已事先使用 systemctl stop firewalld 命令将防火墙关闭,在实际使用中,只需要开放数据库运行的3306端口即可,结果如下:
4-mysql.png

文章目录