安装mariadb速度慢的解决方法-使用国内源

在centos7上安装mariadb,按照官网的方法设置安装源,但是国外源慢得几乎崩溃。

搜索国内源,发现清华的源的确快,每秒2M,快如闪电。

国外源的配置(慢):

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

修改为国内源的配置(快):

[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.1/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

Mariadb二进制安装和配置说明

这几天搞智能DNS,用到了mariadb,在yum install mariadb以后,bind编译安装设置mysql目录时会报出一系列的错误,所以解决办法有3种:1.想都不要想跟定是库lib的问题,只能建立链接库 2.编译安装mariadb 3.二进制安装mariadb,编译很好时,所以选择了二进制安装mariadb。mariadb官网下载:https://downloads.mariadb.org/mariadb/10.2.11/ 你会发现有很多包,这里说明一下这些包都是干什么用的:

未分类

未分类

相关软件包介绍

mariadb-10.2.11.tar.gz   源代码包,编译用的
Galera 25.3.22   搞mariadb集群用的,单机不需要

mariadb-10.2.11-winx64.msi
mariadb-10.2.11-winx64.zip
mariadb-10.2.11-win32.zip
mariadb-10.2.11-win32.msi   //这几个就不用说了吧,Windows上面用的。

//下面这个包是包含glibc的二进制包
mariadb-10.2.11-linux-glibc_214-x86_64.tar.gz (requires GLIBC_2.14+)  
//各linux发行版二进制通用包,比如centos6
mariadb-10.2.11-linux-x86_64.tar.gz
//支持systemd的二进制包 (本例就是用这个) 比如centos7 systemd
mariadb-10.2.11-linux-systemd-x86_64.tar.gz (for systems with systemd)

//下面这几个是32位linux的包
mariadb-10.2.11-linux-i686.tar.gz
mariadb-10.2.11-linux-systemd-i686.tar.gz (for systems with systemd)
mariadb-10.2.11-linux-glibc_214-i686.tar.gz (requires GLIBC_2.14+)


//下面这两个是rpm包
Debian and Ubuntu Packages
Red Hat, Fedora, and CentOS Packages

Mariadb安装步骤

#创建mysql用户和用户组,并禁止用户登陆
[root@toydns local]# groupadd mysql
[root@toydns local]# useradd -r -g mysql -s /sbin/nologin mysql

#下载mariadb包:
wget https://downloads.mariadb.org/interstitial/mariadb-10.2.11/bintar-linux-systemd-x86_64/mariadb-10.2.11-linux-systemd-x86_64.tar.gz

#解压:
tar xf mariadb-10.2.11-linux-systemd-x86_64.tar.gz 

#更改安装目录:注意不要提前建立/usr/local/mysql目录,直接放过去即可
mv mariadb-10.2.11-linux-systemd-x86_64 /usr/local/mysql/

#进入/usr/local/mysql目录
[root@toydns mysql]#cd  /usr/local/mysql
[root@toydns mysql]# ll
total 204
drwxr-xr-x.  2 1021 1004  4096 Oct 26 14:04 bin
-rw-r--r--.  1 1021 1004 17987 Nov 27 18:09 COPYING
-rw-r--r--.  1 1021 1004 86263 Nov 27 18:09 COPYING.thirdparty
-rw-r--r--.  1 1021 1004  2275 Nov 27 18:09 CREDITS
drwxrwxr-x.  3 1021 1004  4096 Nov 28 06:16 data
drwxr-xr-x.  2 root root  4096 Dec  4 23:06 etc
-rw-r--r--.  1 1021 1004  8245 Nov 27 18:09 EXCEPTIONS-CLIENT
drwxrwxr-x.  3 1021 1004  4096 Nov 28 06:15 include
-rw-r--r--.  1 1021 1004  8694 Nov 27 18:09 INSTALL-BINARY
drwxr-xr-x.  4 1021 1004  4096 Nov 14 22:35 lib
drwxrwxr-x.  4 1021 1004  4096 Nov 28 06:16 man
drwxrwxr-x. 11 1021 1004  4096 Nov 28 06:15 mysql-test
-rw-r--r--.  1 1021 1004  2374 Nov 27 18:09 README.md
-rw-r--r--.  1 1021 1004 19510 Nov 27 18:09 README-wsrep
drwxrwxr-x.  2 1021 1004  4096 Nov 28 06:15 scripts
drwxrwxr-x. 32 1021 1004  4096 Nov 28 06:15 share
drwxrwxr-x.  4 1021 1004  4096 Nov 28 06:15 sql-bench
drwxrwxr-x.  4 1021 1004  4096 Nov 28 06:15 support-files


相关目录介绍:
bin: 可执行的二进制程序的存放目录,客户端程序mysql就位于这个目录下。
COPYING:版权以及开源信息
COPYING.thirdparty: 版权信息
CREDITS:关于MariaDB软件基金会的一些相关信息,里面还有国内的互联网巨头阿里
data:默认的数据库存放目录,如果我们一开始没有指定数据库存放目录的话,那就会被存储到这个位置。
EXCEPTIONS-CLIENT:例外情况
include:MariaDB 所需要的一些程序文件
INSTALL-BINARY: 安装帮助文档,可以详细阅读,对安装数据库有很大的帮助
lib: 软件运行所需要的库文件
man:软件的帮助文档
mysql-test: 数据库的测试组件
scipts:mysql初始化初始化时要用到的脚本文件,通读一下脚本,可以了解Mysql 的安装过程
share: 共享的文件内容
support-files: mysql 正常运行所需要的配置文件或者文档,这一点很重要,如果我们要自定义配置文件的话,就需要参考这里面的配置文件来进行定义。

这里有一点需要注意:data目录是数据库的存放路径,我们在之前已经手动指定。在实际生产中,企业数据增长很快,数据库文件有可能会很大,因此最好将该目录指定到一个单独的磁盘上,或者大分区,或者使用逻辑卷都可以,避免因物理空间不足,导致出现故障。

#创建mysql配置文件目录
mkdir  /usr/local/mysql/etc

#在/usr/local/mysql/support-files/下的配置文件模板,已经配置好的部分参数,分别用于不同的环境,这里说明一下:
my-small.cnf 这个是为小型数据库或者个人测试使用的,不能用于生产环境
my-medium.cnf 这个适用于中等规模的数据库,比如个人项目或者小型企业项目中,
my-large.cnf 一般用于专门提供SQL服务的服务器中,即专门运行数据库服务的主机,配置要求要更高一些,适用于生产环境
my-huge.cnf 用于企业级服务器中的数据库服务,一般更多用于生产环境使用
所以根据以上几个文件,如果个人使用或者测试,那么可以使用前两个模板;企业服务器或者64G以上的高配置服务器可以使用后面两个模板,另外也可以根据自己的需求来加大参数和扩充配置获得更好的性能。

[root@toydns mysql]# ll support-files/
-rw-r--r--. 1 1021 1004  4914 Nov 27 18:32 my-huge.cnf
-rw-r--r--. 1 1021 1004 20421 Nov 27 18:32 my-innodb-heavy-4G.cnf
-rw-r--r--. 1 1021 1004  4901 Nov 27 18:32 my-large.cnf
-rw-r--r--. 1 1021 1004  4914 Nov 27 18:32 my-medium.cnf
-rw-r--r--. 1 1021 1004  2840 Nov 27 18:32 my-small.cnf

#复制my-medium.cnf到etc下并改名为my.cnf,修改数据库文件存放的目录
cp support-files/my-medium.cnf  etc/my.cnf
vi etc/my.cnf

# The MariaDB server
[mysqld]
port            = 3306   
socket          = /tmp/mysql.sock
basedir = /usr/local/mysql   //mysql目录
datadir= /usr/local/mysql/data  //数据存放位置
innodb_file_per_table = on  //每张表一个单独文件,便于管理
skip_name_resolve = on  //忽略反向解析,加快访问速度
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

:wq保存后给权限
[root@toydns local]# chown -R mysql.mysql /usr/local/mysql/
[root@toydns local]# chmod -R 755 /usr/local/mysql/

#添加MySQL到PATH环境变量里面去,省的以后敲命令麻烦
[root@toydns mysql]# vi /etc/profile.d/mysql.sh
export  PATH=/usr/local/mysql/bin:$PATH

source /etc/profile.d/mysql.sh  使这个配置文件及时生效

#初始化mysql
/usr/local/mysql/scripts/mysql_install_db --user=mysql

#创建mariadb自启动脚本,并加入开机启动
cp /usr/local/mysql/support-files/systemd/mariadb.service  /usr/lib/systemd/system/

systemctl enable mariadb
systemctl start mariadb
systemctl stop mariadb

MySQL的安全设置

[root@toydns mysql]# ./bin/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] y     //是否设置MySQL管理员root的密码,y设置,输入2次
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    //是否删除匿名账户 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] n   //是否不允许root用户远程登陆,n不禁用
 ... skipping.

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   //是否删除test测试数据库,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!

linux下安装mariadb(mysql)并创建账户

安装mariadb(mysql)并创建账户

(red hat内置的安装已经是mariadb,而非mysql,因为二者差不多,所以不建议重新下载mysql,建议直接用mariadb)

安装:

[root@localhost ~]# dnf -y install mariadb*

启动:

[root@localhost ~]# systemctl start mariadb

[root@localhost ~]# systemctl status mariadb

[root@localhost ~]# firewall-cmd --permanent --add-service=mysql

[root@localhost ~]# firewall-cmd --reload

[root@localhost ~]# firewall-cmd --permanent --add-service=mariadb

Error: INVALID_SERVICE: mariadb

[root@localhost ~]# mysql_secure_installation

进入root下的mariadb(即mysql)

[root@localhost ~]# mysql -u root -p

Enter password: (这里要输入密码)

Welcome to the MariaDB monitor.  Commands end with ; or g.

Your MariaDB connection id is 13

Server version: 10.1.26-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

输入密码后进入

MariaDB [(none)]>





#------

建账户:

MariaDB [(none)]> create database menmydb;

Query OK, 1 row affected (0.02 sec)

创建密码:

MariaDB [(none)]> create user menxiaolei@localhost identified by 'men12xiao34lei56';

Query OK, 0 rows affected (0.02 sec)

给权限:

MariaDB [(none)]> grant all on menmydb.* to menxiaolei@localhost identified by 'men12xiao34lei56';

Query OK, 0 rows affected (0.02 sec)

退出

MariaDB [(none)]>



Ctrl+D

#——————————————————–

普通用户使用menmydb

[xjj@localhost ~]$ mysql -u menxiaolei -p

Enter password: (这里输入密码)

Welcome to the MariaDB monitor.  Commands end with ; or g.

Your MariaDB connection id is 17

Server version: 10.1.26-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)]> show databases;

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

