首先在windows系统下载mysql 5.7安装包,官网:https://downloads.mysql.com/archives/community/
安装包下载好之后用Xftp上传到Linux的目录 /usr/local
[root@localhost local]# rpm -qa|grep mariadb //查询出已安装的mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@localhost local]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64 //卸载 , 文件名为使用rpm -qa|grep mariadb 命令查出的所有文件
删除etc目录下的my.cnf文件
[root@localhost local]# rm /etc/my.cnf
添加mysql组和mysql用户,用于设置mysql安装目录文件所有者和所属组。
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -r -g mysql mysql
useradd -r 参数表示mysql用户是系统用户,不可用于登录系统。
useradd -g 参数表示把mysql用户添加到mysql用户组中。
[root@localhost ~]# cd /usr/local //进入目录
[root@localhost local]# tar -zxvf mysql-5.7.32-el7-x86_64.tar.gz //解压
[root@localhost local]# mv mysql-5.7.32-el7-x86_64 mysql
进入mysql文件夹,更改mysql目录所属的组和用户,更改权限。
[root@localhost local]# cd mysql
[root@localhost mysql]# chown -R mysql .
[root@localhost mysql]# chgrp -R mysql .
[mysqld]
skip-name-resolve
socket=/var/lib/mysql/mysql.sock
#设置3306端口
port=3306
# 设置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_names=1
max_allowed_packet=16M
[client]
socket=/var/lib/mysql/mysql.sock
# 设置mysql客户端默认字符集
default-character-set=utf8
#设置3306端口
port=3306
为了方便,可在windows下建立文本文件,并复制以上代码进去,然后重命名为my.cnf 再用Xftp上传到 /ect 目录下。
创建用到的目录并将其用户设置为mysql
[root@localhost mysql]# mkdir /usr/local/mysql/data
[root@localhost mysql]# mkdir /var/lib/mysql
[root@localhost mysql]# mkdir /var/lib/mysql/mysql
[root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql/data
[root@localhost mysql]# chown -R mysql:mysql /var/lib/mysql
[root@localhost mysql]# chown -R mysql:mysql /var/lib/mysql/mysql
注意:
mysql5.7和之前版本不同,很多资料上都是这个命令:…/scripts/mysql_install_db --user=mysql,
而mysql5.7的mysql_install_db命令是在bin目录下 的并且建议 用 mysqld --initialize命令
[root@localhost mysql]# cd /usr/local/mysql
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2021-04-13T08:40:19.825494Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-04-13T08:40:20.252337Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-04-13T08:40:20.301578Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-04-13T08:40:20.395583Z 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: e16909ae-9c33-11eb-aac5-000c29e4cc3a.
2021-04-13T08:40:20.405435Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-04-13T08:40:21.239588Z 0 [Warning] CA certificate ca.pem is self signed.
2021-04-13T08:40:21.287945Z 1 [Note] A temporary password is generated for root@localhost: f;CW0?#OwS7b
此处要记下最后一行的初始密码,后面登录mysql要用到
root@localhost: f;CW0?#OwS7b
安装完成后,将mysql/目录下除了data/目录的所有文件,改回root用户所有,mysql用户只需作为mysql/data/目录下所有文件的所有者。
[root@localhost mysql]# chown -R root .
[root@localhost mysql]# chown -R mysql data
将 /usr/local/mysql/support-files/mysql.server 拷贝为 /etc/init.d/mysql 并设置运行权限,这样就可以使用service mysql命令启动/停止服务
[root@localhost mysql]# cd support-files
[root@localhost support-files]# cp mysql.server /etc/init.d/mysql
[root@localhost support-files]# chmod +x /etc/rc.d/init.d/mysql
把mysql注册为开机启动的服务
[root@localhost support-files]# chkconfig --add mysql
查看是否添加成功
[root@localhost mysql]# chkconfig --list mysql
mysql 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@localhost mysql]# service mysql start //开启服务
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
.. SUCCESS!
[root@localhost mysql]# service mysql stop //关闭服务
Shutting down MySQL.. SUCCESS!
首先建立软链接:
[root@localhost mysql]# ln -s /usr/local/mysql/bin/mysql /usr/local/bin
[root@localhost mysql]# ln -s /usr/local/mysql/bin/mysqladmin /usr/local/bin
[root@localhost mysql]# ln -s /usr/local/mysql/bin/mysqld_safe /usr/local/bin
登录mysql ,需要输入之前得到的初始密码然后按回车键,输入密码的时候光标是没有变化的,不要怀疑键盘坏了。
[root@localhost /]# mysql -uroot -p
Enter password:
密码正确的话会出现以下画面
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.32
Copyright (c) 2000, 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>
此时操作mysql 会提示必须修改密码才能使用数据库
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改成强密码,要求有大小写字母,符号,数字组合8位数以上,我这里的密码是:
Data-20210413
mysql> alter user 'root'@'localhost' identified by 'Data-20210413';
Query OK, 0 rows affected (0.20 sec)
到此,mysql安装完成,接下来是配置数据库。
Data-20210413是root用户的密码,以自己的密码为准
mysql> GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTIFIED BY 'Data-20210413' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES ; //更新权限
Query OK, 0 rows affected (0.04 sec)
开启端口才能远程连接上数据库,要注意的是先退出mysql,进入linux环境下运行命令。
mysql> exit; //退出mysql
[root@localhost /]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
[root@localhost /]# firewall-cmd --reload //重启防火墙,上面开启的端口才生效
[root@localhost /]# systemctl stop firewalld.service //关闭防火墙
[root@localhost /]# systemctl start firewalld.service //开启防火墙