centos7 安装配置 mariadb 服务器

实验中atom是mysql服务器端,localhost是同一网段下的客户机

安装mariadb

[root@atom ~]#yum install mysql
...
---> Package mariadb.x86_64 1:5.5.68-1.el7 will be installed
--> Finished Dependency Resolution
...
Installed:
  mariadb.x86_64 1:5.5.68-1.el7                                                                                                                           

Complete!
[root@atom ~]#yum install mariadb  #使用这个也可以,均改到开源mysql-mariadb
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror.01link.hk
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package 1:mariadb-5.5.68-1.el7.x86_64 already installed and latest version
Nothing to do
[root@atom ~]#mysql             
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@atom ~]#rpm -ql mariadb-5.5.68-1.el7.x86_64 
/etc/my.cnf.d/client.cnf      #<------只有client配置
/usr/bin/my_print_defaults
/usr/bin/mysql
/usr/bin/mysql_find_rows
/usr/bin/mysql_waitpid
/usr/bin/mysqlaccess
/usr/bin/mysqladmin
...
[root@atom ~]#systemctl start mariadb    
Failed to start mariadb.service: Unit not found.

这样服务是启动不起来的,只安装了client不能当mysql服务器给别人用,下面安装service。


[root@atom ~]#yum install  mariadb.service    #发现没有,打错了
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror.01link.hk
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
No package mariadb.service available.
Error: Nothing to do
[root@atom ~]#yum search mariadb          #search看一下
...
mariadb-libs.i686 : The shared libraries required for MariaDB/MySQL clients
mariadb-libs.x86_64 : The shared libraries required for MariaDB/MySQL clients
mariadb-server.x86_64 : The MariaDB server and related files    #要安装这个来启动mysql服务
anope-mysql.x86_64 : MariaDB/MySQL modules for Anope IRC services
mariadb.x86_64 : A community developed branch of MySQL
mariadb-test.x86_64 : The test suite distributed with MariaD
...
[root@atom ~]#yum install mariadb-server.x86_64        #直接写mariadb-server也可以
[root@atom ~]#systemctl start mariadb              #启动成功

本地mysql安装完成,可能安的不太全后面可以直接 install mariadb* 来安装。


mysql报错1045的话,一般用localhost下的root登入就好了,后面进入后可以更改root用户密码。

[root@atom ~]#mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@atom ~]#mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
...
MariaDB [(none)]> use mysql
MariaDB [mysql]> set password for 'root'@'localhost' = password('123456');

开启远程

[root@atom ~]#mysql -uroot -p
Enter password: 
...
MariaDB [(none)]> use mysql;
Database changed
MariaDB [mysql]> select user,host,plugin from mysql.user;
+------+-----------+--------+
| user | host      | plugin |
+------+-----------+--------+
| root | localhost |        |
| root | atom.2ll  |        |
| root | 127.0.0.1 |        |
| root | ::1       |        |
|      | localhost |        |
|      | atom.2ll  |        |
+------+-----------+--------+

MariaDB [mysql]> CREATE USER 'tonixtom'@'192.168.31.%' IDENTIFIED  BY '123456';		#创建一个固定网段的远程用户
MariaDB [mysql]> grant all privileges on *.* to 'tonixtom'@'192.168.31.%' identified by '123456' with grant option;    #这里给的最大权限
MariaDB [mysql]> show grants for 'tonixtom'@'192.168.31.%';    #show 权限看看
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> select user,host,plugin from mysql.user;
+----------+--------------+--------+
| user     | host         | plugin |
+----------+--------------+--------+
| root     | localhost    |        |
| root     | atom.2ll     |        |
| root     | 127.0.0.1    |        |
| root     | ::1          |        |
|          | localhost    |        |
|          | atom.2ll     |        |
| tonixtom | 192.168.31.% |        |
+----------+--------------+--------+

服务器端mariadb运行良好

[root@atom ~]#systemctl status mariadb.service     #查看mariadb是运行中
[root@atom ~]#netstat -lnp | grep mysql				#全网段监听
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2113/mysqld         
unix  2      [ ACC ]     STREAM     LISTENING     29843    2113/mysqld          /var/lib/mysql/mysql.sock

修改成功后,先在服务器本机上试一下,连接测试中的tonixtom被匿名用户给顶了,登不上,问题不大。

[root@atom ~]#mysql -utonixtom -h192.168.31.177 -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'tonixtom'@'atom.2ll' (using password: YES)

在远程客户端试试,发现问题。

