Centos 7安装Nginx+PHP+MariaDB环境搭建WordPress博客

WordPress是一个免费的开源项目,是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可以把 WordPress当作一个内容管理系统(CMS)来使用,国内外有不少的知名网站建设都是基于WordPress程序,WordPress有许多第三方开发的免费模板和免费插件,安装方式简单易用,相信很多人的第一个站点都是基于WordPress建的。访问官网https://wordpress.org/

在安装ShadowsocksR服务之前希望先对服务器做基本安全配置(非强制) 跳转链接https://www.gyuryong.com/index.php/archives/18/

运行环境搭建

WordPress基于PHP开发的,相信是大家最熟悉也是最容易部署的Web项目了。环境准备:lnmp(linux+nginx+mysql+php)或者lamp(linux+apache+mysql+php),大同小异,本文推荐使用nginx作为Web服务器。
为了避免不必要的麻烦先关闭防火墙和selinux。

1.安装nginx

安装nginx,默认情况Centos7中无Nginx的源,可以如下执行命令添加源,Centos其他版本或者RHEL查看官方教程(教程链接https://www.nginx.com/resources/wiki/start/topics/tutorials/install/):

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

写入

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

安装nginx:

yum install nginx -y

2.安装php和mariadb

yum install php-fpm php-mysql mariadb-server unzip

3.修改配置文件

修改/etc/nginx/conf.d/default.conf中下面两断内容:

vi /etc/nginx/conf.d/default.conf

更改前:

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

更改后:

    root   /usr/share/nginx/html;
    index  index.html index.htm index.php;
location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

更改前:

#location ~ .php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

更改后:

location ~ .php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $request_filename;
    include        fastcgi_params;
}

修改/etc/php-fpm.d/www.conf配置:

vi /etc/php-fpm.d/www.conf

user = apache改为user = nginx,将group = apache改为group = nginx

4.开启服务

systemctl start nginx.service
systemctl start mariadb.service
systemctl start php-fpm.service

5.设置开机自启

systemctl enable nginx mariadb php-fpm

安装WorePress

1.移除/usr/share/nginx/html内所有文件:

cd /usr/share/nginx/html
rm 50x.html index.html

