Centos7下安装配置MariaDB及忘记root密码重置

1.安装mariadb

通过yum安装mariadb-server,默认依赖安装mariadb,一个是服务端、一个是客户端。

#] yum -y install mariadb mariadb-server

2.配置mariadb

1)安装完成后首先要把MariaDB服务开启,并设置为开机启动

]# systemctl start mariadb  # 开启服务
]# systemctl enable mariadb  # 设置为开机自启动服务

2)首次安装需要进行数据库的配置,命令都和mysql的一样

]# mysql_secure_installation

3)配置时出现的各个选项
Enter current password for root (enter for none): # 输入数据库超级管理员root的密码(注意不是系统root的密码),第一次进入还没有设置密码则直接回车

Set root password? [Y/n] # 设置密码,y

New password: # 新密码

Re-enter new password: # 再次输入密码

Remove anonymous users? [Y/n] # 移除匿名用户, y

Disallow root login remotely? [Y/n] # 拒绝root远程登录,n,不管y/n,都会拒绝root远程登录

Remove test database and access to it? [Y/n] # 删除test数据库,y:删除。n:不删除,数据库中会有一个test数据库,一般不需要

Reload privilege tables now? [Y/n] # 重新加载权限表,y。或者重启服务也行

4)测试是否能够登录成功,出现 MariaDB [(none)]> 就表示已经能够正常登录使用MariaDB数据库了

]# mysql -u root -p
 Enter password:

3.设置mariadb字符集为utf-8

1)/etc/my.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)/etc/my.cnf.d/client.cnf 文件
在 [client] 标签下添加

default-character-set=utf8

3)/etc/my.cnf.d/mysql-clients.cnf 文件
在 [mysql] 标签下添加

default-character-set=utf8

4)重启服务

]# systemctl restart mariadb

5)进入mariadb查看字符集

MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

MariaDB [(none)]>

4.忘记mariadb数据库root密码

#####经测试,此方法在centos7和centos8上都可成功执行恢复
版本:
Server version: 10.3.17-MariaDB MariaDB Server
mariadb-5.5.65-1.el7.x86_64
mariadb-server-5.5.65-1.el7.x86_64

解释:在MariaDB/MySQL中,官方保留了一个特权模式

1)停止数据库服务

]# systemctl stop mariadb
]# ps -ef | grep mariadb
 root      8735  7880  0 16:13 pts/0    00:00:00 grep --color=auto mariadb
]# netstat -ntlp | grep mysqld
]#

停止服务后,需要通过ps查看是否保留相关进程,然后使用netstat命令查看端口占用情况,从上面的反馈信息来看,mariadb服务已经被杀的一干二净了,这时候就可以使用特权模式进行密码修改了。

2)启动特权模式

]# mysqld_safe --skip-grant-tables &
 [1] 8738
]# 200909 16:14:49 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
200909 16:14:49 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

3)出现上面那些后直接输出 mysql,然后回车,就会登录到mariadb数据库

]# 200909 16:14:49 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
200909 16:14:49 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysql        //此处输入后回车,下面内容可能不会显示,但只要进入数据库就是成功
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

4)选择mysql数据库

MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

5)更新root密码

Mariadb [(mysql)]> update user set password=password('mariadb123') where user='root';
其中mariadb123改为新的密码即可

6)刷新权限并退出登录

Mariadb [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

Mariadb [(none)]> exit;

7)停止特权模式

]# pkill mysqld

8)启动mariadb服务

]# systemctl start mariadb

启动成功之后,尝试使用特权模式的登录命令进行登录,然后发现登录失败,说明特权模式已经关闭了

]# mysql
 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

9)使用新密码登录

]# mysql -u root -p mariadb123
 Welcome to the MariaDB monitor.  Commands end with ; or \g.
 Your MariaDB connection id is 2
 Server version: 5.5.65-MariaDB MariaDB Server

 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

修改成功!

这是我之前读过几篇文章后,进行组合和自己的理解翻译出来的,但是前几篇文章地址不记得了,对此非常抱歉!

欢迎有兴趣的朋友加入运维群交流学习!!!
微信:zdqqbb

你可能感兴趣的:(笔记,mariadb,centos,数据库)