【Mariadb数据库安装及配置】

数据库运维
部署环境(数据库安装、参数配置、权限分配),备份/还原,监控,故障处理,性能优化,容灾,升级/迁移,系统用户反馈的数据库问题。
数据库运维工作原则
1.能不给数据库做的事情就不要给数据库,数据库只做数据容器。
2.对于数据库的变更必须有记录,可以回滚。

一、MYSQL数据库

1.DBMS数据库管理系统:增删改查数据
关系型数据库:二维表:数据存储有行和列
关系型数据库有哪些?ORACLE(1521) MYSQL/Mariadb(3306) SQLSERVER(1433) DB2

2.概念模型:
实体:客观存在的 、可以被描述的事物
属性:用于描述实体的属性
关系:实体之间的联系(一对一 一对多 多对多)
行:记录
列:属性
主键和外键

3.MYSQL体系结构:Mysql是由连接池,SQL接口,解析器,优化器,缓存,存储引擎组成的。
Connetcors:不同语言中与SQL的交互
Management Services & Utilities:系统管理和控制工具
Connection Pool:连接池,管理缓冲用户连接,线程处理等需要缓存的需求
SQL Interface:SQL接口,接收用户的SQL命令,并且返回用户需要查询的结果
Parser:解析器。SQL命令产地到解析器的时候会被解析器验证和解析
Optimizer:查询优化器。SQL语句在查询之前会使用查询优化器对查询进行优化
Cache和Buffer:查询缓存。如果查询缓存有命中的查询结果,查询语句就可以直接去查询缓存中取数据
Enigne:存储引擎。存储引擎是MYSQL中具体的与文件大交通的子系统

#查询连接数:
MariaDB [mysql]> show variables like '%max_connections%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| extra_max_connections | 1     |
| max_connections       | 151   |
+-----------------------+-------+
2 rows in set (0.002 sec)
#查询缓存
MariaDB [mysql]> show variables like '%query_cache%';
+------------------------------+---------+
| Variable_name                | Value   |
+------------------------------+---------+
| have_query_cache             | YES     |
| query_cache_limit            | 1048576 |
| query_cache_min_res_unit     | 4096    |
| query_cache_size             | 1048576 |
| query_cache_strip_comments   | OFF     |
| query_cache_type             | OFF     |
| query_cache_wlock_invalidate | OFF     |
+------------------------------+---------+
7 rows in set (0.002 sec)

在这里插入图片描述
4.MariaDB 10.4的安装和配置
环境准备:Centos7.3以后,最小化安装,安装完毕之后禁用selinux,关闭防火墙。
安装常用软件:vim wget bash-completion net-tools
1)安装:有两种方法:网络源、本地源

1.网络源:安装的版本新	10.
[root@manager yum.repos.d]# vim mariadb.repo 
[mariadb]
name=mariadb-10.4
baseurl=http://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos7-amd64/
gpgcheck=0
[root@manager yum.repos.d]# rpm -q MariaDB-server
MariaDB-server-10.4.13-1.el7.centos.x86_64

2.本地光盘作为yum源:安装的版本旧 5.
[root@manager ~]# yum install mariadb-server -y

主配置文件:/etc/my.cnf
子配置文件目录:/etc/my.cnf.d

[root@manager ~]# rpm -qc MariaDB-server
/etc/my.cnf.d/enable_encryption.preset
/etc/my.cnf.d/server.cnf
/etc/security/user_map.conf
[root@manager ~]# cat /etc/my.cnf #主配置文件
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include *.cnf from the config directory
#
!includedir /etc/my.cnf.d  #子配置文件

2)启动服务
systemctl start mariadb
systemctl enable mariadb
等价于:systemctl enable --now mariadb (启动加开机自启动)

验证服务是否运行?查看端口、查看服务状态

a.查看端口
[root@manager ~]# netstat -ltunp | grep :3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      38251/mysqld    
b.查看服务状态
[root@manager ~]# systemctl is-active mariadb  #是否开启
active
[root@manager ~]# systemctl is-enabled mariadb #是否开机自启
enabled

