标签归档:mariadb

使用Vagrant+CentOS 7搭建PHP7开发环境(含centos7.box直接下载地址)

Vagrant是一款基于命令行的虚拟机管理软件,可以用来快速部署统一的开发环境。

下载Vagrant

https://www.vagrantup.com/downloads.html

下载CentOS 7 Box

官方box下载地址

https://app.vagrantup.com/boxes/search

第三方box下载地址

http://www.vagrantbox.es/

使用原生下载

https://app.vagrantup.com/centos/boxes/7

vagrant init centos/7
vagrant up

vagrant 配置

  config.vm.network "public_network", ip: "192.168.31.245"
  config.vm.synced_folder "d:/data", "/data"
  config.vm.synced_folder "d:/phpcode", "/phpcode"

通过下载工具下载centos 7 box

官方box文件下载地址:http://cloud.centos.org/centos/7/vagrant/x86_64/images/

百度网盘box文件下载地址

  • CentOS 7: https://pan.baidu.com/s/1kVlAz59

添加并运行box

vagrant box add centos7 CentOS-7.box
vagrant init centos7
vagrant up

基础系统安装

基本系统安装

vagrant ssh
sudo passwd vagrant
sudo yum groupinstall "Development tools" -y
sudo yum install vim gcc kernel-devel kenel-devel-`uname -r`

禁止selinux

sudo setenforce 0 

sudo vi /etc/selinux/config

SELINUX=disabled

停止防火墙

sudo systemctl disable firewalld
sudo systemctl stop firewalld

更新系统

sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum install yum-utils
sudo yum-config-manager --enable remi-php72
sudo yum update
sudo yum install php-gd php-pdo php-opcache php-fpm php-pecl-redis php-pecl-mysql php-pecl-mysql php-mbstring php-intl php-cli php-xml
sudo yum install nginx -y
sudo yum install mariadb mariadb-server -y

修改nginx配置

mkdir /data/log/nginx /data/run/nginx -p
sudo service nginx stop 
sudo vim /etc/nginx/nginx.conf

nginx.conf配置修改如下:

user vagrant;
error_log /data/log/nginx/error.log;
pid /data/run/nginx/nginx.pid;

access_log  /data/log/nginx/access.log  main;

include /data/phpcode/projectname/vagrant/nginx/app.conf;

测试nginx配置

sudo nginx -t

修改nginx service配置:

sudo vim /usr/lib/systemd/system/nginx.service

nginx.service修改内容如下:

[Service]
PIDFile=/data/run/nginx/nginx.pid

重新加载service

sudo systemctl daemon-reload
sudo systemctl start nginx

修改PHP配置

mkdir  /data/run/php-fpm/session /data/run/php-fpm/wsdlcache /data/run/php-fpm/opcache /data/log/php-fpm/ -p
sudo service php-fpm stop
sudo vim /etc/php-fpm.d/www.conf

配置内容

user = vagrant
group = vagrant
php_value[session.save_path]    = /data/run/php-fpm/session
php_value[soap.wsdl_cache_dir]  = /data/run/php-fpm/wsdlcache
php_value[opcache.file_cache]  = /data/run/php-fpm/opcache
php_admin_value[error_log] = /data/log/php-fpm/www-error.log
slowlog = /data/log/php-fpm/www-slow.log
request_slowlog_timeout = 1

重启

sudo service php-fpm stop

配置Mysql

mkdir /data/mysql /data/run/mariadb /data/log/mariadb -p
sudo service mariadb stop
sudo vim /etc/my.cnf

mysqld配置

[mysqld]
datadir=/data/mysql
socket=/usr/lib/mysql/mysql.sock

[mysqld_safe]
log-error=/data/log/mariadb/mariadb.log
pid-file=/data/run/mariadb/mariadb.pid

mysql client 配置

[client]

初始化数据库

sudo /usr/libexec/mariadb-prepare-db-dir mariadb.service

修改systemd配置

sudo vim /usr/lib/systemd/system/mariadb.service

配置内容

User=vagrant
Group=vagrant

重载systemd

sudo systemctl daemon-reload
sudo systemctl start mariadb

修改mysql账号密码

'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h localhost.localdomain password 'new-password'

# Alternatively you can run:
'/usr/bin/mysql_secure_installation'

配置composer

下载安装文件

php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

安装

php composer-setup.php

删除安装文件

php -r "unlink('composer-setup.php');"

设置全局路径(windows请按参考文档设置)

sudo mv composer.phar /usr/bin/composer

配置使用国内镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com

amazon aws linux下python环境安装

允许远程通过密码登录

给root设置一个密码,允许远程通过密码登录

chmod 400 xxx.pem
ssh -i xxx.pem ec2-user@host-ip    
sudo passwd root
sudo su -
vim /etc/ssh/sshd_config 

/etc/ssh/sshd_config内容如下

PermitRootLogin yes
PasswordAuthentication yes
UsePAM yes

然后

service sshd reload

开启epel

amazon aws如果采用redhat需要额外收授权费,目前aws官方2014.3基于centos 6,许多软件版本较旧,可以通过epel软件仓库安装一些新软件

yum-config-manager --enable epel
yum update

安装基本环境

yum groupinstall "Development tools" -y
yum install openssl-devel libxslt-devel libxml2-devel libffi-devel -y

安装mysql客户端

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

