阿里云(CentOS7.4)安装MySQL,以及本地远程连接MySQL
阿里云yum安装MySQL
第一步:检查是否本机存在mysql
rpm -qa | grep mysql
yum info mysql-community-server
第二步:拷贝mysql官网yum-community-mysql的下载地址
https://dev.mysql.com/downloads/
https://dev.mysql.com/downloads/file/?id=484922
第三步:安装mysql-community-server
rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
第四步:再次检查mysql-community-server信息
yum info install mysql-community-server
Loaded plugins: fastestmirror
mysql-connectors-community | 2.6 kB 00:00:00
mysql-tools-community | 2.6 kB 00:00:00
mysql80-community | 2.6 kB 00:00:00
(1/3): mysql-connectors-community/x86_64/primary_db | 68 kB 00:00:00
(2/3): mysql-tools-community/x86_64/primary_db | 83 kB 00:00:00
(3/3): mysql80-community/x86_64/primary_db | 128 kB 00:00:00
Loading mirror speeds from cached hostfile
Available Packages
Name : mysql-community-server
Arch : x86_64
Version : 8.0.22
Release : 1.el7
Size : 510 M
Repo : mysql80-community/x86_64
Summary : A very fast and reliable SQL database server
URL : http://www.mysql.com/
License : Copyright (c) 2000, 2020, Oracle and/or its affiliates. Under GPLv2 license as shown in the Description field.
Description : The MySQL(TM) software delivers a very fast, multi-threaded, multi-user,
: and robust SQL (Structured Query Language) database server. MySQL Server
: is intended for mission-critical, heavy-load production systems as well
: as for embedding into mass-deployed software. MySQL is a trademark of
: Oracle and/or its affiliates
:
: The MySQL software has Dual Licensing, which means you can use the MySQL
: software free of charge under the GNU General Public License
: (http://www.gnu.org/licenses/). You can also purchase commercial MySQL
: licenses from Oracle and/or its affiliates if you do not wish to be bound by the terms of
: the GPL. See the chapter "Licensing and Support" in the manual for
: further info.
:
: The MySQL web site (http://www.mysql.com/) provides the latest news and
: information about the MySQL software. Also please see the documentation
: and the manual for more information.
:
: This package includes the MySQL server binary as well as related utilities
: to run and administer a MySQL server.
第五步:安装MySQL:我们安装的是MySQL8.0.22版本
yum -y install mysql-community-server
第六步:检查MySQL的状态,并启动MySQL(这里是CentOS7.4的指令)
//检查MySQL的状态
systemctl status mysqld
//启动MySQL
systemctl start mysqld
第七步:我们查看此时的临时密码,并进行重置root密码操作
//查看root账户的临时密码
grep "temporary password" /var/log/mysqld.log
//进行root密码的重置操作
//注意:MySQL8版本以上的密码初始安全级别为中等:密码需要设置为至少8位,包括大写字母和特殊字符
mysqladmin -u root -p password
先输入上面的临时密码,再输入你设置的新密码,同时进行密码的确认
第八步:此时你就可以使用你的root账户和密码进行MySQL的登录操作了
mysql -u root -p
输入密码: 你的密码
登录成功!!!
远程连接阿里云的MySQL数据库
1、赋予账号远程登录权限
MySQL默认情况下只允许用户本地登录,所有需要赋予用户远程登录的权限。进去MySQL,
mysql> grant all privileges on *.* to '用户'@'ip' identified by '密码' with grant option;
mysql> flush privileges;
第一句语句语句中,*.*代表所有库表,若想给予所有IP权限,"ip"写成“%”,所以第一句sql的意思是给予来自所有IP地址的通过“用户”,
“密码”登录的用户对所有库表的所有操作权限。
第二句sql,刷新权限。
然后同样重启MySQL生效 systemctl restart mysql
注意:MySQL8版本以上不支持上面的写法,需要按照下面命令执行:
CREATE USER 'root'@'%' IDENTIFIED BY '你的密码';
GRANT ALL ON *.* TO 'root'@'%';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的密码';
三条命令按顺序执行完成后,刷新权限:
flush privileges
最后重启MySQL生效
2、检查防火墙
服务器是否开启了防火墙,防火墙是否允许3306端口连接
登录云服务器控制台->安全组->配置规则。默认情况下,入方向阿里云只允许访问3389,22,-1端口,所以添加3306,即可成功
登录MySQL出现如下错误解决方法
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password)
这个问题一般是重置密码出现的问题,所以我们需要按照以下步骤进行密码的重新设置操作
第一步:找到my.cnf文件的位置,我这里的my.cnf文件在/etc目录下
在[mysqld]下方添加下面代码
//直接跳过密码进行登录
skip-grant-tables
第二步:我们直接登录MySQL,此时无需密码可以登录成功
//1.登录MySQL
mysql -u root
//2.重置修改root账户的密码
mysql>alter user 'root'@'localhost' identitfied by '你要设置的密码';
如果出现1290ERROR,使用 flush privileges命令刷新一下权限再执行上面操作即可
//3.最后再进行刷新权限操作
mysql>flush privileges
mysql>quit
//4.将 /etc/my.cnf中的skip-grant-tables进行删除操作
//5.重启mysql
systemctl restart mysqld
此时再使用你的root账户和新重置的密码进行登录则直接完美登录!