3).初始化安全设置:mysql_secure_installation
设置root密码
禁用root远程登录
移除匿名用户
移除test数据库

[root@manager ~]# mysql_secure_installation

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

In order to log into MariaDB to secure it, we'll need the c
password for the root user. If you've just installed MariaD
haven't set the root password yet, you should just press en

Enter current password for root (enter for none):   #当前密码为空,回车即可
OK, successfully used password, moving on...

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n  #是否设置安全套接字认证:否
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y     #是否修改root密码:yes
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          #是否移除匿名用户:yes
 ... 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] y   #是否不允许远程root登录:yes
 ... 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] y    #是否移除测试数据库:yes
 - 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] y     #是否现在加载权限表:yes
 ... 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!

测试密码:mysql -uroot -p123456

[root@manager ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.4.13-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)]> 

关于密码的设置:修改密码,重置密码
修改密码:
1.没有密码设置密码:
mysqladmin -uroot password “123456”
2.修改密码:
mysqladmin -uroot -p"123456" password “pineyang000”

[root@manager ~]# mysqladmin -uroot -p password
Enter password:    #输入旧密码
New password: 	   #输入新密码
Confirm new password:   #再次输入新密码
[root@manager ~]# mysql -uroot -ppineyang000  #测试新密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.4.13-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)]> 		

3.登录mysql数据库后修改密码:
[root@kongd ~]# mysql -uroot -p旧密码
方法1:alter user root@‘localhost’ identified by ‘123456’; // 5.5版本不支持
方法2:set password for root@localhost = password(‘123456’);
方法3:不建议
MariaDB [(none)]> update mysql.user
-> set password=password(‘123456’)
-> where User=“root” and Host=“localhost”;
MariaDB [(none)]> flush privileges;
重置密码:
以下是10.4重置方式:
1.跳过权限表:mysqld --skip-grant-tables --user=mysql 这时候这个会话不能使用,我们则需要重新复制个会话进入数据库
(前提停止数据库:systemctl stop mariadb)

[root@manager ~]# mysqld --skip-grant-tables
2020-06-18 12:32:01 0 [Note] mysqld (mysqld 10.4.13-MariaDB) starting as process 5550 ...
mysqld: Please consult the Knowledge Base to find out how to run mysqld as root!
2020-06-18 12:32:01 0 [ERROR] Aborting
[root@manager ~]# mysqld --skip-grant-tables
2020-06-18 12:32:31 0 [Note] mysqld (mysqld 10.4.13-MariaDB) starting as process 5575 ...
mysqld: Please consult the Knowledge Base to find out how to run mysqld as root!
2020-06-18 12:32:31 0 [ERROR] Aborting
[root@manager ~]# mysqld --skip-grant-tables --user=mysql
2020-06-18 12:33:24 0 [Note] mysqld (mysqld 10.4.13-MariaDB) starting as process 5618 ...
2020-06-18 12:33:24 0 [Note] InnoDB: Using Linux native AIO
2020-06-18 12:33:24 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-06-18 12:33:24 0 [Note] InnoDB: Uses event mutexes
2020-06-18 12:33:24 0 [Note] InnoDB: Compressed tables use zlib 1.2.7
2020-06-18 12:33:24 0 [Note] InnoDB: Number of pools: 1
2020-06-18 12:33:24 0 [Note] InnoDB: Using SSE2 crc32 instructions
2020-06-18 12:33:24 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2020-06-18 12:33:24 0 [Note] InnoDB: Completed initialization of buffer pool
2020-06-18 12:33:24 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2020-06-18 12:33:24 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2020-06-18 12:33:24 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2020-06-18 12:33:24 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2020-06-18 12:33:24 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2020-06-18 12:33:24 0 [Note] InnoDB: Waiting for purge to start
2020-06-18 12:33:24 0 [Note] InnoDB: 10.4.13 started; log sequence number 261916; transaction id 475
2020-06-18 12:33:24 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2020-06-18 12:33:24 0 [Note] InnoDB: Buffer pool(s) load completed at 200618 12:33:24
2020-06-18 12:33:24 0 [Note] Plugin 'FEEDBACK' is disabled.
2020-06-18 12:33:24 0 [Note] Server socket created on IP: '::'.
2020-06-18 12:33:24 0 [Note] Reading of all Master_info entries succeeded
2020-06-18 12:33:24 0 [Note] Added new Master_info '' to hash table
2020-06-18 12:33:24 0 [Note] mysqld: ready for connections.

