Linux手动安装mysql 5.7

安装Linux版本的MySQL

前言

安装版本mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz

卸载Centos上自带Mysql

  1. 先看本地有没有安装 rpm -qa |grep -i mysql
1
2
3
4
[root@localhost ~]# rpm -qa |grep -i mysql
mysql-devel-5.1.66-2.el6_3.x86_64
mysql-libs-5.1.66-2.el6_3.x86_64
mysql-5.1.66-2.el6_3.x86_64
  1. 开始卸载

    由于是局域网安装,直接用rpm -e XXXX 会出现error: Failed depend6encies这样的错误提示,这时加上--nodeps的参数。

1
2
3
[root@localhost ~]# rpm -e --nodeps mysql-devel-5.1.66-2.el6_3.x86_64
[root@localhost ~]# rpm -e --nodeps mysql-libs-5.1.66-2.el6_3.x86_64
[root@localhost ~]# rpm -e --nodeps mysql-5.1.66-2.el6_3.x86_64

​ 再执行一下 rpm -qa |grep -i mysql,没有东西列出来,说明已经卸载。

  1. 清除残余的文件
1
2
3
[root@localhost ~]# find / -name mysql
/usr/lib64/mysql
[root@localhost ~]# rm -rf /usr/lib64/mysql
  1. 删除启动文件
1
2
[root@localhost ~]# chkconfig --list | grep -i mysql
[root@localhost ~]# chkconfig --del mysql

安装

  1. 下载Mysql

  2. 解压tar - zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz

    解压后:

1
2
3
[root@localhost tools]# ls
apache-tomcat-8.0.36 jdk1.7.0_79 mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
apache-tomcat-8.0.36_8081 mysql-5.7.28-linux-glibc2.12-x86_64 redis-3.0.7
  1. 移动位置,并在mysql目录下创建database 目录 、data 目录 、data/binlog 二进制日志目录 、 data/relay 主从复制的本地文件存放目录 、data/tmp临时数据存放目录 , 并权限设置为0755
1
2
3
4
5
6
7
8
9
10
11
[root@localhost tools]# mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost local]# cd mysql
[root@localhost mysql]# mkdir database
[root@localhost mysql]# mkdir data
[root@localhost mysql]# mkdir data/relay
[root@localhost mysql]# mkdir data/tmp
[root@localhost mysql]# chmod -R 755 data
[root@localhost mysql]# ls
bin data database docs include lib LICENSE man README share support-files
[root@localhost mysql]# ls data
relay tmp
  1. 创建mysql用户组和用户并授权目录
1
2
3
4
5
6
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -r -g mysql mysql
[root@localhost mysql]# cd ..
[root@localhost mysql]# chgrp -R mysql .
[root@localhost mysql]# chown -R mysql .
[root@localhost local]# chown -R mysql:mysql mysql
  1. 进到myslq目录下安装
1
2
3
4
5
6
7
8
9
[root@localhost local]# cd mysql
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/database
2019-11-17T01:26:52.844368Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-11-17T01:26:53.159085Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-11-17T01:26:53.240493Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-11-17T01:26:53.313258Z 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: 5611788c-08d9-11ea-96df-005056b8be6e.
2019-11-17T01:26:53.317771Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-11-17T01:26:54.551802Z 0 [Warning] CA certificate ca.pem is self signed.
2019-11-17T01:26:54.812906Z 1 [Note] A temporary password is generated for root@localhost: laj3w?hlor:M

注意,最后一行出现的是默认mysql的初始密码laj3w?hlor:M

  1. 创建启动服务
1
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql

配置

  1. 创建my.cnf配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@localhost ~]# vim /etc/my.cnf

[client]

port = 3306
socket = /usr/local/mysql/data/mysql.sock
default-character-set = utf8mb4

[mysqld]

port = 3306
socket = /usr/local/mysql/data/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/database
tmpdir = /usr/local/mysql/data/tmp

character_set_server = utf8mb4
collation_server = utf8mb4_bin
user = mysql

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
  1. 添加环境变量
1
[root@localhost mysql]# vi /etc/profile

添加三行环境变量:

1
2
3
export PATH="$PATH":/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export MYSQL_HOME=/usr/local/mysql
export PATH="$PATH":"$MYSQL_HOME"/bin

source一下使配置生效source /etc/profile

  1. 设置开机启动
1
2
[root@localhost mysql]# chkconfig --add mysql
[root@localhost mysql]# chkconfig --level 2345 mysql on
  1. 启动 mysql
1
2
[root@localhost mysql]# service mysql start
Starting MySQL. [ OK ]
  1. 修改root密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost ~]# mysql -u root -p
Enter password:laj3w?hlor:M ##密码根据实际来
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28

Copyright (c) 2000, 2019, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> set password = password('root'); ##密码也设置成root
Query OK, 0 rows affected, 1 warning (0.00 sec)
  1. mysql默认情况下只能本地访问,需要设置可以远程访问,接着上面继续
1
2
3
4
5
6
7
8
9
10
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 Host = "%" where user = 'root';
##刷新缓存
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

初始化数据库

  1. 创建数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost tools]# mysql -u root -p
Enter password:root ##上面修改过的密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> CREATE DATABASE `dmas` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.01 sec)

mysql> use dmas
Database changed
mysql> source /usr/tools/dump-dmas-201911191055.sql; ##.sql文件是从139这个库导出来的
Query OK, 0 rows affected (0.00 sec)
...... ##此处省略若干执行结果
mysql>

参考资料:centos安装mysql-5.7.24-linux-glibc2.12-x86_64