# MariaDB 5.5 CentOS repository list - created 2014-09-13 05:43 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安装MariaDB客户端

yum install MariaDB-client mysql-devel -y

安装redis客户端

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum-config-manager --enable remi
yum install redis -y

安装mongodb客户端

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

[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

安装mongodb客户端

yum update
yum install mongodb-org-shell -y

安装python环境

# yum install centos-release-SCL (aliyun only)
yum install python27*
yum install freetype-devel libjpeg-devel libpng-devel
# scl enable python27 bash (aliyun only)
virtualenv --no-site-packages /data/pyenv
source /data/pyenv/bin/activate
pip install redis cryptography sqlalchemy flask simplejson mongoengine python-amazon-product-api scrapy mysql-python gunicorn gevent

安装glusterfs客户端

#install glusterfs repo
wget -P /etc/yum.repos.d http://download.gluster.org/pub/gluster/glusterfs/LATEST/EPEL.repo/glusterfs-epel.repo
#fix it for amazon linux
sed -i 's/$releasever/6/g' /etc/yum.repos.d/glusterfs-epel.repo 

#install glusterfs
yum install -y glusterfs-fuse

#setup fstab
echo "172.31.42.77:/pcvol /data_pcvol glusterfs defaults,noatime 0 0" >> /etc/fstab

#mount
mkdir /data_pcvol
mount -a
ls /data_pcvol

挂载日志磁盘

lsblk
mkfs -t ext4 /dev/xvdf
echo '/dev/xvdf       /data_log   ext4    defaults,nofail        0       2' >> /etc/fstab
mkdir /data_log
mount -a

centos7基本系统安装(php-fpm,mariadb,nginx,epel)

安装epel

su -c 'rpm -Uvh http://mirrors.hust.edu.cn/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm'
su -c 'yum install epel-release-7-0.2'

安装mariadb,php,nginx

su -c 'yum install mariadb mariadb-server php php-cli php-fpm php-pdo nginx'

创建数据库账户

CREATE USER 'th'@'%' IDENTIFIED BY '***';
GRANT ALL PRIVILEGES ON * . * TO 'th'@'%' IDENTIFIED BY '***';
flush privileges;

使用vagrant快速搭建MariaDB Galera Cluster集群测试环境

参考

下载并安装ubuntu 12.04的32位box 初始化vagrant box

wget http://files.vagrantup.com/precise32.box
vagrant box add base Downloads/precise32.box
vagrant init

设置最大内存512

vim ~/VagrantFile

config.vm.provider :virtualbox do |vb|
    # Don't boot with headless mode
    vb.gui = false

    # Use VBoxManage to customize the VM. For example to change memory:
    vb.customize ["modifyvm", :id, "--memory", "512"]
end

配置虚拟IP

config.vm.network :private_network, ip: "10.0.1.100"

启动虚拟机修改为163源

启动虚拟机,并通过ssh登录到客户机

vagrant up
vagrant ssh

采用163.com的源

sudo vim /etc/apt/sources.list

deb http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-backports main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ precise-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse

更新源

sudo aptitude update

添加mariadb源,并安装集群版

大部分linux的二进制发行包都可以在这里找到:https://downloads.mariadb.org/mariadb/repositories/

apt-get install python-software-properties
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/5.5/ubuntu precise main'
sudo aptitude update
sudo aptitude install mariadb-galera-server mariadb-client

打包虚拟机,以便再次安装 退出客户机,回到主机界面

vargrant package --output galera-db.box
vargant box add db galera-db.box

配置虚拟机

vim ~/Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: "echo Hello"
  config.vm.define "db1" do |db1|
    db1.vm.box = "db"
    db1.vm.network :private_network, ip: "10.0.1.100"
  end

  config.vm.define "db2" do |db2|
    db2.vm.box = "db"
    db2.vm.network :private_network, ip: "10.0.1.101"
  end
end

重新加载虚拟机配置

vagrant reload

配置第一台db节点

vagrant ssh db1
sudo service mysql start
sudo mysql_secure_installation mysql -uroot -p

mysql> GRANT USAGE ON *.* to sst@'%' IDENTIFIED BY 'sstpass123';
mysql> GRANT ALL PRIVILEGES on *.* to sst@'%';
mysql> FLUSH PRIVILEGES;
mysql> quit

sudo cp /usr/share/mysql/wsrep.cnf /etc/mysql/conf.d/ sudo vim /etc/mysql/conf.d/wsrep.cnf

wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://"
wsrep_sst_auth=sst:sstpass123
wsrep_sst_method=rsync
wsrep_node_name=db1
wsrep_node_address=10.0.1.100

sudo service mysql restart

安装第二个节点

vagrant ssh db2

除了wsrep.cnf有少许不同外,其他同第一节点

wsrep_cluster_address="gcomm://10.0.1.100"
wsrep_node_name=db2
wsrep_node_address=10.0.1.101

sudo service mysql restart

测试节点同步

mysql -usst -psstpass123 -h10.0.1.100 show status like 'wsrep%'; 可以看到wsrep_connected都为ON

create database testdb;
use testdb;
create table t(id int primary key auto_increment);
insert into t values(null);
insert into t values(null);
select * from t;
exit;

mysql -usst -psstpass123 -h10.0.1.101

use testdb;
select * from t;
insert into t values(null);
insert into t values(null);
select * from t;

参考: