17090701_CentOS7(64)下MySQL卸载、安装与配置
1. 环境准备
1.1 系统
操作系统:CentOS7(64位)
1.2 软件工具
2. 方法/步骤
2.1 卸载MySql数据库
在安装MySQL数据库之前,先查看一下是否已经安装,如已安装,卸载后重新安装。
查资料发现CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替。
2.1.1 yum卸载mariadb和mysql
# rpm -qa | grep mariadb
#然后使用remove
# yum remove mariadb*;
#把安装包全给删了
# rpm -qa | grep mysql
# yum remove mysql*;
2.1.2 rm删除mariadb和mysql相关的文件
# find / -name mysql*;
#把所有的文件都删了
# rm -rf 搜索出来的文件路径
# find / -name mariadb*;
#把所有文件都删了
rm -rf 搜索出来的文件路径
2.2 安装MySql数据库
2.2.1 查看系统环境
yum update升级以后的系统版本为:
# cat /etc/redhat-release
CentOS Linuxrelease 7.2.1511 (Core)
2.2.2 安装mysql和mysql-devel
# yum install mysql
# yum install mysql-devel
2.2.3 安装mysql-server
安装mysql和mysql-devel一般都成功,但是安装mysql-server失败,如下:
# yum install mysql-server
Loaded plugins:fastestmirror
Loading mirrorspeeds from cached hostfile
* base: mirrors.sina.cn
* extras: mirrors.sina.cn
* updates: mirrors.sina.cn
No packagemysql-server available.
Error: Nothingto do
查资料发现是CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了。
有两种解决办法:
方法一:安装mariadb(亲测可用)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。
1) 安装mariadb:
# yum install mariadb-server mariadb
2) 设置开机启动mariadb数据库服务:
# systemctl enable mariadb
3) 启动mariadb数据库服务:
# systemctl start mariadb
然后就可以正常使用mysql了
方法二:官网下载安装mysql-server(未亲测)
1) 官网下载安装mysql-server
# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
# rpm -ivh mysql-community-release-el7-5.noarch.rpm
# yum install mysql-community-server
2) 安装成功后重启mysql服务
# service mysqld restart
2.2.4 为root用户设置密码(初始状态下root无密码)
此处提供三种方法:
方法1: 用SET PASSWORD命令(亲测可用)
# mysql -u root -p
Enter password:(无密码直接回车)
Welcome to theMariaDB monitor. Commands end with ; or\g.
Your MariaDBconnection id is 26
Server version:5.5.52-MariaDB MariaDB Server
Copyright (c)2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or'\h' for help. Type '\c' to clear the current input statement.
MariaDB[(none)]> set password for'root'@'localhost' = password('password');
Query OK, 0 rowsaffected (0.00 sec)
方法2:用mysqladmin
1.例如你的 root用户现在没有密码,你希望的密码修改为(1234),那么命令是:(亲测)
# mysqladmin -u root password 1234
2.如果你的root现在有密码了(password),那么修改密码为(1234)的命令是:(亲测)
# mysqladmin -u root -p password 1234
注意,命令回车后会问你旧密码,输入旧密码(password)之后命令完成,密码修改成功。
3.如果你的root现在有密码了(1234),那么修改密码为(pass)的命令是:(亲测)
# mysqladmin -u root -p1234 password pass
(注意-p 不要和后面的密码分开写,要写在一起,不然会出错,错误如下所示)
[[email protected]]# mysqladmin -u root -p 1234 password pass
Enter password:
mysqladmin:Unknown command: '1234'
4.使用phpmyadmin,这是最简单的了,修改mysql库的user表,不过别忘了使用PASSWORD函数。(未测)
方法3:用UPDATE直接编辑user表(未测)
mysql -u root
mysql> usemysql;
mysql>UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
mysql>FLUSH PRIVILEGES;
在丢失root密码的时候,可以这样
mysqld_safe--skip-grant-tables&
mysql -u rootmysql
mysql>UPDATE user SET password=PASSWORD("new password") WHERE user='root';
mysql> FLUSH PRIVILEGES;2.2.5 配置数据库编码(utf-8)
mysql配置文件为/etc/my.cnf,最后加上编码配置(红字部分)。
[[email protected]~]# vim /etc/my.cnf
[[email protected]~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disablingsymbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings userand group are ignored when systemd is used.
# If you need torun mysqld under a different user or group,
# customize yoursystemd unit file for mariadb according to the
# instructionsin http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
[mysql]
default-character-set =utf8
#
# include allfiles from the config directory
#
!includedir/etc/my.cnf.d
这里的字符编码必须和/usr/share/mysql/charsets/Index.xml中一致。
2.2.6 授权远程访问
[[email protected]]# mysql -u root -p
Enter password:
Welcome to theMariaDB monitor. Commands end with ; or\g.
Your MariaDBconnection id is 39
Server version:5.5.52-MariaDB MariaDB Server
Copyright (c)2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or'\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases; #显示数据库一览
+--------------------+
| Database |
+--------------------+
|information_schema |
| mysql |
|performance_schema |
| test |
+--------------------+
4 rows in set(0.11 sec)
MariaDB [(none)]>use mysql #使用mysql数据库(真正的数据库,而非数据库服务)
Reading tableinformation for completion of table and column names
You can turn offthis feature to get a quicker startup with -A
Database changed
MariaDB[mysql]> grant all privileges on *.* to [email protected]'%'identified by 'pass'; #将所有数据库的所有表(*.*)的所有权限(all privileges),授予通过任何ip(%)访问的密码为pass的root用户。
Query OK, 0 rowsaffected (0.07 sec)
MariaDB[mysql]> flush privileges; #最后刷新即可
Query OK, 0 rowsaffected (0.05 sec)
2.2.7 开放防火墙端口:
1.安装iptables服务(首次配置时,已亲测)
由于在新安装的CentOS7系统中,防火墙默认是被禁掉的,一般也没有配置过任何防火墙的策略,所以不存在/etc/sysconfig/iptables文件。
1) 停止并屏蔽firewalld服务
# systemctl stop firewalld
# systemctl mask firewalld
2) 安装iptables-services软件包
# yum install iptables-services
3) 在引导时启用iptables服务
# systemctl enable iptables
4) 启动iptables服务
# systemctl start iptables
iptables服务指令:
systemctl[stop|start|restart] iptables
5) 保存防火墙规则
# service iptables save或/usr/libexec/iptables/iptables.init save
/etc/sysconfig/iptables存在。
2.添加防火墙端口规则(已亲测)
通过vim修改/etc/sysconfig/iptables,添加一行。
# vim /etc/sysconfig/iptables
3.重启iptables防火墙服务
# systemctl restart iptables
2.2.8 在windows环境下,用navicat测试:
远程连接成功。
3. 最后,介绍一下mariadb数据库的相关命令是:
systemctl start mariadb #启动MariaDB
systemctl stop mariadb #停止MariaDB
systemctlrestart mariadb #重启MariaDB
systemctl enable mariadb #设置开机启动
systemctldisable mariadb #关闭开机启动
systemctl status mariadb #查看运行状态
最后,说明下本文章的内容是结合网上的一些资料和自己的实践整理完成的,仅供学习之用。感谢每一个乐于共享知识的人。如有错误或建议,请多多指正。谢谢!