Ubuntu running docker, connect mysql

First, build the project environment

1. Docker mounted in Ubuntu

Update apt source index of ubuntu

sudo apt-get update

Apt to allow the installation package repository over HTTPS

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

Add Docker official GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Set Docker Stable warehouse

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

After adding warehouse, update apt source index

sudo apt-get update

Install the latest version Docker CE (Community Edition)

sudo apt-get install docker-ce

Check the Docker CE is installed correctly

sudo docker run hello-world

The following message appears indicating that the installation was successful

In order to avoid each input commands sudo, you can set user privileges, attention must log out again after execution

sudo usermod -a -G docker $USER

Start docker

sudo service docker start

 

2. Installation Docker-compose

1. Go to https://github.com/docker/compose/releases view the latest version, the current version 1.23.1

sudo curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

 

2. Set permissions

sudo chmod +x /usr/local/bin/docker-compose

 

3. Check whether the installation is successful

docker-compose --version

 If the message [12147] Can not open self / usr / local / bin / docker-compose or archive /usr/local/bin/docker-compose.pkg should not download the full package, 1,2,3 steps to do it again.

3. Download the background image

http://git.internal.taqu.cn/dev-tools/docker

Ubuntu mirrored into the system, extract to docker directory, it will produce the following five files (the rectangle)

cd ~/docker/

 

4. Run the sh script to extract the image file

bash make_xy.sh XXX 8888 ~/touch_code/bbs ~/logs

XXX is the name of your project is my project name is grow_score

bash make_xy.sh grow_score 8888 ~/touch_code/bbs ~/logs

It will automatically generate the remaining folder FIG.

 

Into the project you create, modify the path yaml file

 

 

      - /home/liugang/touch_code/grow_score:/data/html/grow_score
      - /home/liugang/touch_code/xyframework:/data/html/xyframework
      - /home/liugang/logs/php-fpm:/data/logs/php-fpm
      - /home/liugang/logs/sfpm:/data/logs/sfpm
      - /home/liugang/logs/scli:/data/logs/scli

 

关闭Nginx中的数据加密,在www.conf中添加 gzip off;

 

 

 

 

 

打开控制台,执行框架

sudo make run

 

makefile文件中有项目执行指令 

sudo make stop停止项目等等

5.git拉取xy框架和项目空框架

项目空框架:http://git.internal.taqu.cn/common/privilege/tree/branch_52week_191224

xy框架:http://git.internal.taqu.cn/framework/xy

 

将两个框架内部文件分别对应文件名放置到 ~ 文件夹中的touch_code中

 

 

 

 

 

 此时会生成如下两图

 

 

 

 

 

 包名就是项目名要,跟yaml配置一致

1代表版本v1

config是配置文件

service是代码放置的地方

 

 

 

<?php

class GrowScoreService extends BaseService
{
    public function getLevel(){
        return '1';
    }
}
GrowScoreService

 

6.更改ms中网管配置

ifconfig

查看虚拟机ip

    {
      "addrs": [{
      "url": "http://10.10.30.161:8888/",
      "env": 0,
      "protocol": "json",
      "stop": false
      }],
      "type": "php",
      "name": "grow_score"
    }

7.前端调用

SoaClient.getSoa(‘grow_score’, 'v1/growScore').getLevel()
grow_score项目名  v1版本号   growScore 代码GrowScoreService  getLevel()方法。 
 

网址直接调用 :host:port/v1/Soa/jService?service=xxx&method=xxx&form=xxx

  • form是soa的参数的base64
     
  • soa参数是json数组的格式
     
  • 比如add(1,2) 参数是[1,2]

http://10.10.30.161:8888/v1/Soa/jService?service=growScore&method=getLevel

 

PS:注意php文件名大小写要和路径一致

 

二、连接数据库

1.安装mysql

sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install libmysqlclient-dev

安装成功后可以通过下面的命令测试是否安装成功:

sudo netstat -tap | grep mysql

现在设置mysql允许远程访问,首先编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf:

sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

注释掉bind-address = 127.0.0.1

保存退出,然后进入mysql服务,执行授权命令:

进入数据库,默认密码123456

mysql -uroot -p

 

grant all on *.* to root@'%' identified by '你的密码' with grant option;

  

flush privileges;

  

然后执行quit命令退出mysql服务,执行如下命令重启mysql:

service mysql restart

 

这样,我们的数据库就可以被远程连接了。

2.修改php.ini

sudo docker ps

  找到docker中运行的php

 

1d778593e84d = xxxxxxxx

docker exec -it xxxxxxxxxx /bin/bash

  

cd app/program/php-7.1.8/conf/

  

 

 修改php.ini中的default.socket

查询default.socket相当于Ctrl+F,按N下一行

 

/default.socket

 

pdo_mysql.default.socket =/var/run/mysqld/mysqld.sock

mysqli.default_socket =/var/run/mysqld/mysqld.sock

 

 

 

将扩展打开 去掉这5个前边的分号

 

 

 

 

保存退出。

 

在数据库中运行这两个即可连接数据库

$ sudo mysql -u root # I had to use "sudo" since is new installation

mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;

$ service mysql restart

  

$ sudo mysql -u root # I had to use "sudo" since is new installation

mysql> USE mysql;
mysql> CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY '';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
mysql> UPDATE user SET plugin='auth_socket' WHERE User='YOUR_SYSTEM_USER';
mysql> FLUSH PRIVILEGES;
mysql> exit;

$ service mysql restart

 

连接数据库代码:

 

 

<?php
//连接数据库
class MysqlConectService
{
    public function connect(){

// $serverName = '10.10.30.161';
// $databaseName = 'test';
// $serverPort = '3306';

// $username = 'root';
// $password = '123456';

//if (class_exists('PDO')) {
//   echo "pdo";

//    try {
//        $conn = new PDO("mysql:host=" . $serverName . ";dbname=" . $databaseName . ";port=" . $serverPort, $username, $password);
//    if ($conn->connect_error) {
//            return 'error';
//        }else{
//           return 'success';
//        }
//    } catch (PDOException $e) {
//        echo 'Connection failed: ' . $e->getMessage();
//    }
//}
    $db            = db();
        $db->tableName = 'levelconfig';
    $where['level_id'] = 1;
        return  $db->find($where);
    }
}
MysqlConectService

1.进入https://github.com/docker/compose/releases 查看最新版本,当前版本为1.23.1

sudo curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

 

2.设置权限

sudo chmod +x /usr/local/bin/docker-compose

 

3.查看是否安装成功

docker-compose --version

 如果报 [12147] Cannot open self /usr/local/bin/docker-compose or archive /usr/local/bin/docker-compose.pkg  应该是包没下载完整,1、2、3步骤再来一遍。

Guess you like

Origin www.cnblogs.com/LiuOOP/p/12103995.html