欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在Linux下安装MySQL数据库

程序员文章站 2022-05-27 13:26:46
...

这篇文章主要介绍如何在Linux下使用Yum安装MySQL

一、环境

OS信息:

LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.4.1708 (Core) 
Release:    7.4.1708
Codename:   Core

MySQL版本:5.7.20

二、步骤

配置Yum repository

从MySQL官网找到你要的库文件,获取链接,然后下载到本地

wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

添加repository命令

sudo yum localinstall mysql57-community-release-el7-11.noarch.rpm 

检查是否配置成功

shell> yum repolist enabled | grep "mysql.*-community.*"

安装MySQL

shell> sudo yum install mysql-community-server

启动MySQL服务器

shell> sudo service mysqld start
shell> sudo service mysqld status

三、配置

修改密码

安装程序会自动创建一个超级用户'root'@'localhost,密码在错误日志里,可以用下面命令获得:

shell> sudo grep 'temporary password' /var/log/mysqld.log

利用刚才的密码登录:

shell> mysql -uroot -p

修改默认密码:

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

开启外部访问权限

此时,数据库仅能被本机访问,外部访问时会提示该主机不允许连接MySQL服务器:

ERROR 1130 (00000): Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQ
L server

原因是MySQL限制了现有用户只有localhost才能访问

mysql> select host, user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

解决方法是创建允许其它主机访问的用户或直接把root的host改成”%”(表示任何主机)

CREATE USER 'username'@'%' IDENTIFIED BY 'password';
update user set host = '%' where user = 'root';

flush privileges;

其它信息

/etc/my.cnf mysql的主配置文件
/var/lib/mysql mysql数据库文件存放位置
/var/log mysql日志输出存放目录