CentOs自从Mysql被收购后,放弃了之前内置Mysqll的方式,从Centos7开始默认安装Mariadb

使用命令:rpm -qa|grep mariadb查看linux中已经有Mariadb包

centos卸载mariadb重装mysql centos mariadb安装_数据库

发现版本实在是太旧了,所以要卸载这个,安装新版。

卸载过程

1.rpm -e --nodeps mariadb-*

centos卸载mariadb重装mysql centos mariadb安装_root用户_02

发现未安装,执行命令

2.yum remove mysql mysql-server mysql-libs compat-mysql51

centos卸载mariadb重装mysql centos mariadb安装_mysql_03

3.再次运行命令,rpm -qa|grep mariadb

可以看到已经卸载干净了

centos卸载mariadb重装mysql centos mariadb安装_数据库_04

安装过程:

1.创建mariadb配置文件

vi /etc/yum.repos.d/MariaDB.repo

2.在这个新文件里需要插入mariadb下载链接和版本,由于防火墙的原因,直接从国外下载会异常缓慢,所以使用国内镜像进行替代

# MariaDB 10.2 CentOS repository list - created 2017-07-03 06:59 UTC

# http://downloads.mariadb.org/mariadb/repositories/

[mariadb] 
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

然后Esc退出编辑,直接:x强制保存退出

3.运行安装命令

yum -y install MariaDB-server MariaDB-client

4.一通下载之后,终于完成,启动mariadb服务

systemctl start mariadb #启动服务

systemctl enable mariadb #设置开机启动

systemctl restart mariadb #重新启动

systemctl stop mariadb.service #停止MariaDB

 

 

设置数据库基本信息

1.登陆数据库mysql -uroot

centos卸载mariadb重装mysql centos mariadb安装_数据库_05

此时root用户密码为空

2.对maraidb进行配置mysql_secure_installation

centos卸载mariadb重装mysql centos mariadb安装_root用户_06

centos卸载mariadb重装mysql centos mariadb安装_数据库_07

[root@shusheng ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none): 
Aborting!

Cleaning up...
[root@shusheng ~]# clear
[root@shusheng ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@shusheng ~]#

View Code

 首先是设置密码,会提示先输入密码

Enter current password for root (enter for none):<–初次运行直接回车

设置密码

Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码

其他配置

Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车

Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车,

Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车

Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车

3.测试登陆

mysql -uroot -ppassowrd

centos卸载mariadb重装mysql centos mariadb安装_数据库_08

登陆成功

 

 

配置数据库字符集

1.使用命令查看my.cnf文件,最后一句再说配置引入的是my.cnf.d下的文件

vi /etc/my.cnf

centos卸载mariadb重装mysql centos mariadb安装_root用户_09

2.vi server.cnf,在[mysqld]标签下添加

init_connect='SET collation_connection = utf8_unicode_ci' 
init_connect='SET NAMES utf8' 
character-set-server=utf8 
collation-server=utf8_unicode_ci 
skip-character-set-client-handshake

 

2.vi /etc/my.cnf.d/client.cnf

在[client]中添加

default-character-set=utf8

3.vi /etc/my.cnf.d/mysql-clients.cnf

在[mysql]中添加

default-character-set=utf8

4.重启mariadb

systemctl restart mariadb

5.进入数据库中查看修改过的编码

centos卸载mariadb重装mysql centos mariadb安装_数据库_10

修改编码成功

 

添加用户

1.创建用户

MariaDB [(none)]> create user shusheng@localhost identified by 'shusheng';

2.直接创建用户并授权的命令

MariaDB [(none)]>grant all on *.* to shusheng@localhost indentified by 'shusheng';

3.授予外网登陆权限

MariaDB [(none)]>grant all privileges on *.* to root@'%' identified by 'shusheng';

4.授予权限并且可以授权

MariaDB [(none)]>grant all privileges on *.* to shusheng@'hostname' identified by 'shusheng' with grant option;

 

使用navicat测试连接:

centos卸载mariadb重装mysql centos mariadb安装_数据库_11

出问题了,怀疑是防火墙没关闭

运行命令:

直接关闭防火墙

systemctl stop firewalld.service

禁止防火墙开机启动

systemctl disable firewalld.service

然后就成功了

centos卸载mariadb重装mysql centos mariadb安装_数据库_12

补充:

centos7对于防火墙的设置

1、firewalld的基本使用

启动: systemctl start firewalld
查看状态: systemctl status firewalld 
停止: systemctl disable firewalld
禁用: systemctl stop firewalld

2.开启一个端口:

添加
firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent