docker常用命令

##安装docker 使用apt

#移除老版本

1
sudo apt-get remove docker docker-engine docker.io containerd runc

#安装

1
sudo apt-get install docker-ce docker-ce-cli containerd.io

#安装指定版本

#查看可用的版本

1
apt-cache madison docker-ce

#安装

1
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

#验证安装

1
sudo docker run hello-world

#查看docker信息

1
docker info

#避免使用sudo 加入docker用户组

1
sudo usermod -aG docker $USER

#启动服务

1
sudo service docker start

#停止服务

1
sudo service docker stop

img模板命令

#列出image文件

1
docker image ls

#删除image文件

1
docker image rm [imagename]

#拉取远程image到本地

1
docker image pull library/hello-world 或者  docker image pull hello-world

注:library是官方默认组

#运行image

1
docker container run hello-world

注:此命令实际等同于 docker image pull hello-world + docker container run

#启动容器

1
docker container start [containerID]

注:与run的区别是:start每次执行的是同一个containerID 而run每次都是新contarinerID

#停止

1
docker container stop [containerID]

#查看容器里shell输出

1
docker container logs

#进入到正在运行的容器中

1
docker container exec -it [containerID] /bin/bash

#copy 文件到本地

1
docker container cp [containerID]:/var/www/html

#强制停止容器进程

1
docker container kill [containerID]

container容器命令

#列出正在运行的容器

1
docker container ls

#删除本地容器文件

1
docker container rm [containerID]

##编写dockerfile 文件,例子

1
2
3
4
5
6
7
8
9
10
11
12
#该文件继承官方(docker官方)mysql image,版本号是5.7
FROM mysql:5.7
#运行授权用户组命令
RUN chown -R mysql:root /var/lib/mysql/
#copy本机文件到image /etc/mysql...目录
COPY my.cnf /etc/mysql/conf.d/my.cnf
#分配权限为配置文件
RUN chmod 0444 /etc/mysql/conf.d/my.cnf
#
CMD ["mysqld"]
#外部访问端口
EXPOSE 3306

#使用dockerfile 创建image文件

1
docker image build -t image-demo . or docker image build -t image-demo:0.0.1 ./demo

注:./demo/ 为创建的目录地址,:0.0.1为版本号,默认为latest

#查看刚才创建的image

1
docker image ls

#使用刚才建立的image 生成对应容器

1
docker container [--rm] run -p 9000:3306 -it image-demo /bin/bash

注:-p 9000 本机访问端口 -it:将本地shell映射到image shell,/bin/bash 运行的命令使用bash shell
–rm 运行停止后自动清除本地容器文件

##发布image

#注册账号在[hub.docker.com]hub.docker.com

#登陆

1
docker login

#为本地image标注用户名和标签

1
docker image tag image-demo:0.0.1 demo-username/image-demo:0.0.1

#发布

1
docker image push demo-username/image-demo:0.0.1

##使用docker-compose

#安装

1
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

#赋权限

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

#增加软连接

1
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

#测试安装

1
docker-compose --version

#启动

1
docker-compose up

#停止

1
docker-compose stop

#容器停止后移除容器文件

1
docker-compose rm

php7.4.4扩展开发

编写步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
mkdir /test

cd /test

wget -O https://www.php.net/distributions/php-7.4.4.tar.bz2

tar -jxvf php-7.4.4.tar.bz2


cd php7.4.4/ext

php ext_skel.php --ext my_test

cd my_test

vim my_test.c

增加 自定义函数

PHP_FUNCTION(demo_test){
// ZEND_PARSE_PARAMETERS_NOME();
php_printf("The extension %s is loaded and working!\r\n","my_test");
}

找到以下代码并修改

static const zend_function_entry my_test_functions[] = {
PHP_FE(my_test,NULL)
PHP_FE(my_test_test1, arginfo_my_test_test1)
PHP_FE(my_test_test2, arginfo_my_test_test2)
PHP_FE_END
};

修改完执行以下操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/usr/bin/php74/bin/phpize

./configure --with-php-config=/usr/bin/php74/bin/php-config

make

make test

make install

编辑php.ini

查看php配置文件 /usr/bin/php74/bin/php -i |grep php.ini

增加扩展: extension=my_test.so

查看扩展

/usr/bin/php74/bin/php -m

测试新加函数

/usr/bin/php74/bin/php -r "my_test();"

mysql 大数据查询分页

mysql 数据量过大, 导致分页查询过慢解决办法

1
2
3
4
DB::select("select a.title,a.name from 
(SELECT `id` FROM `article` WHERE `status` = ? AND `updated_at` > ? LIMIT ? OFFSET ? ) c
LEFT JOIN article a ON a.id = c.id
LEFT JOIN `detail` AS `ab` ON `a`.`id` = `ab`.`article_id` ",[1,'2018-12-01',$limit,$offset]

自定义函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* updateBigDataBySql 用SQL拼接批量更新
*
* @param array $multipleData 更新数据
*
* @return bool|int
*/
public function updateBigDataBySql($multipleData = []){
try {
if (empty($multipleData)) {
throw new \Exception("数据不能为空");
}
$tableName = $this->model->getTable();
$firstRow = current($multipleData);

$updateColumn = array_keys($firstRow);
// 默认以id为条件更新,如果没有ID则以第一个字段为条件
$referenceColumn = isset($firstRow['id']) ? 'id' : current($updateColumn);
unset($updateColumn[0]);
// 拼接sql语句
$updateSql = "UPDATE " . $tableName . " SET ";
$sets = [];
$bindings = [];
foreach ($updateColumn as $uColumn) {
$setSql = "`" . $uColumn . "` = CASE ";
foreach ($multipleData as $data) {
$setSql .= "WHEN `" . $referenceColumn . "` = ? THEN ? ";
$bindings[] = $data[$referenceColumn];
$bindings[] = $data[$uColumn];
}
$setSql .= "ELSE `" . $uColumn . "` END ";
$sets[] = $setSql;
}

$updateSql .= implode(', ', $sets);
$whereIn = collect($multipleData)->pluck($referenceColumn)->values()->all();
$bindings = array_merge($bindings, $whereIn);
$whereIn = rtrim(str_repeat('?,', count($whereIn)), ',');
$updateSql = rtrim($updateSql, ", ") . " WHERE `" . $referenceColumn . "` IN (" . $whereIn . ")";
// 传入预处理sql语句和对应绑定数据
return DB::update($updateSql, $bindings);
} catch (\Exception $e) {
return false;
}
}

mysql 生成备份文件

#脚本如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#! /bin/bash

#host
host="127.0.0.1"
# uname
user="root"
# pwd
userPWD="123456"
#port
port="3306"

# dbnames
dbNames=(dbName1 dbName2)

# now time
NOW=`date -d "now" +%Y%m%d%H`

# back path
back_path=/var/wwwroot/mysql/$NOW

# create new back data directory
mkdir $back_path
# Add authority to execute
chmod -R 777 $back_path

# When to Back up Data (one day ago)
whenDel=`date -d "-1 day" +%Y%m%d%H`

del_path=/var/wwwroot/mysql/$whenDel

#
if [ -d $del_path ];
then
rm -rf $del_path
fi

# backup
for dbName in ${dbNames[*]}
do
dumpSqlFile=$dbName-$back_path.sql.gz
mysqldump -h$host:$port -u$user -p$userPWD $dbName | gzip > $back_path/$dumpSqlFile
done

mysql容量查询

查看所有数据库容量大小

1
2
3
4
5
6
7
8
select 
table_schema as `database`,
sum(table_rows) as `records`,
sum(truncate(data_length/1024/1024, 2)) as 'dataLength(MB)',
sum(truncate(index_length/1024/1024, 2)) as 'indexLength(MB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;

查看所有数据库各表容量大小

1
2
3
4
5
6
7
8
select 
table_schema as 'database',
table_name as 'table_name',
table_rows as 'records',
truncate(data_length/1024/1024, 2) as 'dataLength(MB)',
truncate(index_length/1024/1024, 2) as 'indexLength(MB)'
from information_schema.tables
order by data_length desc, index_length desc;

查看指定数据库容量大小

1
2
3
4
5
6
7
select 
table_schema as 'database',
sum(table_rows) as 'records',
sum(truncate(data_length/1024/1024, 2)) as 'dataLength(MB)',
sum(truncate(index_length/1024/1024, 2)) as 'indexLength(MB)'
from information_schema.tables
where table_schema='mysql';

查看指定数据库各表容量大小

1
2
3
4
5
6
7
8
9
select 
table_schema as 'database',
table_name as 'table_name',
table_rows as 'records',
truncate(data_length/1024/1024, 2) as 'dataLength(MB)',
truncate(index_length/1024/1024, 2) as 'indexLength(MB)'
from information_schema.tables
where table_schema='mysql'
order by data_length desc, index_length desc;
你是本站第位访客 本站访客数人次
Fork me on GitHub