CentOS7安装MySQL

本文详细介绍了在CentOS环境下安装MySQL5.7的步骤,包括检查系统、下载安装包、安装服务端、启动服务、登录数据库、修改密码及开启远程连接等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 先检查系统是否装有MySQL

$ rpm -qa | grep mysql

  返回空值,说明没安装,如果有安装,则删除可用:

$ yum remove mysql

  注意:这里执行安装命令yum install mysql是无效的,因为centos-7默认是Mariadb,所以它只是更新Mariadb数据库。

2 下载MySQL

  从 MySQL 官网选取合适的 MySQL 版本,获取下载地址。然后使用 wget 下载:

$ wget http://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm

3 安装yum repository

$ yum -y install mysql57-community-release-el7-8.noarch.rpm

4 安装MySQL服务端

  查看mysql server:

$ yum search mysql-com

  安装:

$ yum -y install mysql-community-server.x86_64

5 启动MySQL服务

  注意 CentOS7 的启动方式和以前不一样,CentOS7 使用了 systemctl

$ systemctl start mysqld.service

  查看启动是否成功:

$ systemctl status mysqld.service

6 登录数据库

  MySQL5.7.6 之后会在启动 mysql 进程的时候生成一个用户密码,首次登陆需要这个密码才行。密码保存在 mysql 进程的日志里,即(/var/log/mysqld.log)。
  查看密码:

[root@localhost ~]# cat /var/log/mysqld.log | grep 'password'
2018-05-09T05:05:40.656798Z 1 [Note] A temporary password is generated for root@localhost: pldEoqu1h!ks

  登录:

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22

Copyright (c) 2000, 2018, 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> 

  安装完成!

7 修改root密码

  如果需要正常使用,需要重置密码,否则如下:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

  重置密码:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_passwd';

  注意设置密码的复杂度,过于简单会报错:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

8 开启远程连接

  CentOS系统安装好MySQL后,默认情况下不支持用户通过非本机连接上数据库服务器,开启远程连接方法:

# 任何远程主机都可以访问数据库 
$ GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
# 使修改生效
$ FLUSH PRIVILEGES;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值