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

相关推荐
嵩山小老虎1 天前
Windows 10/11 安装 WSL2 并配置 VSCode 开发环境(C 语言 / Linux API 适用)
linux·windows·vscode
Fleshy数模1 天前
CentOS7 安装配置 MySQL5.7 完整教程(本地虚拟机学习版)
linux·mysql·centos
a41324471 天前
ubuntu 25 安装vllm
linux·服务器·ubuntu·vllm
az44yao1 天前
mysql 创建事件 每天17点执行一个存储过程
mysql
一只自律的鸡1 天前
【Linux驱动】bug处理 ens33找不到IP
linux·运维·bug
17(无规则自律)1 天前
【CSAPP 读书笔记】第二章:信息的表示和处理
linux·嵌入式硬件·考研·高考
!chen1 天前
linux服务器静默安装Oracle26ai
linux·运维·服务器
秦老师Q1 天前
php入门教程(超详细,一篇就够了!!!)
开发语言·mysql·php·db
REDcker1 天前
Linux 文件描述符与 Socket 选项操作详解
linux·运维·网络
蒹葭玉树1 天前
【C++上岸】C++常见面试题目--操作系统篇(第二十八期)
linux·c++·面试