vagrant+docker一键搭建php+mysql开发环境

必备软件

  1. Vagrant
  2. Virtual Box

启动脚本

  1. Vagrantfile
  1. Vagrant.configure("2") do |config|
  2. config.vm.box = "ubuntu/xenial64"
  3. config.vm.network "forwarded_port", guest: 80, host: 80
  4. config.vm.network "forwarded_port", guest: 3306, host: 3306
  5. config.vm.network "private_network", ip: "10.0.0.2"
  6. config.vm.provision "shell", inline: <<-SHELL
  7. # 更换aliyun软件源
  8. cp /etc/apt/sources.list /etc/apt/sources.list.bak
  9. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > /etc/apt/sources.list
  10. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.list
  11. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.list
  12. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse' >> /etc/apt/sources.list
  13. echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' >> /etc/apt/sources.list
  14. apt-get update
  15. # 安装docker
  16. apt-get -y install apt-transport-https ca-certificates curl software-properties-common
  17. curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
  18. add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
  19. apt-get -y update
  20. apt-get -y install docker-ce docker-compose
  21. # 使用aliyun加速docker镜像
  22. mkdir -p /etc/docker
  23. echo '{' > /etc/docker/daemon.json
  24. echo ' "registry-mirrors": ["https://kag9wqej.mirror.aliyuncs.com"]' >> /etc/docker/daemon.json
  25. echo '}' >> /etc/docker/daemon.json
  26. systemctl daemon-reload
  27. systemctl restart docker
  28. usermod -G docker vagrant
  29. cd /vagrant
  30. docker-compose up -d
  31. # 开机自启动
  32. echo '#!/bin/bash' > /etc/rc.local
  33. echo 'sleep 10' >> /etc/rc.local
  34. echo 'cd /vagrant' >> /etc/rc.local
  35. echo 'docker-compose up -d' >> /etc/rc.local
  36. echo 'exit 0' >> /etc/rc.local
  37. sudo rm /bin/sh
  38. sudo ln -s /bin/bash /bin/sh
  39. SHELL
  40. end
  1. docker-compose.yml
  1. version: '2'
  2. services:
  3. mysql:
  4. image: mysql
  5. environment:
  6. - MYSQL_ROOT_PASSWORD=root
  7. volumes:
  8. - "./mysql:/var/lib/mysql"
  9. ports:
  10. - '3306:3306'
  11. networks:
  12. - bridge
  13. www:
  14. build: .
  15. volumes:
  16. - "./wwwroot/www:/app"
  17. - "./wwwroot/static/Uploads:/app/Uploads"
  18. environment:
  19. - VIRTUAL_HOST="你要绑定的域名"
  20. - PHP_DB_HOST=mysql
  21. - PHP_DB_NAME=你的数据库名称
  22. - PHP_DB_USER=root
  23. - PHP_DB_PASSWORD=root
  24. networks:
  25. - bridge
  26. lb:
  27. image: 'dockercloud/haproxy'
  28. volumes:
  29. - /var/run/docker.sock:/var/run/docker.sock
  30. links:
  31. - www
  32. ports:
  33. - '80:80'
  34. networks:
  35. - bridge
  36. networks:
  37. bridge:
  1. Dockerfile
  1. FROM php:5.6-apache
  2. # 安装扩展
  3. COPY ./soft/*.tgz /soft/
  4. WORKDIR /soft
  5. RUN pecl install redis-3.1.6.tgz \
  6. && pecl install xdebug-2.5.5.tgz \
  7. && docker-php-ext-enable redis xdebug \
  8. && docker-php-ext-install pdo_mysql \
  9. && docker-php-ext-install mysqli \
  10. && rm -rf /soft
  11. # URL重写
  12. RUN a2enmod rewrite
  13. # 安装composer
  14. ADD ./soft/composer.phar /usr/local/bin/composer
  15. RUN chmod 755 /usr/local/bin/composer \
  16. && composer config -g repo.packagist composer https://packagist.phpcomposer.com
  17. # 安装代码
  18. COPY ./wwwroot /app/
  19. # 修改网站主目录
  20. ENV APACHE_DOCUMENT_ROOT /app
  21. RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
  22. RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
  23. # 可写权限
  24. RUN chmod -R 777 /app/
  25. # 默认工作目录
  26. WORKDIR /app
  27. # 暴露工作端口
  28. EXPOSE 80 443 22

mysql新建远程用户后不能远程连接的问题

编辑 /etc/mysql/my.cnf 注释掉 bind-address = 127.0.0.1
然后重启mysql, 就可以了

mysql的workbench导出数据报错问题

MysqlWorkbench6.0之后的版本使用export data时会遇到这个错误

问题

  1. Running: /Applications/MySQLWorkbench.app/Contents/MacOS/mysqldump --defaults-file
  2. ...
  3. mysqldump: [ERROR] unknown variable 'delayed-insert=FALSE'
  4. ...

原因

我们看错误报告显示这个变量 ‘delayed-insert=FALSE’ 不被识别,workbench默认配置里会开启这个变量并且默认值是false。如果我们用不到这个变量可以更改配置文件,注释掉这个变量来解决

解决步骤

  1. 找到配置文件目录:InstallPath/modules
  2. 打开python格式的输出配置文件:wb_admin_export_options.py
  3. 找到所在行:”delayed-insert”:[“Write INSERT DELAYED statements rather than …
  4. 在行开头插入#号注释改行
  5. 保存文件,重启Workbench

配置文件所在位置

windows

C:\Program Files\MySQL\MySQL Workbench 6.3 CE\modules

Mac

Applications -> MYSQLWorkbench.app/Contents/Resources/plugins.
Note: 在应用上右击选择 Show Package contents 进入应用目录

Ubuntu

/usr/lib/mysql-workbench/modules/wb_admin_export_options.py