[root@localhost ~]#mysql -utonixtom -h192.168.31.177 -p
Enter password: 
ERROR 2003 (HY000): Can`t connect to MySQL server on '192.168.31.177' (113)
[root@localhost ~]#
[root@localhost ~]#telnet  192.168.31.177 3306        #没tel上,3306端口没开
Trying 192.168.31.177...
telnet: connect to address 192.168.31.177: No route to host
[root@localhost ~]#telnet  192.168.31.177 27017   #相反地,我的mongo运行得很好
Trying 192.168.31.177...
Connected to 192.168.31.177.
Escape character is '^]'.
^]
telnet> quit
Connection closed.
[root@atom ~]#systemctl stop firewalld.service    #关了mysql服务器端的防火墙,后面添加放行
[root@localhost ~]#telnet  192.168.31.177 3306		#3306通了
Trying 192.168.31.177...
Connected to 192.168.31.177.
Escape character is '^]'.
R
5.5.68-MariaDBL5ct>w[
[root@localhost ~]#mysql -utonixtom -h192.168.31.177  -p      #远程连接成功
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.

最后发现大多数结果都是防火墙的问题

sudo firewall-cmd --add-port=3306/tcp --permanent					#放行3306端口	
sudo firewall-cmd --reload

未开放端口

[root@localhost ~]#mysql -utonixtom -h192.168.31.177 -p
Enter password: 
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.31.177' (113)

在这里插入图片描述

权限设置问题

这里我弄错了通配符释意,以为*代表多主机,mysql里%代表区间内任意网段,切记。
在这里插入图片描述

MariaDB [mysql]> CREATE USER 'tonixtom'@'192.168.31.*' IDENTIFIED  BY '123456';		
MariaDB [mysql]> select user,host,plugin from mysql.user;  
+----------+--------------+--------+
| user     | host         | plugin |
+----------+--------------+--------+
| root     | localhost    |        |
| root     | atom.2ll     |        |
| root     | 127.0.0.1    |        |
| root     | ::1          |        |
|          | localhost    |        |
|          | atom.2ll     |        |
| tonixtom | 192.168.31.* |        |      #这是错误的设置
+----------+--------------+--------+


[root@atom ~]#mysql -utonixtom -h192.168.31.177  -p		#会权限不匹配
Enter password: 
ERROR 1130 (HY000): Host '192.168.31.177' is not allowed to connect to this MariaDB server

删除匿名用户

有时候匿名用户可能引发一些莫名的问题,可以提前作删除处理。

MariaDB [mysql]> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+----------------------------------+
| query                            |
+----------------------------------+
| User: 'root'@'127.0.0.1';        |
| User: 'tonixtom'@'192.168.31.%'; |
| User: 'root'@'::1';              |
| User: ''@'atom.2ll';             |  #这个就是 
| User: 'root'@'atom.2ll';         |
| User: 'root'@'localhost';        |
+----------------------------------+
MariaDB [mysql]> REVOKE ALL PRIVILEGES ON *.* FROM ''@'localhost';      #回收权利 
MariaDB [mysql]> drop user ''@'localhost';						#先回收后删除
MariaDB [mysql]> flush privileges;

#简单小的建表sql
create table test_user (    
	id int(20) not null primary key,
	name varchar(30) default null,
	pwd varchar(30) default null
) engine=innodb default charset=utf8;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在CentOS 7上安装MariaDB之后,如果你想继续安装MySQL,你需要进行一些额外的步骤。安装MySQL的过程与安装MariaDB略有不同。以下是在CentOS 7上安装MySQL的一般步骤: 1. 首先,确保你已经卸载了之前安装MariaDB。你可以使用如下命令卸载MariaDB: ``` sudo yum remove mariadb-libs ``` 2. 安装MySQL的依赖包。在终端中运行以下命令: ``` sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm ``` 3. 安装MySQL服务器。运行以下命令: ``` sudo yum install -y mysql-server ``` 4. 启动MySQL服务并设置开机自启动: ``` sudo systemctl start mysqld sudo systemctl enable mysqld ``` 5. 运行MySQL安全脚本,该脚本将帮助你设置MySQL的root密码等安全选项: ``` sudo mysql_secure_installation ``` 6. 完成安全脚本的设置后,你可以通过以下命令登录到MySQL: ``` mysql -u root -p ``` 请注意,在安装过程中可能还会有其他附加步骤和设置。具体步骤可能会根据你的系统配置和需求而有所不同。确保按照MySQL官方文档或适用于CentOS 7的特定指南进行操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [CentOS 7 安装mariadb](https://blog.csdn.net/xueren_83/article/details/124325803)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [centos 7安装mysql5.5安装 mariadb使用的命令](https://download.csdn.net/download/weixin_38606019/12830336)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值