本站使用了 Pjax 等基于 JavaScript 的开发技术,但您的浏览器已禁用 JavaScript,请开启 JavaScript 以保证网站正常显示!

CentOS7 yum安装MariaDB

前言

之前学习使用Net-snmp获取目标设备SNMP信息,考虑到后面进一步学习需要对数据进行存储,之前考虑使用xml文件进行存储,对于以后系统的开发、运行效率以及后期维护上来说确实是个问题,所以考虑后决定还是使用数据库来存储,这样比较合理。

废话不多说,下面就在之前安装Net-snmpCentOS上安装数据库吧。

安装

数据库选择了MariaDB,其实就是MySQL了,这个不必多介绍了。

YUM安装MariaDB

#yum -y install mariadb mariadb-server

静静地等待完成。。。。。完成后启动服务并配置开机自启动。

# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
# systemctl start mariadb

MariaDB初始化配置

# 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] -->是否设置root密码,直接回车
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] -->移除匿名用户,这个为了安全起见,建议还是移除
... 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] -->禁止root远程登录,这个根据实际情况吧,当然最好禁止,启用其他自定义用户远程连接。
... Success!
 
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] -->删除测试数据库
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] -->重新加载权限表
... 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!

完成初始化配置。

配置MariaDB字符集
编辑文件 /etc/my.cnf

   #vim /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

编辑文件/etc/my.cnf.d/client.cnf

#vim /etc/my.cnf.d/client.cnf
 
#在[client]中添加
default-character-set=utf8

全部配置完成,重启mariadb

转载请先获得授权

 #systemctl restart mariadb

之后进入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.01 sec)

这样基本配置就完成了。
添加用户,设置权限

创建用户命令:
mysql>create user username@localhost identified by 'password';
 
直接创建用户并授权的命令:
mysql>grant all on *.* to username@localhost identified by 'password';
 
授予外网登陆权限:
mysql>grant all privileges on *.* to username@'%' identified by 'password';
 
授予权限并且可以授权:
mysql>grant all privileges on *.* to username@'hostname' identified by 'password' with grant option;

简单的用户和权限配置基本就这样了。

其中只授予部分权限把 其中 all privileges或者all改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。

基本的SQL语句
下面介绍下基本的SQL语句。
show databases; ##显示数据库

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

use mysql; ##进入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
 
Database changed
MariaDB [mysql]>
 
show tables; ##显示当前库中表的名称
 
MariaDB [mysql]> show tables
-> ;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)

select * from user; ##查询user表中的所有内容

MariaDB [mysql]> select User,Password from user;
+------+-------------------------------------------+
| User | Password |
+------+-------------------------------------------+
| root | *4A488726AE5A0B0F0DB967998EE12D87F25C9610 |
| root | *4A488726AE5A0B0F0DB967998EE12D87F25C9610 |
| root | *4A488726AE5A0B0F0DB967998EE12D87F25C9610 |
+------+-------------------------------------------+
3 rows in set (0.00 sec)

desc user; ##查询user表的结构

MariaDB [mysql]> desc user;
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) | NO | | 0 | |
| plugin | char(64) | NO | | | |
| authentication_string | text | NO | | NULL | |
+------------------------+-----------------------------------+------+-----+---------+-------+
42 rows in set (0.01 sec)

总结

上面是整个MariaDB的安装和配置过程,正好最近博客需要跟换主机,所以环境也需要重新搭建,这个就作为一个手册,方便后面的学习和环境搭建。对于数据我也只是入门小白,后期的学习肯定还会着重学习这一方面内容。

请保留出处


 继续浏览关于 的文章

 本文最后更新于 2019/04/22 14:58:33,可能因经年累月而与现状有所差异

 引用转载请注明:咬定面包不放松 > IT > CentOS7 yum安装MariaDB

您直接访问了本站,莫非记住了域名?