一.删除原来安装的mysql

1.查询并删除

[root@1 /]# find / -name mysql
/usr/share/mysql
/usr/bin/mysql
[root@1 /]#  rm -rf /usr/share/mysql /usr/bin/mysql

2.删除配置文件

2.1删除mysql的配置文件(my.cnf文件)

[root@1 /]# rm -rf /etc/my.cnf

2.2删除 /etc/init.d/ 下跟mysql有关的全部文件

mysql5.7.26安装 mysql5.7.31安装_数据库

3. 删除mysql的用户和组

[root@1 /]# userdel mysql
userdel: user 'mysql' does not exis

到这里。mysql已经被删除完成,可以安装新的mysql了

二.安装步骤

1.进入mysql的安装目录

[root@1 /]# cd /usr/local

2.下载安装包两种方式:

方式一:官网下载:https://dev.mysql.com/downloads/mysql/5.7.html#downloads

方式二:通过linux的wget命令,服务器网速可以的话直接wget

[root@1 /]# wget https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

3.下载完后,先找到自己下载的 mysql包

[root@1 local]# ll /usr/local/

然后解压

[root@1 local]# tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

重命名

[root@1 local]# mv mysql-5.7.31-linux-glibc2.12-x86_64 mysql

4.添加用户组mysql和用户mysql,并将其添加到mysql用户组中

[root@1 local]# groupadd mysql
[root@1 local]# useradd -r -g mysql mysql

注: useradd -r参数表示mysql用户是系统用户,不可用于登录系统。 useradd -g参数表示把mysql用户添加到mysql用户组中。

5.检查是否安装了 libaio

[root@1 local]# rpm -qa | grep libaio

如果没有安装,则执行:

[root@1 local]# yum search libaio
[root@1 local]# yum install -y libai

6.配置my.cnf文件

[root@1 local]# vim /etc/my.cnf

将下面内容拷进去

[mysql]  
 
# 设置mysql客户端默认字符集  
 
default-character-set=utf8   
 
socket=/tmp/mysql.sock
 
[mysqld]  
 
#skip-name-resolve  
 
#设置3306端口  
 
port=3306   
 
socket=/tmp/mysql.sock
  
 
# 设置mysql的真正的安装目录 (看自己的是在哪里)
 
basedir=/usr/local/mysql  
 
# 设置mysql数据库的数据的存放目录 (看自己的是在哪里)
 
datadir=/usr/local/mysql/data  
 
# 允许最大连接数  
 
max_connections=200  
 
# 服务端使用的字符集默认为8比特编码的latin1字符雿 
 
character-set-server=utf8  
 
# 创建新表时将使用的默认存储引擿 
 
default-storage-engine=INNODB  
 
#lower_case_table_name=1  

# 非必要
max_allowed_packet=16M  
join_buffer_size = 128M
sort_buffer_size = 16M
read_rnd_buffer_size = 16M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
max_allowed_packet = 500000M

7.创建data文件夹(就是my.cnf中datadir 所指定的文件夹)

[root@1 mysql]# pwd
/usr/local/mysql
[root@1 mysql]# mkdir data

8.将mysql目录的所属用户和组改为mysql

[root@1 mysql]#  chown -R mysql:mysql ./

9.初始化mysqld 生成初始化密码(注意文件地址)

[root@1 mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

输出为

2020-10-24T04:25:13.673365Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-24T04:25:15.808961Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-10-24T04:25:16.105505Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-10-24T04:25:16.184776Z 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: cec94f21-d744-11e8-a0b5-fa163ed8e403.
2020-10-24T04:25:16.188372Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-10-24T04:25:16.189074Z 1 [Note] A temporary password is generated for root@localhost: k;runXw2,2;r
[root@1 mysql]#

将打印信息最后一句的"root@localhost:" 后边的内容记录下来,这是第一次登陆mysql需要的密码(非常重要);

10.mysql配置

10.1 设置开机启动,复制mysql.server脚本到资源目录,并赋予执行权限

[root@1 mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysql.server
[root@1 mysql]# chmod +x /etc/rc.d/init.d/mysql.server

10.2:将 mysqld 服务加入到系统服务并检测是否生效:

[root@1 mysql]#  chkconfig --add mysql.server
[root@1 mysql]# chkconfig --list mysql.server

可以启动mysql了:

[root@1 mysql]#  service mysql.server start

11.配置环境变量

11.1 在 /etc/profile配置文件中,添加如下内容:

[root@1 mysql]#  vim  /etc/profile
#mysql环境变量

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

11.2 执行命令,使etc/profile生效

[root@1 mysql]# source /etc/profile

11.3 校验是否成功:

[root@1 mysql]# echo $PATH
.:/usr/local/zk/bin:.:/usr/local/zk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

12.初次登录 修改访问密码(第一次的密码就是 上边我让你记得密码,可以回去看下第9步)

[root@baidu64 mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.31
 
Copyright (c) 2017, 2020, 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>

修改登录密码:(为 ‘123456’)

mysql> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
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 authentication_string=PASSWORD('123456') where User='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1
 
mysql>

13.允许远程访问

mysql> grant all privileges on *.* to root@"%" identified by "password" with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql>

14.使用navicate远程连接报错
14.1关闭mysql服务

[root@1 mysql]# service mysql.server stop

 Redirecting to /bin/systemctl stop mysql.service

如果提示‘mysql unrecognized service’,则执行:

[root@1 mysql]#  cp ./support-files/mysql.server /etc/rc.d/init.d/mysql.server

然后执行:

[root@1 mysql]#   chmod +x /etc/rc.d/init.d/mysql.server

在执行:

[root@1 mysql]# service  mysql.server start

15.修改/etc/my.cnf修改为无密码登录

[root@1 mysql]# vim /etc/my.cnf

在my.cnf配置文件添加如下内容:

# mysql无密码登录
skip-grant-tables

16.重启mysql服务

[root@1 mysql]# service mysql.server restart 
Redirecting to /bin/systemctl restart mysql.service

17.无密码登录mysql

[root@1 mysql]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 MySQL Community Server (GPL)
 
Copyright (c) 2017, 2020, 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>

18.再次修改mysql密码:

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 mysql.user set authentication_string=password('123456') where user='root' ;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 2  Changed: 1  Warnings: 1
 
mysql>

19.然后将my.cnf无密码登录配置去掉(就是上面刚加的那句话)

20.退出mysql,并重启:

mysql> quit
Bye
[root@1 mysql]# service mysql restart
Redirecting to /bin/systemctl restart mysql.service
[root@1 mysql]#

21.重新连接

mysql5.7.26安装 mysql5.7.31安装_linux_02