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 即可。

相关推荐
chlk12311 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑11 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件12 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号21 小时前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash1 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI1 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
于眠牧北2 天前
MySQL的锁类型,表锁,行锁,MVCC中所使用的临键锁
mysql
十日十行2 天前
Linux和window共享文件夹
linux
木心月转码ing2 天前
WSL+Cpp开发环境配置
linux
Turnip12023 天前
深度解析:为什么简单的数据库"写操作"会在 MySQL 中卡住?
后端·mysql