2.下载WordPress并解压,到官网复制最新版链接(跳转链接https://cn.wordpress.org/download/):

yum install wget -y
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.zip
unzip wordpress-4.9.4-zh_CN.zip

3.将Web文件移动到根目录并删除没用文件夹:

mv wordpress/* .
rmdir wordpress
rm wordpress-4.9.4-zh_CN.zip

4.权限设置

chown nginx.nginx -R .

5.创建数据库wordpress:

mysql
create database wordpress;
exit

接下来输入你的ip地址就可以安装WordPress了!

MySQL选型以及使用mariadb踩过的几个坑

MySQL新版本选型

公司早期主要用mysql5.5这个版本,今年我们把数据库配置中心搭建起来,主要推的是mysql5.6这个版本,性能和功能上都有了一定的提升,mysql5.6也能支持gtid,但是无法在线在gtid模式与普通模式之间切换,同时5.6的同步性能还是无法让人满意,只能做到在多个db的情况启动并行复制,业务上很难有这样的保证,所以一旦写操作密集的业务,同步慢就会是个严重的问题;

所以,最近一直在考虑升级MySQL,升级MySQL首先面临的一个问题就是选一个合适的版本,首先我们考虑是的采用mysql5.7,5.7今年已经连续发了多个正式版本了,目前使用范围也比较广,可以考虑在正式环境使用了。于是我们进行了线上对比测试,发现mysql5.7与我们线上的mysql5.6版本的性能有较大的差距(也许还是有些参数没有调好的原因,5.7确实要复杂很多)。

与此同时,最近公司频繁出现一些日志性存储,大多数都是采用innodb引擎,容量非常浪费,另一方面由于我们公司的标准mysql服务器容量是1.3T左右,对于一些大容量需求的业务来说,容量上也存在瓶颈,如果要保留长时间的数据就难以满足需求了。所以借此机会我们打算把这块一起考虑进去,目前Percona和Mariadb都支持Tokudb,Mariadb 10.2还是10.3也准备支持Myrocks了。

于是决定对比试一下,选择了Percona 5.7.14、Mariadb 10.1.18与我们线上的MySQL 5.6进行了对比测试,经过压测。

首先是都使用Innodb引擎的情况:

Mariadb与MySQL5.6测试结果接近

Percona 5.7.14与官方MySQL5.7的性能结果差不多,比起MySQL5.6有一定的差距(去掉performance_schema好一点,但是也有差距)。

采用Tokudb引擎测试的结果与官方声称的有差距,使用snappy压缩的情况下,insert比innodb约慢1/4,update只有innodb的一半左右。Percona性能更差,就不考虑了。

最终选型Mariadb 10.1.18,并且在线上部署了一个业务,通过这个业务慢慢试用以后,逐步推广开来。

使用Mariadb踩到的一些坑

在使用Mariadb的过程中,碰到了不少问题,这里主要提一下我碰到的两个较大的问题,供大家参考:

1、同步性能问题:

我们上的这个业务高峰期达到了9000多写操作/秒,首先面临的第一个问题就是同步性能跟不上,slave同步线程数加到了16个线程,勉强能追上,但是一旦数据库从库停一会,就有可能面临永远最不上的可能。当快绝望的时候,看了一下mariadb的官方文章(https://mariadb.com/kb/en/mariadb/parallel-replication/),Mariadb的并行复制支持好几种模式,其中有in-order和out-of-order两种,不过我们这个业务支持in-order,所以没考虑out-of-order,在in-order模式下,又支持两种:Conservative 和 Optimistic,缺省情况下Conservative ,这种并行模式会严格保证事物的顺序性,估计和5.7的group commit原理差不多;而Optimistic模式下,复制的时候会尽量启动更多的会话,直到发现冲突时才会去处理冲突。果断试了一下Optimistic,非常强劲,最高同步速度达到了14000次/秒。

2、”内存泄露”

系统部署结构为:两个Mariadb做成主主复制,在前面部署了一个自己开发的分布式数据库,业务方连接到分布式数据库进程。系统上线了几天,发现主库会莫名其妙的挂掉,好在有分布式数据库,并且会自动切换,Mariadb主库挂了,会自动切到另外一个主库上,业务方没有感知。查看内核日志,发现是OOM了,内核把MySQL杀掉了。

于是开始了各种尝试,去掉Tokudb引擎配置,换Mariadb 10.1.19 ,都尝试过,最终都会发生主库挂掉的事情。一次偶然的机会,我把主库上的slave停掉了,发现主库的内存突然下降好多,并且内存不再增加了,但是一旦把主库上的slave启动就会发现,内存又逐渐身高。这种现象很像mysql线程内的内存分配机制造成的(基于mem_root的内存分配,线程停掉会全部释放),所以初步怀疑是这个原因造成的。发现作为双主中的另外一个Mariadb,就不会出现内存上涨的问题。

发现上面的现象以后,就开始代码上的调试,用gdb启动一个mariadb,另外一个用普通命令启动,这两个库做成双主:

第一种情况:测试作为从库的时候,接收到的binlog事件情况

在普通命令启动的mariadb上插入一行数据,gdb查看接收到的事件的顺序如下:

### i ) Gtid_log_event

### ii) Table_map_log_event

### iii) Write_rows_log_event

### iv) Xid_log_event

第二种情况:测试作为主库的时候,接收到的binlog事件

在gdb启动的mariadb上插入一行记录,然后gdb观察接收到的事件为:

### 1)Rotate_log_event

### 2)Gtid_list_log_event

### 3)Rotate_log_event

Rotate_log_event事件是虚拟出来的,用于让主库跟上从库的同步位置,这基本上是一个空事件,没有做任何处理,所以初步怀疑是在处理Gtid_list_log_event事件的时候,出现了问题。

反复查看Gtid_list_log_event::do_appy_event函数中的调用情况,发现确实有些方法会调用thd->alloc来分配内存,但是没有回收,所以造成内存不断的增大,我考虑了一下,因为是主库对于同步性能要求也不高,所以在Gtid_list_log_event::do_apply_event函数的最后加了一行代码:free_root(thd->mem_root, MYF(MY_KEEP_PREALLOC));  重新编译后,跑了一天,内存终于稳定了。

由于目前发现只有主库有该事件,主库同步处理性能要求不高,所以暂时先这样用着了。不知道mariadb官方版本什么时候会优化一下。

总体来看,Mariadb还是比较适合我们公司的,它有最新的功能、特性能够给我们提供很多解决方案。Tokudb可以解决日志型存储的问题;连接池可以解决大量连接情况下性能地下的问题;审计插件提供安全方面的审核;slave并发模式能够提供高性能的复制能力。除了这些常见功能以外,Mariadb还提供了Cassandra插件、图数据库插件等等,这些都给我们给业务的服务增加了想象力。

CentOS 7.x yum 方式安装 MariaDB(MySQL)

MariaDB 是 MySQL 的一个分支,采用 GPL 授权许可证,目前主要由开源社区在维护。MariaDB的目的是完全兼容MySQL,包括 API 和命令行,是之能轻松成为 MySQL 的替代品。

通过 yum 方式安装 MariaDB 非常简单,执行如下命令即可。

# yum install mariadb-server mariadb
# rpm -q mariadb mariadb-server
mariadb-5.5.56-2.el7.x86_64
mariadb-server-5.5.56-2.el7.x86_64

安装完成后,将 MariaDB 设置为开机启动,操作如下:

# systemctl enable mariadb

输出类似如下:

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

接着,启动 MariaDB,执行如下命令:

# systemctl start mariadb

最后,登录 MariaDB,执行如下命令:

# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

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

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

MariaDB [(none)]>

从这里可以看到,在 MariaDB 中,默认的登录方式跟 MySQL 数据库一样,root 密码默认为空。
对 MariaDB 服务进行管理,可以通过 systemctl 命令实现。例如:

关闭 MariaDB 

# systemctl stop mariadb

重启 MariaDB 

# systemctl restart  mariadb

检查 MariaDB 服务运行状态

# systemctl status  mariadb

MariaDB 通过内置的安全脚本可实现对数据库的安全保护,执行“/usr/bin/mysql_secure_installation”命令,加固 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):  // 这里输入目前 MariaDB 数据库的 root 密码,默认为空
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      // 这里询问是否是之 root 密码,输入 y  给 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] y  //这里询问是否删除 anonymous 用户,输入 "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] y   // 这里询问是否关闭 root 用户远程登录权限 输入 "Y"
 ... 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  // 这里询问是否删除测试数据库及其权限 输入 "Y"
 - 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   // 这里询问是否重新载入授权表 输入 "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!

至此,MariaDB 数据库安转完成了。

CentOS7 平滑升级 MariaDB 5.5 到 10.x 新版本实践

1. 前言

自从 CentOS 7 开始,自带的数据库就变成 MariaDB 了,yum 安装之后的默认版本是 5.5,但是这个数据版本已经比较老了,无论是安装全新的 Percona 还是升级 MariaDB 第一步始终是不要忘记备份。

2. 备份数据库

重要的事情说三遍,备份,备份,备份

# 备份数据库,如果升级顺利是不要实施备份还原的 
mysqldump -u root -p --all-databases > alldb.sql
# 如果想保留自己的 my.cof 配置,则备份一下这个文件 
cp /etc/my.cnf /etc/my.cnf.bak
# 停止数据库运行 
systemctl stop mariadb
# 卸载 MariaDB 老版本 
yum remove mariadb mariadb-server

3. 添加 MariaDB Yum 库

建议使用 MariaDB 官方推荐的 stable 稳定版,

https://downloads.mariadb.org/mariadb/
http://yum.mariadb.org/

# 添加 MariaDB 官方源 
vi /etc/yum.repos.d/MariaDB.repo

# MariaDB 10.3 CentOS repository list
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

# 清除 yum 缓存 
yum clean all 
yum makecache

4. 升级已有数据库

# 安装 MariaDB 新版本 
yum install mariadb mariadb-server

# 启动新版 MariaDB
systemctl start mariadb

# 升级已有数据库 
mysql_upgrade -uroot -p 

[root@sg-gop-10-71-12-89 tmp]# mysql_upgrade -uroot -p
Enter password:
Phase 1/7: Checking and upgrading mysql database
Processing databases
mysql
mysql.columns_priv                                 OK
mysql.db                                           OK
mysql.event                                        OK
mysql.func                                         OK
mysql.help_category                                OK
mysql.help_keyword                                 OK
mysql.help_relation                                OK
mysql.help_topic                                   OK
mysql.host                                         OK
mysql.ndb_binlog_index                             OK
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv                                 OK
mysql.servers                                      OK
mysql.tables_priv                                  OK
mysql.time_zone                                    OK
mysql.time_zone_leap_second                        OK
mysql.time_zone_name                               OK
mysql.time_zone_transition                         OK
mysql.time_zone_transition_type                    OK
mysql.user                                         OK
Upgrading from a version before MariaDB-10.1
Phase 2/7: Installing used storage engines
Checking for tables with unknown storage engine
Phase 3/7: Fixing views
Phase 4/7: Running 'mysql_fix_privilege_tables'
Phase 5/7: Fixing table and database names
Phase 6/7: Checking and upgrading tables
Processing databases
information_schema
performance_schema
zabbix
zabbix.acknowledges                                OK
zabbix.actions                                     OK
zabbix.alerts                                      OK
zabbix.application_discovery                       OK
zabbix.application_prototype                       OK
zabbix.application_template                        OK
zabbix.applications                                OK
zabbix.auditlog                                    OK
zabbix.auditlog_details                            OK
zabbix.autoreg_host                                OK
zabbix.conditions                                  OK
zabbix.config                                      OK
zabbix.corr_condition                              OK
zabbix.corr_condition_group                        OK
zabbix.corr_condition_tag                          OK
zabbix.corr_condition_tagpair                      OK
zabbix.corr_condition_tagvalue                     OK
zabbix.corr_operation                              OK
zabbix.correlation                                 OK
zabbix.dashboard                                   OK
zabbix.dashboard_user                              OK
zabbix.dashboard_usrgrp                            OK
zabbix.dbversion                                   OK
zabbix.dchecks                                     OK
zabbix.dhosts                                      OK
zabbix.drules                                      OK
zabbix.dservices                                   OK
zabbix.escalations                                 OK
zabbix.event_recovery                              OK
zabbix.event_suppress                              OK
zabbix.event_tag                                   OK
zabbix.events                                      OK
zabbix.expressions                                 OK
zabbix.functions                                   OK
zabbix.globalmacro                                 OK
zabbix.globalvars                                  OK
zabbix.graph_discovery                             OK
zabbix.graph_theme                                 OK
zabbix.graphs                                      OK
zabbix.graphs_items                                OK
zabbix.group_discovery                             OK
zabbix.group_prototype                             OK
zabbix.history                                     OK
zabbix.history_log                                 OK
zabbix.history_str                                 OK
zabbix.history_text                                OK
zabbix.history_uint                                OK
zabbix.host_discovery                              OK
zabbix.host_inventory                              OK
zabbix.hostmacro                                   OK
zabbix.hosts                                       OK
zabbix.hosts_groups                                OK
zabbix.hosts_templates                             OK
zabbix.housekeeper                                 OK
zabbix.hstgrp                                      OK
zabbix.httpstep                                    OK
zabbix.httpstep_field                              OK
zabbix.httpstepitem                                OK
zabbix.httptest                                    OK
zabbix.httptest_field                              OK
zabbix.httptestitem                                OK
zabbix.icon_map                                    OK
zabbix.icon_mapping                                OK
zabbix.ids                                         OK
zabbix.images                                      OK
zabbix.interface                                   OK
zabbix.interface_discovery                         OK
zabbix.item_application_prototype                  OK
zabbix.item_condition                              OK
zabbix.item_discovery                              OK
zabbix.item_preproc                                OK
zabbix.items                                       OK
zabbix.items_applications                          OK
zabbix.maintenance_tag                             OK
zabbix.maintenances                                OK
zabbix.maintenances_groups                         OK
zabbix.maintenances_hosts                          OK
zabbix.maintenances_windows                        OK
zabbix.mappings                                    OK
zabbix.media                                       OK
zabbix.media_type                                  OK
zabbix.opcommand                                   OK
zabbix.opcommand_grp                               OK
zabbix.opcommand_hst                               OK
zabbix.opconditions                                OK
zabbix.operations                                  OK
zabbix.opgroup                                     OK
zabbix.opinventory                                 OK
zabbix.opmessage                                   OK
zabbix.opmessage_grp                               OK
zabbix.opmessage_usr                               OK
zabbix.optemplate                                  OK
zabbix.problem                                     OK
zabbix.problem_tag                                 OK
zabbix.profiles                                    OK
zabbix.proxy_autoreg_host                          OK
zabbix.proxy_dhistory                              OK
zabbix.proxy_history                               OK
zabbix.regexps                                     OK
zabbix.rights                                      OK
zabbix.screen_user                                 OK
zabbix.screen_usrgrp                               OK
zabbix.screens                                     OK
zabbix.screens_items                               OK
zabbix.scripts                                     OK
zabbix.service_alarms                              OK
zabbix.services                                    OK
zabbix.services_links                              OK
zabbix.services_times                              OK
zabbix.sessions                                    OK
zabbix.slides                                      OK
zabbix.slideshow_user                              OK
zabbix.slideshow_usrgrp                            OK
zabbix.slideshows                                  OK
zabbix.sysmap_element_trigger                      OK
zabbix.sysmap_element_url                          OK
zabbix.sysmap_shape                                OK
zabbix.sysmap_url                                  OK
zabbix.sysmap_user                                 OK
zabbix.sysmap_usrgrp                               OK
zabbix.sysmaps                                     OK
zabbix.sysmaps_elements                            OK
zabbix.sysmaps_link_triggers                       OK
zabbix.sysmaps_links                               OK
zabbix.tag_filter                                  OK
zabbix.task                                        OK
zabbix.task_acknowledge                            OK
zabbix.task_check_now                              OK
zabbix.task_close_problem                          OK
zabbix.task_remote_command                         OK
zabbix.task_remote_command_result                  OK
zabbix.timeperiods                                 OK
zabbix.trends                                      OK
zabbix.trends_uint                                 OK
zabbix.trigger_depends                             OK
zabbix.trigger_discovery                           OK
zabbix.trigger_tag                                 OK
zabbix.triggers                                    OK
zabbix.users                                       OK
zabbix.users_groups                                OK
zabbix.usrgrp                                      OK
zabbix.valuemaps                                   OK
zabbix.widget                                      OK
zabbix.widget_field                                OK
Phase 7/7: Running 'FLUSH PRIVILEGES'
OK

# 配置服务自启动 
systemctl enable mariadb

# 登录数据库验证 
mysql -uroot -p

# 升级过程遇到错误记得先查看日志分析 

5. MariaDB 官方升级文档

Upgrading from MariaDB 5.5 to MariaDB 10.0

https://mariadb.com/kb/en/library/upgrading-from-mariadb-55-to-mariadb-100/

MariaDB修改最大连接数

1.mariadb数据库最大连接数,默认为151

MariaDB [(none)]> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections |  151  |
+-----------------+-------+

2.配置/etc/my.cnf

[mysqld]下新添加一行如下参数:

max_connections=3000

systemctl restart mariadb 重启mariadb服务,再次查看mariadb数据库最大连接数,最大连接数是214,并非我们设置的3000。

MariaDB [(none)]> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections |  214  |
+-----------------+-------+

由于mariadb有默认打开文件数限制

vi /usr/lib/systemd/system/mariadb.service

取消[Service]前的#号,
[Service]新添加两行如下参数:

LimitNOFILE=10000
LimitNPROC=10000

4.重新加载系统服务,并重启mariadb服务

systemctl --system daemon-reload
systemctl restart mariadb.service

再次查看mariadb数据库最大连接数,可以看到最大连接数已经是3000

MariaDB [(none)]> show variables like 'max_connections';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 3000  |

+-----------------+-------+

从数据库延迟同步主数据库;

Mysql (需5.6以上版本)延迟复制配置,通过设置Slave上的MASTER TO MASTER_DELAY参数实现:

CHANGE MASTER TO MASTER_DELAY = N; 

N为多少秒,该语句设置从数据库延时N秒后,再与主数据库进行数据同步复制

具体操作:

登陆到Slave数据库服务器

mysql>stop slave; 
mysql>CHANGE MASTER TO MASTER_DELAY = 600; 
mysql>start slave; 
mysql>show slave status G; 

查看SQL_Delay的值为600,表示设置成功。

注释:
SQL_Delay:一个非负整数,表示秒数,Slave滞后多少秒于master。
SQL_Remaining_Delay:当 Slave_SQL_Running_State 等待,直到MASTER_DELAY秒后,Master执行的事件,

此字段包含一个整数,表示有多少秒左右的延迟。在其他时候,这个字段是0。

【最新发布的MariaDB10.2.3支持延迟复制】

MariaDB修改编码格式

1、通过set修改value字段

2、登录MySQL,使用 show variables like ‘character%’;

3、查看当前编码格式

未分类

4、使用 set character_set_client = ‘utf8mb4’;

5、直接修改variable_name的value

未分类

Mariadb通过配置文件修改编码

1、登录MySQL,使用

SHOW VARIABLES LIKE 'character%';

查看当前使用的字符集,应该有好几个不是UTF-8格式。

2、要修改的配置文件位于 /etc/my.cnf.d目录下:

client.cnf

在[client]字段里加入

default-character-set=utf8

server.cnf

在[mysqld]字段里加入

character-set-server=utf8

systemctl restart mariadb 配置生效,修改后的效果如下:

未分类

3、修改字段编码格式:

4、use 库名

ALTER TABLE  表名 MODIFY `字段` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '备注';

CentOS 7安装MariaDB 10详解以及相关配置

第一步:添加 MariaDB yum 仓库

首先在CentOS操作系统中/etc/yum.repos.d/目录下添加 MariaDB 的YUM配置文件MariaDB.repo文件。

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

在该文件中添加以下内容保存:

[mariadb]

name = MariaDB

baseurl = http://yum.mariadb.org/10.2/centos7-amd64

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=1

第二步:安装 MariaDB

通过yum命令轻松安装 MariaDB。

yum install MariaDB-server MariaDB-client -y

MariaDB 安装完毕后,立即启动数据库服务守护进程。

systemctl start mariadb

设置 MariaDB 在操作系统重启后自动启动服务。

systemctl enable mariadb

查看 MariaDB 服务当前状态。

systemctl status mariadb

第三步:对 MariaDB 进行安全配置

通过以下命令进行安全配置,根据实际情况用Y/N回复以下问题:设置 MariaDB 的 root 账户密码,删除匿名用户,禁用 root 远程登录,删除测试数据库,重新加载权限表。

mysql_secure_installation

本人全都是选择了Y,然后按回车。

在配置完数据库的安全配置后,可以通过以下命令查看版本,确认 MariaDB已安装成功。

mysql –version

可以通过 MariaDB 命令行登录,然后对数据库进行sql查询操作。

mysql -u root -p

第四步:为 MariaDB 配置远程访问权限

在第三步中如果禁用 root 远程登录选择 Y 的话就不能在别的电脑通过navicat等工具连接到数据库,这时就需要给对应的 MariaDB 账户分配权限,允许使用该账户远程连接到MariaDB。可以输入以下命令查看账号信息:

select User, host from mysql.user;

root账户中的host项是localhost表示该账号只能进行本地登录,我们需要修改权限,输入命令:

GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘password’ WITH GRANT OPTION;        

修改权限。%表示针对所有IP,password表示将用这个密码登录root用户,如果想只让某个IP段的主机连接,可以修改为:

GRANT ALL PRIVILEGES ON *.* TO ‘root’@’192.168.71.%’ IDENTIFIED BY ‘my-new-password’ WITH GRANT OPTION;

最后别忘了:

FLUSH PRIVILEGES;

保存更改后,再看看用户账号信息:

这个时候发现相比之前多了一项,它的host项是%,这个时候说明配置成功了,我们可以用该账号进行远程访问了。

第五步:CentOS 7 开放防火墙端口

在第四步后如果还是不能远程连上数据库的话应该就是3306端口被防火墙拦截了,这时我们就需要关闭防火墙或者开放防火墙端口。

关闭防火墙:

systemctl stop firewalld.service            #停止firewall

systemctl disable firewalld.service        #禁止firewall开机启动

开放防火墙端口,开启后要重启防火墙:

firewall-cmd –zone=public –add-port=3306/tcp –permanent

firewall-cmd –reload

第六步:设置数据库字母大小写不敏感

vim /etc/my.cnf.d/server.cnf

在[mysqld]下加上】

lower_case_table_names=1

默认是等于0的,即大小写敏感。改成1就OK了。如果之前已经建了数据库要把之前建立的数据库删除,重建才生效。

第七步:设置MariaDB数据库默认编码

MariaDB的默认编码是latin1,插入中文会乱码,因此需要将编码改为utf8。

1.登录,使用以下命令查看当前使用的字符集,应该有好几个不是utf8格式。

SHOW VARIABLES LIKE ‘character%’;

2.修改的配置文件

vim /etc/my.cnf.d/client.cnf

在[client]字段里加入

default-character-set=utf8
vim /etc/my.cnf.d/server.cnf

在[mysqld]字段里加入

character-set-server=utf83

重启 MariaDB 配置生效。

systemctl restart mariadb

Amazon Linux安装Mariadb

|默认输入yum -y mariadb mariadb-server

提示No package mariadb , mariadb-server

第一步,创建repo

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

内容:

# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

保存,同时更新yum

yum clean all
yum update

最后再次运行:

yum install -y MariaDB-server MariaDB-client

搞定,安装完成

mariadb数据库冷备份实验

数据的重要性不言而喻,做好数据备份方能在数据损坏丢失时及时恢复

  • 冷备份
  • 热备份
  • 完全备份
  • 增量备份

冷备份即暂停Mariadb服务,用户不能读写访问数据库,直接拷贝整个数据文件

实验拓扑

未分类

实验准备

  • 两台CentOS 7.4 server
  • mariadb软件版本 mariadb-server-5.5.56-2.el7.x86_64
  • sql数据库脚本,一个用于生成测试数据库hellodb,一个是存储过程,批量增加10W条数据记录
    https://github.com/zhongchengling/linuxshell

实验步骤

172.16.8.71–DB1

yum install -y mariadb-server

修改/etc/my.cnf文件拆分innodb引擎的数据库文件

innodb_file_per_table = ON
systemctl start mariadb.service
git clone [email protected]:zhongchengling/linuxshell.git
cd linuxshell
mysql < hellodb_innodb.sql
mysql --database=hellodb < testlog.sql
  • 安装MariaDB软件,修改/etc/my.cnf配置文件,并启动服务
  • 从同性交友网站GitHub上克隆sql脚本,导入数据库hellodb和存储引擎
  • 调用call pro_testlog();导入十万条记录
MariaDB [hellodb]> show databases;
MariaDB [hellodb]> show tables;
MariaDB [hellodb]> call pro_testlog();
MariaDB [hellodb]> select count(*) from testlog;

未分类

systemctl stop mariadb.service

暂停数据服务,打包备份/var/lib/mysql下的所有数据文件到172.16.8.7

cd /var/lib/
tar -Jcvf data.tar.xz mysql
scp /etc/my.cnf [email protected]:/etc
scp data.tar.xz [email protected]:/root

数据备份完成,在172.16.8.71机器上进行数据的验证恢复

172.16.8.71–DB1

  • 同样地yum安装Mariadb,确保版本相同,以避免潜在问题
  • 解压数据文件到/var/lib/mysql
tar -xvf data.tar.xz -C /var/lib/mysql
  • 启动mairadb服务,验证数据

未分类

debian9安装mariadb apache php7

一、

apt install apache2

二、

apt install php (安装后测试phpinfo)
<?php
phpinfo();
?>

三、

apt install mariadb-server

1、安装完 MariaDB 之后,运行 mysql_secure_installation 可以设置root密码及删除测试账户之类的信息。

2、新建一个用户:

insert into mysql.user(Host,User,Password) values("localhost","admin",password("admin"));

刷新系统权限表

flush privileges;

这样就创建了一个名为:admin 密码为:admin 的用户

3、创建数据库(在root权限下)

create database abc;
//授权admin用户拥有abc数据库的所有权限。

grant all privileges on abc.* to admin@localhost identified by 'admin';
//刷新系统权限表
mysql>flush privileges;

4、删除用户。

@>mysql -u root -p
@>密码
mysql>

DELETE FROM mysql.user WHERE User="admin" and Host="localhost";
mysql>flush privileges;

5、mysql>drop database abc;//删除用户的数据库

6、查看数据库中具体某个用户的权限

mysql> show grants for 'cactiuser'@'%';

7、查询所有用户:

SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;

8、显示所有数据库

show databases;

9、修改指定用户密码。

@>mysql -u root -p
@>密码
mysql>

update mysql.user set password=password('新密码') where User="admin" and Host="localhost";
mysql>flush privileges;

10、删除某个用户权限

revoke all privileges on wordpress.* from 'admin'@'localhost';