| Database           |

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

| information_schema |

| menmydb            |

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

2 rows in set (0.00 sec)
MariaDB [(none)]> show tables;



MariaDB [(none)]> describe servers;



Ctrl+D

Centos7 MariaDB安装

MariaDB 是 MySQL 数据库的自由开源分支,与 MySQL 在设计思想上同出一源,在未来仍将是自由且开源的。Red Hat Enterprise Linux/CentOS 7.0 发行版已将默认的数据库从 MySQL 切换到 MariaDB.

安装MariaDB-server

yum -y update  
yum -y install mysql  
yum -y install mariadb-server mariadb-client  
systemctl enable mariadb  
systemctl start mariadb  
systemctl status mariadb  
[root@centos-rpi2 ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2017-10-11 09:46:50 HKT; 4h 12min ago
Main PID: 1425 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─1425 /bin/sh /usr/bin/mysqld_safe –basedir=/usr
└─1587 /usr/libexec/mysqld –basedir=/usr –datadir=/var/lib/mysql…
Oct 11 09:46:36 centos-rpi2 mariadb-prepare-db-dir[1346]: Initializing MariaD…
Oct 11 09:46:37 centos-rpi2 mariadb-prepare-db-dir[1346]: 171011 9:46:37 [No…
Oct 11 09:46:38 centos-rpi2 mariadb-prepare-db-dir[1346]: 171011 9:46:38 [No…
Oct 11 09:46:39 centos-rpi2 mariadb-prepare-db-dir[1346]: PLEASE REMEMBER TO …
Oct 11 09:46:39 centos-rpi2 mariadb-prepare-db-dir[1346]: To do so, start the…
Oct 11 09:46:39 centos-rpi2 mariadb-prepare-db-dir[1346]: ‘/usr/bin/mysqladmi…
Oct 11 09:46:39 centos-rpi2 mariadb-prepare-db-dir[1346]: ‘/usr/bin/mysqladmi…
Oct 11 09:46:40 centos-rpi2 mysqld_safe[1425]: 171011 09:46:40 mysqld_safe L….
Oct 11 09:46:40 centos-rpi2 mysqld_safe[1425]: 171011 09:46:40 mysqld_safe S…l
Oct 11 09:46:50 centos-rpi2 systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.

配置root账户

mysql -u root -p

直接回车

[root@centos-rpi2 ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 5
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)]>
use mysql;  
update user set password=password('Shuai_sqj') where user='root';  
grant all privileges on *.* to 'root'@'%' identified by 'Shuai_sqj' with grant option;  
//创建root远程管理  
flush privileges;  
exit

如何在CENTOS/REDHAT/FEDORA中安装MARIADB GALERA CLUSTER 10.0(数据库集群)

MariaDB Galera Cluster 10.0.12稳定已被释放,可供生产使用。MariaDB的是一个关系型数据库管理系统(RDBMS)。一般我们使用的小型应用程序,使用数据库服务器的单个节点服务。但是当有成千上万的用户不断在网上请求访问应用程序时,在这种情况下,我们需要一个架构,这将能够处理这种负载,并提供高可用性。因此,我们需要增加彼此互连的多个数据库服务器,并保持同步,因此在任何情况下,某一服务器出现故障,其他服务器可以继续向用户提供服务。

未分类

本文将帮助你建立MariaDB Galera Cluster10.0.12用CentOS 6.5运行3个节点。集群服务器具体如下。

  • 集群DB1:192.168.1.10(主机名: db1.howtoing.com )
  • 集群DB2:192.168.1.20(主机名: db2.howtoing.com )
  • 集群DB3:192.168.1.30(主机名: db3.howtoing.com )

注意: 第1步/2/3 将完成所有群集节点,操作过程和节点具体配置。

第1步:添加MariaDB存储库

创建一个MariaDB存储库/etc/yum.repos.d/mariadb.repo在你的系统中使用下面的内容。 下面存储库将在CentOS 6.x的系统工作,对于其他系统中使用存储库生产工具,并添加到您的系统。 在CentOS 6 – 64位

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

在CentOS 6 – 32位

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

第2步:安装MariaDB和Galera

安装MariaDB Galera群集程序包之前,删除系统上安装任何现有的MySQL或MariaDB的包。并在所有节点上使用以下命令安装。

# yum install MariaDB-Galera-server MariaDB-client galera

第3步:初始MariaDB配置

在上述步骤成功安装包之后,做一些初步MariaDB的配置。使用下列命令并按照群集的所有节点上的说明。它将提示设置root帐户密码。

# service mysql start
# mysql_secure_installation

之后,创建所有的节点用户,它可以从您的网络集群中的访问数据库中MariaDB的用户。

# mysql -u root -p

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'cluster'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit

启动群集配置之前停止MariaDB服务

# service mysql stop

第4步:DB1设置MariaDB Galera Cluster

让我们安装MariaDB Galera Cluster从DB1服务器开始。编辑MariaDB服务器配置文件,并在[MariaDB]部分添加下面的值。

[root@db1 ~]# vim /etc/my.cnf.d/server.cnf
query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.1.10,192.168.1.20,192.168.1.30"
wsrep_cluster_name='cluster1'
wsrep_node_address='192.168.1.10'
wsrep_node_name='db1'
wsrep_sst_method=rsync
wsrep_sst_auth=cluster:password

使用以下命令启动集群。

[root@db1 ~]# /etc/init.d/mysql bootstrap
Bootstrapping the clusterStarting MySQL.... SUCCESS!

如果你在启动过程中有任何问题,请检查MariaDB的错误日志文件,在 /var/lib/mysql/.err

第5步:添加DB2,在MariaDB的集群中

DB1成功启动集群后。在DB2上开始配置。编辑MariaDB的服务器配置文件,并在[MariaDB]部分添加下面的值。所有设置都差不多,除了wsrep_node_address,wsrep_cluster_address和wsrep_node_name DB1 。

[root@db2 ~]# vim /etc/my.cnf.d/server.cnf
query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.1.10,192.168.1.20,192.168.1.30"
wsrep_cluster_name='cluster1'
wsrep_node_address='192.168.1.20'
wsrep_node_name='db2'
wsrep_sst_method=rsync
wsrep_sst_auth=cluster:password

使用以下命令启动集群。

[root@db2 ~]# /etc/init.d/mysql start
Starting MySQL..... SUCCESS!

第6步:添加DB3,在MariaDB的集群中

此服务器是可选的,如果你想只有两个集群服务器,则可以忽略这一步,如果不配置DB3 你需要从DB1/DB2配置文件中删除第三个服务器IP。要添加此服务器进行修改和DB2一样。

[root@db3 ~]# vim /etc/my.cnf.d/server.cnf
query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.1.10,192.168.1.20,192.168.1.30"
wsrep_cluster_name='cluster1'
wsrep_node_address='192.168.1.30'
wsrep_node_name='db2'
wsrep_sst_method=rsync
wsrep_sst_auth=cluster:password

使用以下命令启动集群。

[root@db3 ~]# /etc/init.d/mysql start
Starting MySQL..... SUCCESS!

第7步:测试MariaDB的Galera群集设置

在这个阶段,您的群集安装已经完成,并正常运行。现在,您可以测试通过在集群的任何服务器创建数据库和表群集安装,它会立即复制到群集中的所有服务器。

MariaDB的mysql.user表被误删除

误删除mysql.user表或者表中的数据

解决方法:

1、停掉所有mysql或mariadb服务:

~]#service mysqld stop(CentOS6)
~]#systemctl  stop mysqld.service(CentOS7)

或者

~]#pkill mysql

2、命令行启动:

~]#/bin/mysqld_safe --skip-grant-tables &

3、进入mysql库,用户信息存放在mysql库下user表:

MariaDB [none]>use mysql;

4、查看user表:

MariaDB [mysql]>select Host,User,Password from mysql.user;

5、(注意:本步骤在无mysql.user表时执行)因为误删除user表,所以现在需要在mysql库下新创建user表:

MariaDB [mysql]>create table `user` (
  `Host` char(60) collate utf8_bin NOT NULL default '',
  `User` char(16) collate utf8_bin NOT NULL default '',
  `Password` char(41) character set latin1 collate latin1_bin NOT NULL default '
',
  `Select_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Insert_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Update_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Delete_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Create_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Drop_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Reload_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Shutdown_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Process_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `File_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Grant_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `References_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Index_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Alter_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Show_db_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Super_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Create_tmp_table_priv` enum('N','Y') character set utf8 NOT NULL default 'N',

  `Lock_tables_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Execute_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Repl_slave_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Repl_client_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Create_view_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Show_view_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Create_routine_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Alter_routine_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `Create_user_priv` enum('N','Y') character set utf8 NOT NULL default 'N',
  `ssl_type` enum('','ANY','X509','SPECIFIED') character set utf8 NOT NULL defau
lt '',
  `ssl_cipher` blob NOT NULL,
  `x509_issuer` blob NOT NULL,
  `x509_subject` blob NOT NULL,
  `max_questions` int(11) unsigned NOT NULL default '0',
  `max_updates` int(11) unsigned NOT NULL default '0',
  `max_connections` int(11) unsigned NOT NULL default '0',
  `max_user_connections` int(11) unsigned NOT NULL default '0',
  PRIMARY KEY  (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global
privileges';

6、插入root用户数据:

MariaDB [mysql]>insert into mysql.user (Host,User,Password) values(‘localhost’,’root’,password(’pa$$word’));

7、查看添加root用户的权限:

MariaDB [mysql]>select * from mysql.user where User=’root’ G

8、添加属于root用户的管理权限:

MariaDB [mysql]>update mysql.user set  Select_priv='Y',Insert_priv='Y',Update_priv='Y',Delete_priv='Y',Create_priv='Y',Drop_priv='Y',Reload_priv='Y',Shutdown_priv='Y',Process_priv='Y',File_priv='Y',Grant_priv='Y',References_priv='Y',Index_priv='Y',Alter_priv='Y',Show_db_priv='Y',Super_priv='Y',Create_tmp_table_priv='Y',Lock_tables_priv='Y',Execute_priv='Y',Repl_slave_priv='Y',Repl_client_priv='Y',Create_view_priv='Y',Show_view_priv='Y',Create_routine_priv='Y',Alter_routine_priv='Y',Create_user_priv='Y',Event_priv='Y',Trigger_priv='Y',Create_tablespace_priv='Y' where User='root' and Host='localhost';

9、查看是否授权成功:

MariaDB [mysql]>select * from mysql.user where User=’root’;

10、完成提交并刷新内存数据:

MariaDB [mysql]>commit;
MariaDB [mysql]>flush privileges;

11、重新启动Mariadb并登录:

~]#systemctl restart mariadb.service
~]#mysql -uroot -hlocalhost -p

输入设置的密码(此处是pa$$word)。

登陆成功

12、验证:

MariaDB [none]>show grants;
MariaDB [none]>show grants for root@localhost;

Linux下MySQL/MariaDB Galera集群搭建过程

MariaDB介绍

MariaDB是开源社区维护的一个MySQL分支,由MySQL的创始人Michael Widenius主导开发,采用GPL授权许可证。

MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

详细介绍请参考链接:

http://mariadb.org/(官网)

http://baike.baidu.com/link?url=dFJ-My-I52YFc1mx26K804LPwZrcEWCwB4IqfA4-soYx6295BZLIe7bEFgOtt3CWZ8AYpkp1P342L4S-R4x4CK

Galera Cluster介绍

Galera Cluster是基于MySQL/innodb二次开发而成的一个支持“多主同步”的数据库主从集群,具有高可用,易于扩展等特点。

详细介绍请参考链接:

http://galeracluster.com/(官网)

Galera replication for MySQL

本文使用的Linux发行版:CentOS6.7 下载地址:https://wiki.centos.org/Download

1. 添加yum源

[root@localhost ~]# vi /etc/yum.repos.d/CentOS-MariaDB.repo

添加如下几行:

[mariadb] name = MariaDB
baseurl = http://yum.mariadb.org/5.5/rhel6-amd64
gpgkey = https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1

未分类

2. 安装mariadb galera软件包

[root@localhost ~]# yum install MariaDB-Galera-server MariaDB-client galera

未分类

未分类

3. 修改防火墙配置

[root@localhost ~]# vi /etc/sysconfig/iptables

添加如下几行:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4444 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4567 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4568 -j ACCEPT

未分类

4. 重启防火墙功能

[root@localhost ~]# service iptables restart

未分类

5. 安装selinux管理工具

[root@localhost ~]# yum provides /usr/sbin/semanage
[root@localhost ~]# yum -y install policycoreutils-python

未分类

6. 修改selinux安全策略

[root@localhost ~]# semanage port -a -t mysqld_port_t -p tcp 4567
[root@localhost ~]# semanage port -a -t mysqld_port_t -p tcp 4568
[root@localhost ~]# semanage permissive -a mysqld_t

未分类

7. 启动mysql服务

[root@localhost ~]# service mysql start

8. 执行mysql安全设置

[root@localhost ~]# mysql_secure_installation

(先设置root账户密码,再一直“y”下去即可)

未分类

未分类

9. 创建用于节点同步的账号

[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> grant usage on *.* to sst@'%' identified by '123456';
MariaDB [(none)]> flush privileges;

10. 修改mysql默认字符集

MariaDB [(none)]> show variables like 'character%';
MariaDB [(none)]> set character_set_server = utf8;
MariaDB [(none)]> set character_set_database = utf8;

11. 修改集群节点配置

[root@localhost ~]# cp /usr/share/mysql/wsrep.cnf /etc/my.cnf.d/
[root@localhost ~]# vi /etc/my.cnf.d/wsrep.cnf

修改如下几行:

wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://"    #集群节点N的地址(注意把前面的"#"删掉!)
wsrep_sst_auth=sst:123456    #节点N的数据库账户和密码
  • 参数说明

“gcomm://” 是特殊的地址,仅仅是galera cluster初始化启动时候使用。
如果集群启动以后,我们关闭了第一个节点,那么再次启动的时候必须先修改”gcomm://”为其他节点的集群地址,例如wsrep_cluster_address=”gcomm://192.168.0.152″。

检查/etc/my.cnf中有没有!includedir /etc/my.cnf.d/这一行,没有则添加。

[root@localhost ~]# vi /etc/my.cnf

未分类

到这里,第1个节点的配置就完成了,然后在另一台主机上按照步骤1~11配置第2个节点,只需修改节点2的wsrep_cluster_address为节点1的IP即可,以此类推。

12. 启动集群节点

  • 检查mysql进程:[root@localhost ~]# ps aux|grep mysql
  • 停止mysql服务:[root@localhost ~]# service mysql stop
  • 启动第1个节点:[root@localhost ~]# service mysql bootstrap

未分类

启动第2、3、…个节点:[root@localhost ~]# service mysql start

未分类

(注意:启动mysql之前先检查一下服务是否已经启动,不要重复启动,如果无法停止当前mysql服务则手动kill掉mysql的进程)

13. 检查集群运行状态

[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> show status like 'wsrep%';

如果wsrep_connected=ON且wsrep_ready=ON则说明节点成功接入集群。

未分类

未分类

14. 配置集群的仲裁节点

对于只有2个节点的galera cluster和其他集群软件一样,需要面对极端情况下的“脑裂”状态。为了避免这种问题,galera引入了“arbitrator(仲裁人)”。
“仲裁人”节点上没有数据,它在集群中的作用就是在集群发生分裂时进行仲裁,集群中可以有多个“仲裁人”节点。将“仲裁人”节点加入集群的方法很简单,运行如下命令即可:
[root@localhost ~]# garbd -a gcomm://<节点IP> -g my_wsrep_cluster -d

  • 参数说明

-a 集群地址
-g 集群名称
-d 以daemon模式运行

15. 检查数据库是否符合要求

部署到集群之前,建议先检查数据库是否符合galera的要求,比如存储引擎必须是innodb、数据表必须有主键等,否则记录将不会在多台复制。

选择指定的数据库,执行以下SQL输出不符合要求的表及其原因,根据相应的原因修改即可:

select distinct concat( t.table_schema, '.', t.table_name ) as tbl, t. engine, if ( isnull(c.constraint_name), 'nopk', '' ) as nopk, if ( s.index_type = 'fulltext', 'fulltext', '' ) as ftidx, if ( s.index_type = 'spatial', 'spatial', '' ) as gisidx from information_schema. tables as t left join information_schema.key_column_usage as c on ( t.table_schema = c.constraint_schema and t.table_name = c.table_name and c.constraint_name = 'primary' ) left join information_schema.statistics as s on ( t.table_schema = s.table_schema and t.table_name = s.table_name and s.index_type in ('fulltext', 'spatial')) where t.table_schema not in ( 'information_schema', 'performance_schema', 'mysql' ) and t.table_type = 'base table' and ( t.engine <> 'innodb' or c.constraint_name is null or s.index_type in ('fulltext', 'spatial')) order by t.table_schema, t.table_name;

16. 常见问题

  • 启动mysql时出错:SST in progress, setting sleep higher. ERROR!
    • 确保本机已安装rsync:[root@localhost ~]# yum list|grep rsync
    • 确保已允许galera sst使用的端口4444、4567、4568通过防火墙并重启防火墙功能
    • 确保selinux已对端口4444开放权限:[root@localhost ~]# semanage port -a -t mysqld_port_t -p tcp 4444
  • 查看galera集群状态时wsrep_connected和wsrep_ready的值均为OFF!

打开/etc/my.cnf.d/wsrep.cnf文件,找到wsrep_cluster_address=”gcomm://”这一行,检查前面是否有”#”,如果有则删掉并重启mysql。

阿里云centos7.3编译安装NGINX+PHP7+MariaDB+MEMCACHED

一、安装前准备

  • 修改默认主机名称
[root@iZuf60c5bxd15kr9gycvv6Z ~]# hostnamectl set-hostname centos7
[root@iZuf60c5bxd15kr9gycvv6Z ~]# reboot
[root@iZuf60c5bxd15kr9gycvv6Z ~]# yum update
  • 安装依赖库
[root@centos7 ~]# yum -y install libaio libaio-devel bison bison-devel zlib-devel openssl openssl-devel ncurses ncurses-devel libcurl-devel libarchive-devel boost boost-devel lsof wget gcc gcc-c++ make cmake perl kernel-headers kernel-devel pcre-devel

二、删除系统默认数据库配置文件

  • 查询
[root@centos7 ~]# find -H /etc/ | grep my.c
/etc/my.cnf
/etc/my.cnf.d
/etc/my.cnf.d/mysql-clients.cnf
/etc/pki/tls/certs/renew-dummy-cert
/etc/pki/tls/certs/make-dummy-cert
  • 删除
[root@centos7 ~]# rm -rf /etc/my.cnf /etc/my.cnf.d /etc/my.cnf.d/mysql-clients.cnf
  • 确认
[root@centos7 ~]# find -H /etc/ | grep my.c
/etc/pki/tls/certs/renew-dummy-cert
/etc/pki/tls/certs/make-dummy-cert

三、卸载系统自带mariadb-libs

  • 查询
[root@centos7 ~]# rpm -qa|grep mariadb-libs
mariadb-libs-5.5.52-1.el7.x86_64
  • 删除
[root@centos7 ~]# rpm -e mariadb-libs-5.5.52-1.el7.x86_64 --nodeps

四、安装MariaDB数据库

  • 下载安装包
[root@centos7 ~]# cd /usr/local/src
[root@centos7 src]# wget https://downloads.mariadb.org/interstitial/mariadb-10.2.8/source/mariadb-10.2.8.tar.gz 
  • 解压
[root@centos7 src]# tar -zxvf mariadb-10.2.8.tar.gz
  • 创建数据库安装目录,数据存放目录,以及用户组、用户
# 创建mysql用户组
[root@centos7 mariadb-10.2.8]# groupadd -r mysql
# 创建mysql用户
[root@centos7 mariadb-10.2.8]# useradd -r -g mysql -s /sbin/nologin -d /usr/local/mysql -M mysql
# 创建安装目录
[root@centos7 mariadb-10.2.8]# mkdir -p /usr/local/mysql
# 创建数据存放目录
[root@centos7 mariadb-10.2.8]# mkdir -p /data/mysql
# 赋以mysql用户读写权限
[root@centos7 mariadb-10.2.8]# chown -R mysql:mysql /data/mysql
  • 编译安装
[root@centos7 mariadb-10.2.8]# cd /usr/local/src/mariadb-10.2.8
# 输入以下编译参数
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql 
-DMYSQL_DATADIR=/data/mysql 
-DSYSCONFDIR=/etc 
-DWITHOUT_TOKUDB=1 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_ARCHIVE_STPRAGE_ENGINE=1 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 
-DWIYH_READLINE=1 
-DWIYH_SSL=system 
-DVITH_ZLIB=system 
-DWITH_LOBWRAP=0 
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock 
-DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci

# 如果编译失败请删除CMakeCache.txt
[root@centos7 mariadb-10.2.8]# rm -f CMakeCache.txt
# 让指令重新执行,否则每次读取这个文件,命令修改正确也是报错
# cmake没问题,可以编译并且安装了

# 开始安装,这个过程比较久,跟据电脑配制不同可能会有10-30分钟
[root@centos7 mariadb-10.2.8]# make && make install
  • 导入mysql系统表
[root@centos7 mariadb-10.2.8]# cd /usr/local/mysql/
[root@localhost mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • 复制配制文件
[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf
  • 编写快捷启动脚本
[root@centos7 mysql]# vi /lib/systemd/system/mysql.service
# --------------------------------------------------------------------------
# 输入以下代码
# --------------------------------------------------------------------------
[Unit]
Description=MySQL Community Server
After=network.target

[Service]
User=mysql
Group=mysql
Type=forking
PermissionsStartOnly=true
PIDFile=/data/mysql/centos7.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/usr/local/mysql/support-files/mysql.server restart
ExecStop=/usr/local/mysql/support-files/mysql.server stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置开机启动
[root@centos7 mysql]# systemctl enable mysql.service
  • 其它命令说明
# 启动mysql
[root@centos7 mysql]# systemctl start mysql.service
# 停止mysql
[root@centos7 mysql]# systemctl stop mysql.service
# 重启mysql
[root@centos7 mysql]# systemctl restart mysql.service
# 如果提示:Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
# 请选结束mysql进程后再尝试执行上面的快捷操作
[root@centos7 mysql]# pkill -9 mysql
  • 配置环境变量(以便在任何目录下输入mysql命令)
# 创建独立环境变量文件
[root@localhost mysql]# touch /etc/profile.d/mysql.sh
# 写入变量值
[root@localhost mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin/' > /etc/profile.d/mysql.sh 
# 赋以执行权限
[root@localhost mysql]# chmod 0777 /etc/profile.d/mysql.sh 
# 刷新生效
[root@localhost mysql]# source /etc/profile.d/mysql.sh
  • 初始化MariaDB
# 运行MariaDB初始化脚本
[root@localhost mysql]# ./bin/mysql_secure_installation

# --------------------------------------------------------------------------
# 根据相关提示进行操作
# 以下提示:
# --------------------------------------------------------------------------
Enter current password for root (enter for none):    输入当前root密码(没有输入)
Set root password? [Y/n]                             设置root密码?(是/否)
New password:                                        输入新root密码
Re-enter new password:                               确认输入root密码
Password updated successfully!                       密码更新成功
Remove anonymous users? [Y/n]                        删除匿名用户?(是/否)
Disallow root login remotely? [Y/n]                  不允许root登录远程?(是/否)
Reload privilege tables now? [Y/n]                   现在重新加载权限表(是/否)

#全部完成!如果你已经完成了以上步骤,MariaDB安装现在应该安装完成。
  • 创建外部管理员帐号(根据需要,请尽量保证密码的复杂性避免数据库外泄)
[root@localhost mysql] mysql -uroot -p
# 根据提示输入密码
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

五、安装NGINX

  • 下载安装包并解压
[root@localhost mysql]# cd /usr/local/src
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
[root@localhost src]# tar -zxvf nginx-1.12.1.tar.gz
  • 创建NGINX安装目录,WEB存放目录,以及用户组、用户
# 创建nginx用户组
[root@centos7 src]# groupadd -r www
# 创建nginx用户
[root@centos7 src]# useradd -r -g www -s /sbin/nologin -d /usr/local/nginx -M www
# 创建安装目录
[root@centos7 src]# mkdir -p /usr/local/nginx
# 创建数据存放目录
[root@centos7 src]# mkdir -p /data/web
# 赋以mysql用户读写权限
[root@centos7 src]# chown -R www:www /data/web
  • 编译安装
[root@localhost src]# cd nginx-1.12.1
# 输入以下参数
./configure 
--prefix=/usr/local/nginx 
--without-http_memcached_module 
--user=www  
--group=www 
--with-http_stub_status_module 
--with-http_ssl_module 
--with-http_gzip_static_module

# 如果看到以下说明则编译成功
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

# 开始安装
[root@localhost nginx-1.12.1]# make && make install
  • 尝试启动
[root@centos7 nginx-1.12.1]# /usr/local/nginx/sbin/nginx
# 如果未提示错误即代表安装成功
  • 编写快捷启动脚本
[root@centos7 nginx-1.12.1]# vi /lib/systemd/system/nginx.service
# --------------------------------------------------------------------------
# 输入以下代码
# --------------------------------------------------------------------------
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置开机启动
[root@centos7 nginx-1.12.1]# systemctl enable nginx.service
  • 其它命令说明
# 启动nginx
[root@centos7 nginx-1.12.1]# systemctl start nginx.service
# 停止nginx
[root@centos7 nginx-1.12.1]# systemctl stop nginx.service
# 重启nginx
[root@centos7 nginx-1.12.1]# systemctl restart nginx.service
# 如果提示:Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
# 请选结束nginx进程后再尝试执行上面的快捷操作
[root@centos7 nginx-1.12.1]# pkill -9 nginx
  • 修改配NGINX配制文件
[root@centos7 php-7.1.9]# cd /usr/local/nginx/conf
[root@centos7 conf]# vi nginx.conf
# 修改如下代码
# --------------------------------------------------------------------------
#user  nobody;去除前面#号,并将用户改为www www
user www www;
# --------------------------------------------------------------------------
#pid        logs/nginx.pid; 去除前面的#号 
pid        logs/nginx.pid;
# --------------------------------------------------------------------------
去除前面的#号 如下
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
# --------------------------------------------------------------------------
# gzip  on;去除前面的#号并加上隐藏版号代码 
server_tokens off;
# --------------------------------------------------------------------------
index  index.html index.htm;后面加上index.php 默认页
index  index.html index.htm index.php;
# --------------------------------------------------------------------------
去下以下前面的#号并做如下修改
location ~ .php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
  • 最后张节介绍虚拟主机配制,以及memcache配制

七、安装PHP7

  • 下载安装包并解压
[root@centos7 nginx-1.12.1]# cd /usr/local/src
[root@centos7 src]# wget http://cn.php.net/distributions/php-7.1.9.tar.gz
[root@centos7 src]# tar -zxvf php-7.1.9.tar.gz
  • 安装必要的相关扩展
[root@centos7 src]# yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
  • 编译安装
[root@localhost src]# cd php-7.1.9
# 输入以下参数
./configure 
--prefix=/usr/local/php 
--with-config-file-path=/usr/local/php/etc 
--enable-fpm 
--with-fpm-user=www  
--with-fpm-group=www 
--enable-inline-optimization 
--disable-debug 
--disable-rpath 
--enable-shared  
--enable-soap 
--with-libxml-dir 
--with-xmlrpc 
--with-openssl 
--with-mcrypt 
--with-mhash 
--with-pcre-regex 
--with-zlib 
--enable-bcmath 
--with-iconv 
--with-bz2 
--enable-calendar 
--with-curl 
--with-cdb 
--enable-dom 
--enable-exif 
--enable-fileinfo 
--enable-filter 
--with-pcre-dir 
--enable-ftp 
--with-gd 
--with-openssl-dir 
--with-jpeg-dir 
--with-png-dir 
--with-zlib-dir  
--with-freetype-dir 
--enable-gd-native-ttf 
--enable-gd-jis-conv 
--with-gettext 
--with-gmp 
--with-mhash 
--enable-json 
--enable-mbstring 
--enable-mbregex 
--enable-mbregex-backtrack 
--with-libmbfl 
--with-onig 
--enable-pdo 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd 
--with-zlib-dir 
--with-pdo-sqlite 
--with-readline 
--enable-session 
--enable-shmop 
--enable-simplexml 
--enable-sockets  
--enable-sysvmsg 
--enable-sysvsem 
--enable-sysvshm 
--enable-wddx 
--with-libxml-dir 
--with-xsl 
--enable-zip 
--enable-mysqlnd-compression-support 
--with-pear 
--enable-opcache

# 开始安装
[root@localhost php-7.1.9]# make && make install
  • 配置环境变量(以便在任何目录下输入php命令)
# 创建独立环境变量文件
[root@localhost php-7.1.9]# touch /etc/profile.d/php.sh
# 写入变量值
[root@localhost php-7.1.9]# echo 'export PATH=$PATH:/usr/local/php/bin/' > /etc/profile.d/php.sh 
# 赋以执行权限
[root@localhost php-7.1.9]# chmod 0777 /etc/profile.d/php.sh 
# 刷新生效
[root@localhost php-7.1.9]# source /etc/profile.d/php.sh
  • 配制php.ini
[root@centos7 php-7.1.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@centos7 php-7.1.9]# vi /usr/local/php/etc/php.ini
# 做以下修改(时区,不显示版本号,开启opcache缓存加速PHP)
# --------------------------------------------------------------------------
1.找到:;date.timezone =                               修改为:date.timezone = PRC
2.找到:expose_php = On                                   修改为:expose_php = Off
3.找到:opcache.enable=0                              修改为:opcache.enable=1
4.在 Dynamic Extensions 代码块中添加 zend_extension=opcache.so
  • 配置php-fpm
[root@centos7 php-7.1.9]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@centos7 php-7.1.9]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@centos7 php-7.1.9]# cp sapi/fpm/init.d.php-fpm /usr/local/php/bin/php-fpm
[root@centos7 php-7.1.9]# chmod 0777 /usr/local/php/bin/php-fpm
  • 尝试启动
[root@centos7 php-7.1.9]# /usr/local/php/bin/php-fpm start
Starting php-fpm  done
# 如提示以上即表示安装成功
  • 编写快捷启动脚本
[root@centos7 php-7.1.9]# vi /lib/systemd/system/php-fpm.service
# --------------------------------------------------------------------------
# 输入以下代码
# --------------------------------------------------------------------------
[Unit]
Description=php-fpm
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/bin/php-fpm start
ExecReload=/usr/local/php/bin/php-fpm restart
ExecStop=/usr/local/php/bin/php-fpm stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置开机启动
[root@centos7 php-7.1.9]# systemctl enable php-fpm.service
  • 其它命令说明
# 启动php-fpm
[root@centos7 php-7.1.9]# systemctl start php-fpm.service
# 停止php-fpm
[root@centos7 php-7.1.9]# systemctl stop php-fpm.service
# 重启php-fpm
[root@centos7 php-7.1.9]# systemctl restart php-fpm.service
# 如果提示:Job for php-fpm.service failed because the control process exited with error code. See "systemctl status php-fpm.service" and "journalctl -xe" for details.
# 请选结束php-fpm进程后再尝试执行上面的快捷操作
[root@centos7 php-7.1.9]# pkill -9 php-fpm

八、NGINX虚拟主机配制

  • 打开配制文件
[root@centos7 php-7.1.9]# vi /usr/local/nginx/conf/nginx.conf
  • 在http{}节点尾加入以下代码
    ##############################################
    #LIANGZHI
    ##############################################
    server {
        listen          80;
        server_name     *.demo.com;
        root            /data/web/demo/www;
        access_log      /data/web/demo/log/access.log  main;
        error_log       /data/web/demo/log/error.log error;
        index           index.php;

        #THINKPHP伪静态
        location / {
            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.php?s=$1 last;
                break;
            }
        }

        #解析PHP代码
        location ~ .php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        #静态资源缓存1天
        location ~ .*.(gif|jpg|jpeg|png|bmp|ico|swf|js|css)$ {
            expires     1d;
            access_log  off;
        }

        #字体文件跨域问题
        location ~ .*.(eof|ttf|ttc|otf|eof|woff|woff2|svg)(.*){
            add_header Access-Control-Allow-Origin *;
        }
    }

九、安装PHP支持插件Memcache

[root@centos7 www]# cd /usr/local/src
[root@centos7 src]# wget http://memcached.org/files/memcached-1.5.1.tar.gz
[root@centos7 src]# tar -zxvf memcached-1.5.1.tar.gz
[root@centos7 src]# cd memcached-1.5.1
[root@centos7 memcached-1.5.1]# yum install libevent*
[root@centos7 memcached-1.5.1]# ./configure --prefix=/usr/local/memcached
[root@centos7 memcached-1.5.1]# make && make install
  • 编写快捷启动脚本
[root@centos7 memcached-1.5.1]# vi /usr/local/memcached/memcached
# 输入以下代码(启动参数请在下面的脚本中修改,如端口,最大内存)
#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached

# Source function library.
. /etc/rc.d/init.d/functions

PORT=11211
USER=root 
MAXCONN=1024
CACHESIZE=64
OPTIONS=""

if [ -f /etc/sysconfig/memcached ];then
    . /etc/sysconfig/memcached
fi

# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
    exit 0
fi

RETVAL=0

start () {
    echo "Starting memcached ..."
    # insure that /var/run/memcached has proper permissions
    chown $USER /usr/local/memcached/bin/memcached
    /usr/local/memcached/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached.pid $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
}
stop () {
    echo "Stopping memcached ..."
    killproc memcached
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
        rm -f /var/lock/subsys/memcached
        rm -f /var/run/memcached.pid
    fi
}

restart () {
    stop
    start
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
    stop
    ;;
    status)
    status memcached
    ;;
    restart|reload)
    restart
    ;;
    condrestart)
    [ -f /var/lock/subsys/memcached ] && restart || :
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
    exit 1
esac
  • 脚本执行权限
[root@localhost memcached-1.5.1]# chmod 0777 /usr/local/memcached/memcached
编写服务启动脚本
[root@centos7 memcached-1.5.1]# vi /lib/systemd/system/memcached.service
# --------------------------------------------------------------------------
# 输入以下代码
# --------------------------------------------------------------------------
[Unit]
Description=memcached
After=network.target

[Service]
Type=forking
PIDFile=/var/run/memcached.pid
ExecStart=/usr/local/memcached/memcached start
ExecReload=/usr/local/memcached/memcached restart
ExecStop=/usr/local/memcached/memcached stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置开机启动
[root@centos7 memcached-1.5.1]# systemctl enable memcached.service
  • 其它命令说明
# 启动memcached
[root@centos7 memcached-1.5.1]# systemctl start memcached.service
# 停止memcached
[root@centos7 memcached-1.5.1]# systemctl stop memcached.service
# 重启memcached
[root@centos7 memcached-1.5.1]# systemctl restart memcached.service
# 如果提示:Job for memcached.service failed because the control process exited with error code. See "systemctl status memcached.service" and "journalctl -xe" for details.
# 请选结束memcached进程后再尝试执行上面的快捷操作
[root@centos7 memcached-1.5.1]# pkill -9 memcached
  • 为PHP增加memcache支持(官司网memcache-3.0.8暂时不支持PHP的编译)

官网memcache-3.0.8在编译的时候出现“php-smart_str.h”没有找到的错误!
但是我们可以在github里面找到pecl-memcache支持PHP7的分支,请按以下操作即可完成memcache中扩展安装

[root@centos7 memcached]# cd /usr/local/src/php-7.1.9/ext
# 如果已经安装git忽略yun install git
[root@centos7 memcached]# yum install git
[root@centos7 ext]# git clone https://github.com/websupport-sk/pecl-memcache memcache
[root@centos7 ext]# cd memcache
[root@centos7 ext]# yum install autoconf
[root@centos7 ext]# /usr/local/php/bin/phpize
[root@centos7 ext]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@centos7 ext]# make && make install
  • 修改php.ini
[root@centos7 ext]# vi /usr/local/php/etc/php.ini
# 在 Dynamic Extensions 代码块中添加如下扩展
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/memcache.so
  • 重启linux完成安装(或重启nginx php-fpm)
[root@centos7 ext]# reboot

mysql、mariadb安装和多实例配置

本文介绍mysql各种安装方法(rpm/glibc通用二进制/源码编译)以及多实例配置的方法,没什么技术,算是一篇方法归总文章。

  • 本文的安装环境为centos6.6和centos7.2,但大多数地方都以centos6.6作为演示示例。
  • 本文安装MySQL时,它们的运行身份为mysql,数据目录datadir为/mydata/data。
  • pid文件路径设置为/mydata/data/mysql.pid或/mydata/data/hostname.pid。
  • 由于mariadb和mysql 5系列并没有太大的不同。因此仅详细展示mysql的安装。最后将简单提一提mariadb。

一、mysql单实例安装

1.1 rpm包安装mysql

直接yum安装mysql-server即可。但注意两点:

  • centos7上,yum默认将安装mariadb。
  • centos6上,yum默认安装的版本比较老(5.1版),要安装mysql 5.6或mysql 5.7,可以从官方下载,也可以使用以下配置的yum源。
cat <<eof>/etc/yum.repos.d/mysql.repo
[mysql]
name=MySQL
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
# baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
# baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
# baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
eof

此外,在sohu镜像站点也提供了mysql的各个rpm版本。地址:http://mirrors.sohu.com/mysql/

还需注意,配置了yum源后安装Mysql将使用mysql-community-*安装各mysql相关包,例如mysql-community-server。

安装完成后,启动mysqld。

shell> service mysqld start      # 或 systemctl start mysqld

如果启动失败,则可能需要初始化MySQL。

shell> mkdir -p /mydata/data
shell> chown -R mysql.mysql /mydata/data
shell> mysql_install_db --datadir=/mydata/data --user=mysql

如果使用mysql_install_db初始化时提示该命令已经废弃(5.7版本可能会如此提示),那么使用下面的命令进行初始化。

# 初始化时,为root@localhost创建一个临时密码存放在mysql.log中
shell> mysqld --initialize --datadir=/mydata/data --user=mysql
# 初始化时,为root@localhost创建一个空密码
shell> mysqld --initialize-insecure --datadir=/mydata/data --user=mysql

初始化后再启动,启动成功后连接数据库并修改root@localhost用户的密码,然后退出。

shell> mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql> q

如果前面使用mysqld –initialize初始化数据库,那么将会为”root@localhost”创建一个密码,这将使得无法直接使用mysql命令连接数据库。可以先从mysql.log中筛选出创建的临时密码,然后再手动修改为”123456″。

shell> grep 'temporary password' /var/log/mysqld.log
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql> q

1.2 通用二进制包安装mysql

通用二进制包相当于windows中的便携版软件,解压后稍微配置下就可以直接使用,不用安装。

mysql通用二进制版官方下载地址:

  • MySQL 5.6通用二进制包下载:
    https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.12-x86_64.tar.gz

  • MySQL 5.7通用二进制包下载:
    https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.12-x86_64.tar.gz

其中文件中的glibc2.12表示的是Linux系统的glibc版本要比2.12新,可以使用ldd –version查看glibc版本。在CentOS 6上glibc默认就是2.12的,所以无需顾虑。

shell> tar xf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
shell> ln -s /usr/local/mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql

1.2.1 初始化数据库

不使用rpm包安装,就需要对mysql进行初始化以创建一些文件、库和指定一些参数。但在初始化mysql前,要预先做一些操作。

shell> mkdir -p /mydata/data
shell> useradd -r -s /sbin/nologin mysql
shell> chown -R mysql.mysql /usr/local/mysql
shell> chown -R mysql.mysql /mydata/data
shell> cd /usr/local/mysql
shell> scripts/mysql_install_db --datadir=/mydata/data --user=mysql
shell> chown -R root.root /usr/local/mysql

执行mysql_install_db时会在/tmp下创建临时表,所以mysql用户需要对/tmp有写权限,否则执行实例初始化脚本时可能会报类似下面的错误:

ERROR: 1 Can't create/write to file '/tmp/#sql_7a0e_0.MYI' (Errcode: 13)

这说明没有写权限,所以需要修改/tmp目录的权限:

chmod 1777 /tmp

同样,mysql_install_db初始时如果提示已废弃,则使用如下方法:

bin/mysqld --initialize-insecure --datadir=/mydata/data --user=mysql

初始化完成后,提供配置文件和服务启动脚本。

shell> cp -a support-files/mysql.server /etc/init.d/mysqld
shell> cp -a support-files/my-default.cnf /etc/my.cnf  

# 修改my.cnf的datadir
shell> vim /etc/my.cnf 
[mysqld]
datadir=/mydata/data

如果是centos7,则提供如下服务启动脚本(如有必要,修改pid文件路径)。

shell> cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/var/run/mysqld/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Start main service
ExecStart=/usr/local/mysql-5.7.19/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 5000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

修改”root@localhost”密码。

shell> mysql
mysql> alter user 'root'@'localhost' identified by '123456';
mysql> q

1.2.2 安装后的规范化操作

编译安装或通用二进制安装后,一般都需要做一些额外的操作,包括设置环境变量、输出头文件和库文件、设置man路径。

echo "export PATH=/usr/local/mysql/bin:$PATH" >/etc/profile.d/mysql.sh
chmod +x /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
echo "MANPATH /usr/local/mysql/man" >>/etc/man.config

echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig
ln -s /usr/local/mysql/include /usr/include/mysql

1.3 编译源码安装mysql

建议别没事找事,尝试编译装MySQL,完全是吃力不讨好的事。如果确实要编译安装,把my.cnf、服务管理脚本、编译选项等涉及到运行文件路径的项确保相同,例如pid文件、socket文件、datadir路径、log文件。并保证mysql涉及到的目录所有者和所属组都是mysql,例如默认的pid路径/var/run/mysql/。

mysql源码包下载地址:

  • MySQL 5.6源码包下载:
    https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37.tar.gz

  • MySQL 5.7源码包下载:
    https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19.tar.gz

1.3.1 编译安装过程

关于编译选项,见下文。

使用以下命令安装mysql 5.6。

yum -y install ncurses-devel cmake
tar xf ~/mysql-5.6.37.tar.gz
cd ~/mysql-5.6.37
cmake . 
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.37 
-DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci 
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii 
-DENABLED_LOCAL_INFILE=ON 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_FEDERATED_STORAGE_ENGINE=1 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 
-DWITH_FAST_MUTEXES=1 
-DWITH_EMBEDDED_SERVER=1 
-DWITH_SSL=bundled 
-DWITH_DEBUG=0 
make
make install

使用以下命令安装mysql 5.7。比上述多了最后一个boost相关设置项,不设置此项可能会cmake失败。

yum -y install ncurses-devel cmake
tar xf ~/mysql-5.7.19.tar.gz
cd ~/mysql-5.7.19
cmake . 
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.19 
-DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci 
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii 
-DENABLED_LOCAL_INFILE=ON 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_FEDERATED_STORAGE_ENGINE=1 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 
-DWITH_FAST_MUTEXES=1 
-DWITH_EMBEDDED_SERVER=1 
-DWITH_SSL=bundled 
-DWITH_DEBUG=0 
-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
make
make install

如果是centos7,则加上一项-DWITH_SYSTEMD=1,这会提供systemd脚本。

注意,上述编译选项中没有提供sysconfdir、datadir、pidfile和socket等mysql运行时文件类设置选项。虽然可以设置,但没必要,而且在做多实例的时候可能会出现问题。不过,可以考虑加上MYSQL_UNIX_ADDR项来设置socket路径,因为不设置的话其会采用默认的/tmp/mysql.sock。

1.3.2 初始化

shell> ln -s /usr/local/mysql-5.6.37 /usr/local/mysql
shell> cd /usr/local/mysql
shell> useradd -r -s /sbin/nologin mysql
shell> mkdir -p /mydata/data
shell> chown -R mysql.mysql /mydata/data
shell> chmod o-rx /mydata/data
shell> chown -R mysql.mysql /usr/local/mysql
shell> scripts/mysql_install_db --user=mysql --datadir=/mydata/data
shell> cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
shell> cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
shell> chown -R root.root /usr/local/mysql
# 修改my.cnf的datadir、socket、log和pid路径。
shell> vim /etc/my.cnf 
[mysqld]
datadir=/mydata/data
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/mydata/data/mysql.log
pid-file=/mydata/data/mysqld.pid

注:mysql 5.7建议采用如下语句进行初始化。

/usr/local/mysql/bin/mysqld --initialize-insecure --datadir=/mydata/data --user=mysql

如果是centos7,则提供systemd风格的服务管理脚本,并确认是否要修改pid文件路径。

1.3.3 规范化

输出头文件、库文件,设置PATH环境变量,设置man路径。

echo "export PATH=/usr/local/mysql/bin:$PATH" >/etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
chmod +x /etc/profile.d/mysql.sh
echo "MANPATH /usr/local/mysql/man" >>/etc/man.config
echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
ldconfig
ln -s /usr/local/mysql/include /usr/include/mysql

1.3.4 cmake编译MySQL时的选项说明

Installation Layout Options

  • -DCMAKE_INSTALL_PREFIX=dir_name   # MySQL的安装位置
  • -DINSTALL_PLUGINDIR=dir_name     # 插件安装的目录
  • -DMYSQL_DATADIR=dir_name       # MySQL的data dir
  • -DSYSCONFDIR=dir_name        # MySQL默认的配置文件(my.cnf)路径
  • -DTMPDIR=dir_name          # 临时文件存放路径,在MySQL5.6.16中才开始提供该选项

Storage Engine Options

存储引擎是插件式的,可被静态编译到MySQL服务中,也可以动态编译成模块,编译成模块时需要使用INSTALL PLUGIN语句或者–plugin-load选项来启用。但某些插件是固化的,无法指定是静态编译还是动态编译。

InnoDB,MyISAM,MERGE,MEMORY和CSV存储引擎总是默认静态编译到MySQL服务中的,在编译安装的时候无需显式指定它们。

编译存储引擎的时候,使用-DWITH_enginename_STORAGE_ENGINE=1表示静态编译到MySQL。可选的引擎有:ARCHIVE、BLACKHOLE、EXAMPLE、FEDERATED、PARTITION(分区支持引擎)、PERFSCHEMA(Performance Schema)。如:

-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1

使用-WITHOUT_enginename_STORAGE_ENGINE=1表示显式的排除这些引擎,即强制不使用它们。如:

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1

-DWITH_enginename_STORAGE_ENGINE和-WITHOUT_enginename_STORAGE_ENGINE都没有指定某些引擎的话,这些引擎默认动态编译成模块,如果无法动态编译成模块则不编译。

Feature Options

  • -DDEFAULT_CHARSET=charset_name

设置默认字符集,默认为latin1。例如,binary,ascii,big5 ,gb2312,gbk,latin1,latin2,latin5,latin7,utf8。

在cmake/character_sets.cmake文件中的CHARSETS_AVAILABLE定义了允许使用的字符集列表。

字符集的设置可以在MySQL服务启动的时候使用”–character_set_server”选项来指定。

  • -DDEFAULT_COLLATION=collation_name

设置默认的排序规则。默认排序规则为latin1_swedish_ci。使用SHOW COLLATION语句可以查看每种字符集可以使用的排序规则。

排序规则的设置可以在MySQL服务启动的时候使用–collation_server选项来指定。

  • -DENABLED_LOCAL_INFILE=bool

Whether to enable LOCAL capability in the client library for LOAD DATA INFILE.
This option controls client-side LOCAL capability, but the capability can be set on the server side at server startup with the –local-infile option.

  • -DMYSQL_TCP_PORT=port_num

指定MySQL的TCP端口,默认是3306。可以在启动服务时使用–port选项指定。

  • -DMYSQL_UNIX_ADDR=file_name

指定MySQL的套接字路径,必须是绝对路径,默认是/tmp/mysql.sock。可以在启动服务时使用–port选项指定。

  • -DWITH_EXTRA_CHARSETS=name
    Which extra character sets to include:

    • all: All character sets. This is the default.
    • complex: Complex character sets.
    • none: No extra character sets.
  • -DWITH_INNODB_MEMCACHED=bool

Whether to generate memcached shared libraries (libmemcached.so and innodb_engine.so).

  • -DWITH_SSL={ssl_type|path_name}
    The type of SSL support to include (if any) or the path name to the OpenSSL installation to use.

    • ssl_type can be one of the following values:

no: No SSL support. This is the default before MySQL 5.6.6. As of 5.6.6, this is no longer a permitted value and the default is bundled.

yes: Use the system SSL library if present, else the library bundled with the distribution.

bundled: Use the SSL library bundled with the distribution. This is the default as of MySQL 5.6.6.

system: Use the system SSL library.

  • path_name, permitted for MySQL 5.6.7 and after, is the path name to the OpenSSL installation to use. Using this can be preferable to using the ssl_type value of system, for it can prevent CMake from detecting and using an older or incorrect OpenSSL version installed on the system. (Another permitted way to do the same thing is to set the CMAKE_PREFIX_PATH option to path_name.).
    • -DWITH_ZLIB=zlib_type
  • bundled: Use thezliblibrary bundled with the distribution. This is the default.

  • system: Use the system zlib library.

二、mysql多实例配置

mysql可以实现多实例,但因为多实例会共用服务器资源,导致资源争用,在某实例某一刻资源占用很多时(高并发、慢查询),其他的实例会受到影响。

无论是rpm安装、通用二进制安装还是编译安装,都有两种方法实现多实例。

1、共用配置文件
在my.cnf中配置多个”[mysqldN]”,N是一个数字,表示MySQL服务。
启停的时候使用mysqld_multi {start|stop|restart} N。N可以是单个数字,也可以是逗号分隔的多个数字,还可以是短横线表示的范围数字。如果不是rpm包安装的,则mysqld_multi文件的路径在support-files中,将其copy到/etc/init.d下即可(没有原生态的systemd多实例服务管理脚本)。

mysqld_multi start 1,2,4-6

2、单独的配置文件和启动程序(推荐)
MySQL只需安装一次,即不同实例使用同一安装程序。但每个实例使用单独的配置文件、服务管理脚本、datadir目录和socket,并且启停mysqld服务时需要指定套接字文件。

2.1 mysql多实例配置过程

本文介绍第二种方法,并采用rpm包安装的mysql实现多实例。再次说明,无论使用何种方式安装mysql,都可以实现多实例,其实看明白下面配置的过程就知道了。

创建并设置datadir,并初始化、分别提供配置文件。

shell> mkdir -p /mydata/{3306,3307}/data
shell> chown -R mysql.mysql /mydata/{3306,3307}/data
shell> mysql_install_db --datadir=/mydata/3306/data --user=mysql
shell> mysql_install_db --datadir=/mydata/3307/data --user=mysql
shell> cp /etc/my.cnf /mydata/3306/my.cnf
shell> cp /etc/my.cnf /mydata/3307/my.cnf

如果是设置mysql 5.7的多实例,则初始化时使用如下命令替换上面的mysql_install_db。

shell> mysqld --initialize-insecure --datadir=/mydata/3306/data --user=mysql
shell> mysqld --initialize-insecure --datadir=/mydata/3307/data --user=mysql

分别修改两个配置文件。

#以下是3306实例的配置文件要修改的部分。
shell> vim /mydata/3306/my.cnf
[mysqld]
port=3306
datadir=/mydata/3306/data
socket=/mydata/3306/data/mysql.sock
server_id=1
[mysqld_safe]
log-error=/mydata/3306/data/mysqld.log
pid-file=/mydata/3306/data/mysqld.pid

#以下是3307实例的配置文件要修改部分。
shell> vim /mydata/3307/my.cnf
[mysqld]
port=3307
datadir=/mydata/3307/data
socket=/mydata/3307/data/mysql.sock
server_id=2
[mysqld_safe]
log-error=/mydata/3307/data/mysqld.log
pid-file=/mydata/3307/data/mysqld.pid

2.2 提供sysV服务管理脚本

再分别提供服务管理脚本。

  • 以下是3306实例的管理脚本/etc/init.d/mysqld3306,内容修改自原有管理脚本/etc/init.d/mysqld。
  • 由于我的示例中mysql是采用rpm安装,所以mysql的basedir为/usr,如果是编译安装或通用二进制安装,则对应修改下面脚本中的basedir变量。
  • 下面的datadir变量设置为/mydata/$port/data。请务必和上面的初始化设置和配置文件中设置的相同。
  • 将此管理脚本复制为/etc/init.d/mysqld3307,再修改下port=3307即可作为3307实例的服务管理脚本。
  • 此脚本不会初始化mysql(我把这部分代码删了)创建实例,所以启动服务前务必先初始化好对应的mysql实例。
  • 下面的脚本即可作为多实例服务管理脚本,也可以作为单实例服务管理脚本,只需将脚本名称改一改即可。
#!/bin/sh
#
# mysqld    This shell script takes care of starting and stopping
#        the MySQL subsystem (mysqld).
#
# chkconfig: 345 64 36
# description:    MySQL database server.
# processname: mysqld

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

basedir=/usr
exec="$basedir/bin/mysqld_safe"
prog="mysqld"
port=3306
datadir="/mydata/$port/data"
socketfile="$datadir/mysql.sock"
errlogfile="$datadir/mysqld.log"
mypidfile="$datadir/mysqld.pid"
cnf="/mydata/$port/my.cnf"

# Set timeouts here so they can be overridden from /etc/sysconfig/mysqld
STARTTIMEOUT=120
STOPTIMEOUT=60

# Set in /etc/sysconfig/mysqld, will be passed to mysqld_safe
MYSQLD_OPTS=

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

case $socketfile in
    /*) adminsocket="$socketfile" ;;
     *) adminsocket="$datadir/$socketfile" ;;
esac

start(){
    [ -x $exec ] || exit 5
    # check to see if it's already running
    RESPONSE=$(/usr/bin/mysqladmin --no-defaults --socket="$adminsocket" --user=UNKNOWN_MYSQL_USER ping 2>&1)
    if [ $? = 0 ]; then
    # already running, do nothing
    action $"Starting $prog: " /bin/true
    ret=0
    elif echo "$RESPONSE" | grep -q "Access denied for user"
    then
    # already running, do nothing
    action $"Starting $prog: " /bin/true
    ret=0
    else
    # Now start service
    $exec $MYSQLD_OPTS --defaults-file="$cnf" --datadir="$datadir" --socket="$socketfile" 
        --pid-file="$mypidfile" 
        --basedir="$basedir" --user=mysql >/dev/null &
    safe_pid=$!
    # Spin for a maximum of N seconds waiting for the server to come up;
    # exit the loop immediately if mysqld_safe process disappears.
    # Rather than assuming we know a valid username, accept an "access
    # denied" response as meaning the server is functioning.
    ret=0
    TIMEOUT="$STARTTIMEOUT"
    while [ $TIMEOUT -gt 0 ]; do
        RESPONSE=$(/usr/bin/mysqladmin --no-defaults --socket="$adminsocket" --user=UNKNOWN_MYSQL_USER ping 2>&1) && break
        echo "$RESPONSE" | grep -q "Access denied for user" && break
        if ! /bin/kill -0 $safe_pid 2>/dev/null; then
        echo "MySQL Daemon failed to start."
        ret=1
        break
        fi
        sleep 1
        let TIMEOUT=${TIMEOUT}-1
    done
    if [ $TIMEOUT -eq 0 ]; then
        echo "Timeout error occurred trying to start MySQL Daemon."
        ret=1
    fi
    if [ $ret -eq 0 ]; then
        action $"Starting $prog: " /bin/true
        touch $lockfile
    else
        action $"Starting $prog: " /bin/false
    fi
    fi
    return $ret
}

stop(){
    if [ ! -f "$mypidfile" ]; then
        # not running; per LSB standards this is "ok"
        action $"Stopping $prog: " /bin/true
        return 0
    fi
    MYSQLPID=`cat "$mypidfile"`
    if [ -n "$MYSQLPID" ]; then
        /bin/kill "$MYSQLPID" >/dev/null 2>&1
        ret=$?
        if [ $ret -eq 0 ]; then
        TIMEOUT="$STOPTIMEOUT"
        while [ $TIMEOUT -gt 0 ]; do
            /bin/kill -0 "$MYSQLPID" >/dev/null 2>&1 || break
            sleep 1
            let TIMEOUT=${TIMEOUT}-1
        done
        if [ $TIMEOUT -eq 0 ]; then
            echo "Timeout error occurred trying to stop MySQL Daemon."
            ret=1
            action $"Stopping $prog: " /bin/false
        else
            rm -f $lockfile
            rm -f "$socketfile"
            action $"Stopping $prog: " /bin/true
        fi
        else
        action $"Stopping $prog: " /bin/false
        fi
    else
        # failed to read pidfile, probably insufficient permissions
        action $"Stopping $prog: " /bin/false
        ret=4
    fi
    return $ret
}

restart(){
    stop
    start
}

condrestart(){
    [ -e $lockfile ] && restart || :
}


# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status -p "$mypidfile" $prog
    ;;
  restart)
    restart
    ;;
  condrestart|try-restart)
    condrestart
    ;;
  reload)
    exit 3
    ;;
  force-reload)
    restart
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
    exit 2
esac

exit $?

使用下面的命令管理两个实例:

service mysqld3306 {start|stop|status|restart}
service mysqld3307 {start|stop|status|restart}

第一次启动可能会失败,也可能会警告和log相关的项,这是正常的,之后都会正常。

2.3 提供systemd服务管理脚本

  • 以下是3306实例的管理脚本/usr/lib/systemd/system/mysqld3306.service。
  • 由于我的示例中mysql是采用rpm安装,所以mysql的basedir为/usr,如果是编译安装或通用二进制安装,则对应修改下面脚本中的basedir变量。
  • 由于rpm包安装在centos 7上,已经没有mysqld_safe命令,因此使用mysqld来启动mysql实例。
  • 将此管理脚本复制为/usr/lib/systemd/system/mysqld3307.service,再将其内的3306修改为3307即可作为3307实例的服务管理脚本。
  • 此脚本不会初始化mysql(我把这部分代码删了)创建实例,所以启动服务前务必先初始化好对应的mysql实例。
  • 下面的脚本即可作为多实例服务管理脚本,也可以作为单实例服务管理脚本,只需将脚本名称改一改即可。
  • mysql为systemd自带了多实例服务管理脚本/usr/lib/systemd/system/[email protected]。我没有采用,但其设置方法可以借鉴下。
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/mydata/3306/data/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Start main service
ExecStart=/usr/sbin/mysqld --defaults-file=/mydata/3306/my.cnf --basedir=/usr --daemonize $MYSQLD_OPTS
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 5000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

然后重载下systemd服务管理脚本。

systemctl daemon-reload

之后就可以使用下面的命令管理两个实例:

systemctl {start|stop|status|restart} mysqld3306
systemctl {start|stop|status|restart} mysqld3307

三、mariadb安装

mariadb基本上算是mysql的另一个实现,绝大多数以及基础功能上和MySQL都相同。具体到安装上,也基本完全一样。

在centos7上,直接yum install mysql-server将默认安装mariadb,如果配置了mysql的yum源,需要指定”mysql-community-server”才表示安装mysql。

以下仅提供mariadb的各项资源下载地址,具体安装方法见前文对应mysql安装方法。个人建议,将mariadb的服务启动脚本阅读一遍,和MySQL的做个比较。

mariadb的镜像站点:

[mariadb]
name=mariadb
baseurl=http://yum.mariadb.org/10.2.6/centos/6.6/$basearch/
#baseurl=http://yum.mariadb.org/10.2.6/centos/7.2/$basearch/
enabled=1
gpgcheck=0

mariadb各通用二进制版:

mariadb各种二进制版本:https://downloads.mariadb.org/mariadb/+releases/

mariadb 10.2.6 systemd版(centos7):http://ftp.hosteurope.de/mirror/archive.mariadb.org//mariadb-10.2.6/bintar-linux-systemd-x86_64/mariadb-10.2.6-linux-systemd-x86_64.tar.gz

mariadb 10.2.6 非systemd版(centos6):http://ftp.hosteurope.de/mirror/archive.mariadb.org//mariadb-10.2.6/bintar-linux-x86_64/mariadb-10.2.6-linux-x86_64.tar.gz

mariadb源码包: http://ftp.hosteurope.de/mirror/archive.mariadb.org//mariadb-10.2.6/source/mariadb-10.2.6.tar.gz

MariaDB数据库主从复制

摘要:/etc/my.cnf然后按i编辑,输入下图红框中两行数据3、退出之后重启数据库systemctl restart mariadb4、进入数据库mysql -u root -p查看…

未分类

大家好,我是Adam,前面两周讲了MariaDB的安装以及简单的安全配置,这周就深入一些,讲一点相对来说复杂的东西,两个数据库之间的主从复制,没有看前面教程的朋友可以点击下面的链接去看一下

好了,走起走起

1、需要两个数据库服务端,数据库版本一致,并且互通

2、先来配置主数据库

vi /etc/my.cnf

然后按i编辑,输入下图红框中两行数据

3、退出之后重启数据库

systemctl restart mariadb

4、进入数据库

mysql -u root -p

查看主数据库状态

show master statusG;

根据返回的信息可以得出结论,配置的主数据库文件没有问题

5、锁定所有的表,使其他人不能对表做出修改的操作

flush tables with read lock;

6、退出数据库,接下来对数据库做一个导出

mysqldump -hlocalhost -uroot -p3306 -p test > /home/test.sql

然后输入数据库密码即可

7、进入home目录,看test.sql是否已创建

8、利用scp命令把test.sql文件传到从数据库服务器上

scp test.sql [email protected]:/home/

输入从服务器密码

9、在从数据库服务器上查看文件是否上传成功

10、回到主数据库服务器,解除表的锁定

unlock tables;

11、在主数据库中把从数据库需要用的用户创建出来

grAnt replication slave on *.* to 'slave'@'192.168.2.197' identified by '123456';

‘slave’为用户名 ’192.168.2.197’为从数据库IP地址 ’123456’为密码

12、主数据库服务器配置完成,接下来配置从数据库服务器,在主数据库服务器中进入数据库界面,执行第4步操作,把界面放到旁边,待会用得到

13、操作从数据库服务器,一样的,也需要修改数据库配置文件参数

vi /etc/my.cnf

14、同样的,配置完成后重启数据库

15、进入数据库,设置主从复制

CHANGE MASTER TO MASTER_HOST='192.168.2.196',MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS= 555;

后面两项要填写的参数为第12步中显示的参数

16、开启主从复制

START SLAVE;

17、查看从数据库状态

show slave statusG;

可以看到,已经连接成功,配置完成!