linux之mysql5.7环境安装与配置
注意:建议安装之前,先卸载一遍,减少失败报错的几率!
1. tar.gz
1.1. 安装
查看是否安装mysql和mariadb(mysql的一种,具体区别自行百度)
# 查看是否安装 mysql 和 mariadb,显示有就卸载
[root@localhost]# rpm -qa | grep mysql
[root@localhost]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
# 卸载掉 mariadb
[root@localhost]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
在官网下载mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
使用xftp将下载好的mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz,上传的linux中的/usr/local下
在 /usr/local 下进行解压
# 解压 mysql,压缩包有点大,等待解压
[root@localhost local]# tar -zxvf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
# 重命名 mysql
[root@localhost local]# mv mysql-5.7.33-linux-glibc2.12-x86_64 mysql
# 创建一个mysql的用户,也可以使用系统root用户,说明 数据库被攻击时,不会拿到root的权限,去打乱你的linux系统
# 建议:跟着小编来操作,主要后面我会使用mysql的用户 创建错误 删除用户命令 userdel 用户名
[root@localhost local]# useradd -r -s /sbin/nologin mysql
# 查看mysql用户
[root@localhost local]# id mysql
uid=997(mysql) gid=995(mysql) groups=995(mysql)
# 进入到mysql文件夹中
[root@localhost local]# cd mysql
# 创建一个 data 文件夹,用于数据库的存放
[root@localhost mysql]# mkdir data
# 修改 data 拥有者和权限
[root@localhost mysql]# chown mysql:mysql data
[root@localhost mysql]# chmod 750 data
# 初始化mysql,得到mysql的初始化密码
[root@localhost mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql
# 设置安全加密连接(SSL),数据传输采用加密形式,适用于敏感数据
[root@localhost mysql]# ./bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
# 修改mysql文件
[root@localhost mysql]# vim support-files/mysql.server
# 拷贝脚本
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql
# 启动mysql服务
[root@localhost mysql]# service mysql start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
SUCCESS!
# 登录mysql,输入初始化密码
[root@localhost mysql]# mysql -uroot -p
Enter password:
错误
# 登录mysql,遇到以下情况
[root@localhost mysql]# mysql -u root -p
bash: mysql: command not found...
# 一 在之前输入 ./bin
[root@localhost mysql]# ./bin/mysql -u root -p
# 二 直接进入到 bin 目录里敲命令
[root@localhost mysql]# cd bin
[root@localhost bin]# mysql -u root -p
# 三 配置环境变量,在最后配置
[root@localhost mysql]# vim /etc/profile
# 配置内容 若PATH有多个配置使用 : 划分
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:${MYSQL_HOME}/bin
# 执行以下命令刷新并让配置文件生效
[root@localhost usr]# source /etc/profile
配置内容展示
# 设置mysql密码
mysql> set password = password('123456');
# 刷新设置
mysql> flush privileges;
设置 mysql 编码
# 先查看 mysql 的默认编码
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
# 退出 mysql
mysql> exit
Bye
# 设置 mysql 的编码为utf8
[root@localhost mysql]# vim /etc/my.cnf
将以下内容覆盖原内容 注意:复制粘贴时,第一个 # 会丢失,记得手动补上
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
重启
# 重启 mysql 服务
[root@localhost mysql]# service mysql restart
# 登录 mysql 并查看编码是否生效
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
远程连接
# 开启远程连接,在其他电脑中使用 sqlyog 进行连接访问
mysql> grant all privileges on *.* to root@'%' identified by '123456';
# 刷新设置
mysql> flush privileges;
防火墙
# 关闭防火墙,否则sqlyog连接不上 报 2002 错误
[root@localhost mysql]# systemctl stop firewalld.service
# 禁用防火墙自启动,重启服务器时,无需在关闭防火墙
[root@localhost mysql]# systemctl disable firewalld.service
自启动
# 设置 mysql 自启动
[root@localhost mysql]# chkconfig --add mysql
安装完毕!
1.2. 卸载
# 查看是否安装了mysql
[root@localhost /]# rpm -qa | grep mysql
[root@localhost /]# find / -name mysql
/run/lock/subsys/mysql
/etc/rc.d/init.d/mysql
/usr/local/mysql
/usr/local/mysql/bin/mysql
/usr/local/mysql/include/mysql
/usr/local/mysql/data/mysql
# 关掉mysql服务
[root@localhost /]# service mysql stop
# 一一删除即可
[root@localhost /]# rm -rf /run/lock/subsys/mysql
[root@localhost /]# rm -rf /etc/rc.d/init.d/mysql
[root@localhost /]# rm -rf /usr/local/mysql
卸载完毕!
2. rpm
2.1. 安装
进入官网,找到mysql-5.7.33-1.el7.x86_64.rpm-bundle.tar并下载
上传到 linux 系统中的 /usr/local 下
# 运行命令解压
[root@localhost local]# tar -xvf mysql-5.7.33-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-5.7.33-1.el7.x86_64.rpm
mysql-community-common-5.7.33-1.el7.x86_64.rpm
mysql-community-devel-5.7.33-1.el7.x86_64.rpm
mysql-community-embedded-5.7.33-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.33-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.33-1.el7.x86_64.rpm
mysql-community-libs-5.7.33-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.33-1.el7.x86_64.rpm
mysql-community-server-5.7.33-1.el7.x86_64.rpm
mysql-community-test-5.7.33-1.el7.x86_64.rpm
# 按照顺序执行 rpm 安装命令,以下四个rpm包即可
[root@localhost local]# rpm -ivh mysql-community-common-5.7.33-1.el7.x86_64.rpm
[root@localhost local]# rpm -ivh mysql-community-libs-5.7.33-1.el7.x86_64.rpm
[root@localhost local]# rpm -ivh mysql-community-client-5.7.33-1.el7.x86_64.rpm
[root@localhost local]# rpm -ivh mysql-community-server-5.7.33-1.el7.x86_64.rpm
# 开启 mysql 服务
[root@localhost local]# service mysqld start
# 查看 mysql 服务状态
[root@localhost local]# service mysqld status
# 查看初始密码,在开启 mysql 服务时,也就是执行 service mysqld start 命令 ,就生成了初始化密码
[root@localhost local]# cat /var/log/mysqld.log | more
# 显示 mysql 日志,按 enter 下一行,找到 root@localhost 后面就是密码
密码
# 使用初始化密码登录 mysql,复制初始化密码,按住 shift + insert 键,复制进去,回车登录
[root@localhost local]# mysql -uroot -p
Enter password:
# 登录成功后,必须先修改密码,才能执行其他配置操作,由于mysql 5.7版本系列,有默认的密码规则,一定要符合对应的字符信息
# 这里的密码修改,就按照小编这里的一致,特殊字符,大小写,数字等
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '@Ch123456';
# 一定是修改初始化密码后!查看密码规则,并修改密码规则
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
# LOW:0 | MEDIUM:1 | STRONG:2
# 0/LOW:只检查长度
# 1/MEDIUM:检查长度、数字、大小写、特殊字符
# 2/STRONG:检查长度、数字、大小写、特殊字符字典文件
# 设置 validate_password_policy 为 MEDIUM 修改为 LOW 表示密码强度等级
mysql> set global validate_password_policy=LOW;
# 设置 validate_password_length 为 8 修改为 6 表示密码长度
mysql> set global validate_password_length=6;
# 最终修改密码,建议密码设置成123456
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
编码
# 查看mysql编码
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
# 退出mysql登录,查找配置文件
[root@localhost mysql]# find / -iname '*.cnf' -print
/etc/pki/tls/openssl.cnf
/etc/my.cnf
/var/lib/mysql/auto.cnf
# 修改配置文件
[root@localhost mysql]# vim /etc/my.cnf
# 在 [mysqld] 下,添加以下两段代码
collation_server = utf8_general_ci
character_set_server = utf8
# 重启 mysql 服务
[root@localhost mysql]# service mysqld restart
# 登录 mysql 并查看编码,显示以下数据对比表示修改成功!
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
远程连接
# 开启远程连接
mysql> grant all privileges on *.* to root@'%' identified by '123456';
# 执行生效
mysql> flush privileges;
# rpm 安装无需配置自启
防火墙
# 关闭防火墙,否则sqlyog连接不上 报 2002 错误
[root@localhost mysql]# systemctl stop firewalld.service
# 禁用防火墙自启动,重启服务器时,无需在关闭防火墙
[root@localhost mysql]# systemctl disable firewalld.service
查看 mysql 版本
# 查看 mysql 命令
[root@localhost mysql]# mysql --version
mysql Ver 14.14 Distrib 5.7.33, for linux-glibc2.12 (x86_64) using EditLine wrapper
安装完毕!
2.2. 卸载
# 查看是否安装了mysql
[root@localhost local]# pm -qa|grep mysql
mysql-community-common-5.7.33-1.el7.x86_64
mysql-community-client-5.7.33-1.el7.x86_64
mysql-community-server-5.7.33-1.el7.x86_64
mysql-community-libs-5.7.33-1.el7.x86_64
# 停止mysql的服务并删除,使用 rpm -ev xx 进行删除,区分安装的反顺序
[root@localhost local]# rpm -ev mysql-community-server-5.7.33-1.el7.x86_64
[root@localhost local]# rpm -ev mysql-community-client-5.7.33-1.el7.x86_64
[root@localhost local]# rpm -ev mysql-community-libs-5.7.33-1.el7.x86_64
[root@localhost local]# rpm -ev mysql-community-common-5.7.33-1.el7.x86_64
# 查看是否还有mysql遗留数据
# 数据库目录 /var/lib/mysql
# 配置文件 /usr/share/mysql(mysql.server命令及配置文件)
# 相关命令 /usr/bin (mysqladmin mysqldump等命令)
[root@localhost local]# find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/share/mysql
# 将上面显示的文件夹一一删除
[root@localhost local]# rm -rf /var/lib/mysql/
[root@localhost local]# rm -rf /usr/share/mysql
# 综上步骤,可以将 mysql 彻底删除干净
卸载完毕!
3. yum
3.1. 安装
在官网里面,找到与自己linux系统对应的rpm,并下载 说明:这里的rpm安装后会加载出mysql的源,跟着小编走就行了哈~~~
两种方式在linux中获取到 rpm 安装包,在linux中通过 yum 进行安装
# 方式一 :将下载好的 rpm 手动移入到 linux 中
# 方式二 :直接在 linux 中,通过命令 + 下载链接获取到
# 说明 : 方式一 下载 windows 上,就还需要手动拖入到 linux 中,方式二 拿到下载链接,在 linux 中,使用命令 + 链接,直接到linux中
# 使用命令网上下载资源,先到 /usr/local 文件夹中
[root@localhost local]# wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
# 下载完成后,会在当前文件下,安装源
[root@localhost local]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
# 安装完成后,查看新安装的源
[root@localhost local]# yum repolist all | grep mysql
# 先取消掉默认的 mysql 8 的安装
[root@localhost local]# yum-config-manager --disable mysql80-community
# 设置 mysql 7 的默认安装
[root@localhost local]# yum-config-manager --enable mysql57-community
# 安装 mysql 服务,此过程漫长,默认安装 mysql 5.7 系列最新的版本
[root@localhost local]# yum install mysql-community-server
# 开启 mysql 服务
[root@localhost local]# systemctl start mysqld.service
# 获取初始化密码,在 /var/log/mysqld.log 中获取
[root@localhost local]# grep "temporary password" /var/log/mysqld.log
# 使用初始化密码登录 mysql
[root@localhost local]# mysql -u root -p
Enter password:
密码
# 登录成功,修改密码,当前有默认的密码规则
mysql> set password='@Ch123456';
# 更改 mysql 密码规则
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
# LOW:0 | MEDIUM:1 | STRONG:2
# 0/LOW:只检查长度
# 1/MEDIUM:检查长度、数字、大小写、特殊字符
# 2/STRONG:检查长度、数字、大小写、特殊字符字典文件
# 设置 validate_password_policy 为 MEDIUM 修改为 LOW 表示密码强度等级
mysql> set global validate_password_policy=LOW;
# 设置 validate_password_length 为 8 修改为 6 表示密码长度
mysql> set global validate_password_length=6;
# 查看是否生效
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 6 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
# 最终修改密码,建议密码设置成123456
mysql> set password='123456';
编码
# 查看mysql编码
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
# 退出mysql登录,查找配置文件
[root@localhost mysql]# find / -iname '*.cnf' -print
/etc/pki/tls/openssl.cnf
/etc/my.cnf
/var/lib/mysql/auto.cnf
# 修改配置文件
[root@localhost mysql]# vim /etc/my.cnf
# 在 [mysqld] 下,添加以下两段代码
collation_server = utf8_general_ci
character_set_server = utf8
# 重启 mysql 服务
[root@localhost mysql]# service mysqld restart
# 登录 mysql 并查看编码,显示以下数据对比表示修改成功!
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
远程连接
# 开启远程连接
mysql> grant all privileges on *.* to root@'%' identified by '123456';
# 开启远程时报错,以下提示信息,请注意
# Your password does not satisfy the current policy requirements 当前密码不符合规则
# 解决方案 重新执行一遍 改变密码的规则的代码
# 设置 validate_password_policy 为 MEDIUM 修改为 LOW 表示密码强度等级
mysql> set global validate_password_policy=LOW;
# 设置 validate_password_length 为 8 修改为 6 表示密码长度
mysql> set global validate_password_length=6;
# 查看是否生效
mysql> SHOW VARIABLES LIKE 'validate_password%';
# 执行生效
mysql> flush privileges;
# yum 安装无需配置自启
防火墙
# 关闭防火墙,否则sqlyog连接不上 报 2002 错误
[root@localhost mysql]# systemctl stop firewalld.service
# 禁用防火墙自启动,重启服务器时,无需在关闭防火墙
[root@localhost mysql]# systemctl disable firewalld.service
查看 mysql 版本
# 查看 mysql 命令
[root@localhost mysql]# mysql --version
mysql Ver 14.14 Distrib 5.7.33, for linux-glibc2.12 (x86_64) using EditLine wrapper
安装完毕!
3.2. 卸载
# 查看是否安装了mysql
[root@localhost local]# pm -qa|grep mysql
mysql-community-client-5.7.34-1.el7.x86_64
mysql-community-common-5.7.34-1.el7.x86_64
mysql-community-devel-5.7.34-1.el7.x86_64
mysql-community-libs-5.7.34-1.el7.x86_64
mysql80-community-release-el7-3.noarch
mysql-community-server-5.7.34-1.el7.x86_64
# 停止mysql的服务
[root@localhost local]# systemctl stop mysqld.service
# 卸载 mysql,但 mysql80-community-release-el7-3.noarch 是mysql源,无需卸载,后续使用到 yum 安装,就需要它的源进行操作
[root@localhost local]# yum -y remove mysql-community-client-5.7.34-1.el7.x86_64 mysql-community-common-5.7.34-1.el7.x86_64 mysql-community-devel-5.7.34-1.el7.x86_64mysql-community-libs-5.7.34-1.el7.x86_64 mysql-community-server-5.7.34-1.el7.x86_64
# 查看是否还有mysql遗留数据
# 数据库目录 /var/lib/mysql
# 配置文件 /usr/share/mysql(mysql.server命令及配置文件)
# 相关命令 /usr/bin (mysqladmin mysqldump等命令)
[root@localhost local]# find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/share/mysql
# 将上面显示的文件夹一一删除
[root@localhost local]# rm -rf /var/lib/mysql/
[root@localhost local]# rm -rf /usr/share/mysql
# 综上步骤,可以将 mysql 彻底删除干净
说明
# rpm 安装适用于手残党,安装位置固定,环境变量无需配置
# tar 安装比较自由,随意安装在哪里,只需在环境配置文件,配置mysql的安装位置信息等即可!
# yum 安装使用多个版本资源,直接联网安装下载
# yum 和 rpm 都多了密码规则的修改,其他没什么变化
# 综上都可以安装好mysql环境,编写不易,转载请标明出处
从入门到崩溃