2.复制会话进入数据库:只需要输入mysql就可以无密码进入数据库

root@manager ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.13-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)]> 

3.占用mysql数据库:use 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

4.刷新权限表:flush privileges

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

5.重置密码操作:set password for root@localhost=password(‘123456’);

MariaDB [mysql]> set password for root@localhost=password('123456');
Query OK, 0 rows affected (0.003 sec)

验证:
1.首先停止mysql进程(因为之前跳过权限表时那个会话一直卡着,则就需要杀死进程):killall -9 mysqld,直到出现no process found(需要安装yum install psmisc -y)

[root@manager ~]# yum install psmisc -y
已加载插件:fastestmirror
Repository base is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
软件包 psmisc-22.20-16.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@manager ~]# killall -9 mysqld
[root@manager ~]# killall -9 mysqld
mysqld: no process found
#之前卡的那个会话就会出现
.
Version: '10.4.13-MariaDB'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server
已杀死

2.再开启mysql数据库服务:systemctl restart mysqld,使用重置后的密码进入数据库进行验证

[root@manager ~]# systemctl start mariadb
[root@manager ~]# mysql -u root -p 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.13-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)]> \q
Bye

#注意:5.5版本进行重置root密码操作如下:
1) 停止数据库
[root@kongd ~]# systemctl stop mariadb
2) 启动时加上–skip-grant-tables 跳过权限表
[root@kongd ~]# mysqld_safe --skip-grant-tables --user=mysql
3) 登录数据库,修改密码
[root@kongd ~]# mysql
MariaDB [(none)]> update mysql.user
-> set password=password(‘123’)
-> where User=“root” and Host=“localhost”;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

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

5.数据库如何登录?
命令行:[root@kongd ~]# mysql -u root -p123456 -D mysql -h localhost
-u:用户名
-p:密码,注意-p和密码不能有空格
-D:数据库名
-h:主机
windows下的客户端(GUI):Navicat for MySQL/SQLyog
在数据库里授权一个用户:grant all on . to admin@’%’ identified by ‘123456’;

MariaDB [(none)]> grant all on *.* to admin@'%' identified by '123456';
Query OK, 0 rows affected (0.001 sec)

Navicat for MySQL远程连接就可以远程操作数据库:
在这里插入图片描述
6.使数据库支持中文:设置服务器默认字符集
1)vim /etc/my.cnf.d/server.cnf 设置
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

[root@manager my.cnf.d]# vim server.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

2)重启服务:

[root@manager my.cnf.d]# systemctl restart mariadb

测试:进入数据库输入:
show variables like ‘character%’;
show variables like ‘collation%’; 查看是否为UTF-8

[root@manager my.cnf.d]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.13-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)]> show variables like 'character%';
+--------------------------+----------------------------+
| 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.001 sec)

MariaDB [(none)]> show variables like 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.003 sec)

注:若是这种方法不行,也就是说给数据库插入中文时出错,就可以使用下面两种办法:

1.修改数据库字符格式为utf-8
alter table 表名 convert to character set utf8;
2.创建表时后面跟上ENGINE=InnoDB DEFAULT CHARSET=utf8
比如:create table score (id int(10) primary key not null unique auto_increment,stu_id int(10) not null,c_name varchar(20),grade int(10)) innodb default charset=utf-8;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑着蜗牛追汤圆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值