Linux系列:Linux 安装 MySQL 5.7.27 教程

1. 检查当前系统是否安装过mysql

bash 复制代码
[root@yum ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64 #已经存在
# 存在则先卸载
[root@yum ~]# rpm -e --nodeps  mariadb-libs

2. 检查当前mysql依赖环境

bash 复制代码
[root@yum ~]# rpm -qa|grep libaio
libaio-0.3.109-13.el7.x86_64 #已经存在

[root@yum ~]# rpm -qa|grep net-tools
#不存在 net-tools 则安装
[root@yum ~]# yum -y install net-tools
[root@yum ~]# rpm -qa|grep net-tools
net-tools-2.0-0.25.20131004git.el7.x86_64 #已经存在

3. 上传 mysql 安装包到 Linux 的 /root/ apps/mysql目录下并解压

bash 复制代码
[root@yum ~]# mkdir /root/apps/mysql
[root@yum ~]# tar -xvf mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar -C /root/apps/mysql

4. 在mysql 的安装目录下执行:(必须按照顺序执行)

bash 复制代码
[root@yum ~]# cd /root/apps/mysql/
[root@yum mysql]# rpm -ivh mysql-community-common-5.7.27-1.el7.x86_64.rpm
[root@yum mysql]# rpm -ivh mysql-community-libs-5.7.27-1.el7.x86_64.rpm
[root@yum mysql]# rpm -ivh mysql-community-client-5.7.27-1.el7.x86_64.rpm
[root@yum mysql]# rpm -ivh mysql-community-server-5.7.27-1.el7.x86_64.rpm

5. 查看mysql 安装版本

bash 复制代码
[root@yum mysql]# mysqladmin --version
mysqladmin  Ver 8.42 Distrib 5.7.27, for Linux on x86_64

6. mysql 服务初始化

为了保证数据库目录为与文件的所有者为 mysql 登陆用户,如果你是以 root 身份运行 mysql 服务,需要执行下面的命令初始化

bash 复制代码
[root@yum ~]# mysqld --initialize --user=mysql
#查看初始化密码
[root@yum ~]# cat /var/log/mysqld.log
2021-06-06T14:08:09.011209Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-06T14:08:09.223359Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-06-06T14:08:09.251594Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-06-06T14:08:09.314410Z 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: 9f4e6310-c6d0-11eb-b791-000c29e525a3.
2021-06-06T14:08:09.321278Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
#root@localhost后面就是初始化密码:oyiNZ7(9q<q*
2021-06-06T14:08:09.321927Z 1 [Note] A temporary password is generated for root@localhost: oyiNZ7(9q<q* 

注意:另外 --initialize 选项默认以"安全"模式来初始化,则会为 root 用户生成一个密码并将该密码标记为过期,登陆后你需要设置一个新的密码。

7. 启动 mysql 服务

bash 复制代码
#启动mysql服务
[root@yum ~]# systemctl start mysqld.service
#设置开机启动mysql服务
[root@yum ~]# systemctl enable mysqld.service

停止mysql服务器:#systemctl stop mysqld.service

8. 首次登录 mysql 和修改初始密码

bash 复制代码
[root@yum ~]# 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.27
mysql>

#这里的初始化密码默认是过期的,查看数据库会报错如下:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

#修改密码(这里密码改为了 root)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.00 sec)

设置完密码就可以用新密码登陆,正常使用数据库了

9. 修改字符集问题

mysql5.7 默认不支持中文,所以直接插入中文数据报错

bash 复制代码
[root@yum ~]# vim  /etc/my.cnf
#行尾添加
character_set_server=utf8

#重新启动mysql服务使修改配置生效
[root@yum ~]# systemctl restart mysqld

**扩展:**已生成的库表字符集如何变更

bash 复制代码
#修改数据库的字符集
mysql> alter database mydb character set 'utf8';

#修改数据表的字符集
mysql> alter table tableName convert to  character set 'utf8';

10. 远程工具连接 MySQL 数据库

mysql 数据库默认只能本地访问,远程工具连接 mysql 需要授予远程访问权限

bash 复制代码
[root@yum ~]# mysql -uroot -p
Enter password: #这是输入修改后的密码root
mysql>
#授予通过网络方式登录的的root用户 ,对所有库所有表的全部权限,密码设为 root
mysql> grant all privileges on *.* to root@'%'  identified by 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)

接着在 windows 系统下使用 Navicat 工具远程连接 Linux 的 MySQL 即可。

相关推荐
少司府22 分钟前
嵌入式外设篇(二)GPIO·二:GPIO输出与LED控制
linux·stm32·单片机·嵌入式硬件·mcu·51单片机·系统
Rabitebla2 小时前
【Linux操作系统】 文件篇(一):文件描述符与 Linux 文件管理机制
linux·网络·c++·笔记·学习
Flynn Liang2 小时前
HikariCP 连接池不是越大越好:I/O 密集型 Java 服务的数据库容量工程
java·数据库·mysql·高并发·连接池·hikaricp·sql 优化
发疯的技术鼠3 小时前
03-优先级与调度
linux
FungLeo3 小时前
FengCMS · PHP+MySQL 升级实录 ·三重 MD5 迁移 password_hash:老站密码怎么无痛升级
mysql·php·哈希算法·密码安全·fengcms
雾隐隐o4 小时前
优搭校园 01|项目概览、技术选型与初始化
spring boot·mysql·vue
无足鸟ICT5 小时前
【RHCA+】sosreport(收集诊断文档)
linux·运维·服务器
GeW5 小时前
世界目光聚焦中国:底层技术、Linux、数据库的破局逻辑
linux·数据库
Decay__6 小时前
SQLServer 运维自动化平台 —— PowerShell + SQL Agent 作业:部署、监控、巡检、告警、工单闭环一体化
运维·数据库·sql·mysql·架构
星恒随风6 小时前
MySQL 从零入门(十):MVCC、Undo Log 与 Read View 详解——InnoDB 如何实现事务隔离?
linux·数据库·笔记·sql·mysql