卸载mariadb

检查是否安装mariadb,若安装则卸载
在这里插入图片描述

[root@localhost ~]# 
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@localhost ~]# rpm -qa | grep mariadb-server
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
[root@localhost ~]# rpm -qa | grep mariadb
[root@localhost ~]# 

下载

下载mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar,地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
在这里插入图片描述
登录后可进行下载,如下所示:
在这里插入图片描述
或者(注意和上面版本差一点)

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar

安装

上传

使用xftp上传到“/usr/local/mysql”(rz -be的方法太慢了)
在这里插入图片描述

解压

[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar

mysql-community-embedded-devel-5.7.26-1.el7.x86_64.rpm
mysql-community-libs-5.7.26-1.el7.x86_64.rpm
mysql-community-embedded-5.7.26-1.el7.x86_64.rpm
mysql-community-test-5.7.26-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.26-1.el7.x86_64.rpm
mysql-community-common-5.7.26-1.el7.x86_64.rpm
mysql-community-devel-5.7.26-1.el7.x86_64.rpm
mysql-community-client-5.7.26-1.el7.x86_64.rpm
mysql-community-server-5.7.26-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm

[root@localhost mysql]# 

安装

只需要安装其中4个即可

[root@localhost mysql]# rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
[root@localhost mysql]# rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm 
[root@localhost mysql]# rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm 

若发生下面报错,则安装net-tool后再装即可

[root@localhost mysql]# rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm 
警告:mysql-community-server-5.7.26-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
	net-tools 被 mysql-community-server-5.7.26-1.el7.x86_64 需要

[root@localhost mysql]# yum install net-tools
[root@localhost mysql]# rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm 

配置

为了保证数据库目录为与文件的所有者为 mysql 登陆用户,如果你是以 root 身份运行 mysql 服务,需要执行下面的命令初始化

[root@localhost mysql]# mysqld --initialize --user=mysql

修改密码

直接登录会出错,修改文件/etc/my.cnf,后面添加skip-grant-tables,这是表示没有密码可登录,授权跳跃(后面这行还是要去掉的)。

[root@localhost mysql]# vi /etc/my.cnf
// 按i进入编辑,末尾添加
skip-grant-tables
// 按esc,输入:wq保存病退出;然后重启mysqld服务
[root@VM_5_17_centos mysql]# systemctl restart mysqld

启动并修改root密码为123456

[root@localhost mysql]# mysql -u root
...
mysql> use mysql;
mysql> select User from user;
	+---------------+
	| User          |
	+---------------+
	| mysql.session |
	| mysql.sys     |
	| root          |
	+---------------+
	3 rows in set (0.00 sec)

mysql> update mysql.user set authentication_string=password('123456') where user='root';
mysql> flush privileges;
mysql> quit;
[root@localhost mysql]# 

删除上面的skip-grant-tables,重启服务后,登录成功,如下所示:

[root@localhost mysql]# systemctl stop mysqld
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# mysql -u root -p
Enter password: 123456

mysql> 

远程访问权限

mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by '123456';
mysql> flush privileges;
mysql> use mysql;
mysql> update user set host='%' where user='root';
mysql> select host,user from user;
	+-----------+---------------+
	| host      | user          |
	+-----------+---------------+
	| %         | root          |
	| localhost | mysql.session |
	| localhost | mysql.sys     |
	+-----------+---------------+
	3 rows in set (0.00 sec)

mysql> quit;
[root@localhost mysql]# systemctl stop mysqld;
[root@localhost mysql]# systemctl start mysqld

最后可以吧/usr/local/mysql文件夹干掉了,没什么用了

测试

关闭防火墙,再用另外一台电脑上用Navicat for MySQL进行连接,如下所示:

[root@localhost mysql]# systemctl stop mysqld;
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# systemctl stop firewalld

在这里插入图片描述
在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