From 6dcdfc2fc4004a68cfaaf557f7f729268b9e3ba4 Mon Sep 17 00:00:00 2001 From: Shaowei Song <1498430017@qq.com> Date: Fri, 9 Dec 2022 11:14:35 +0800 Subject: [PATCH] [demos] Add MySQL demo --- demos/mysql/.gitignore | 4 ++ demos/mysql/README.md | 28 ++++++++++++++ demos/mysql/apply-mysql-to-occlum.patch | 28 ++++++++++++++ demos/mysql/dl_and_build_mysql.sh | 50 +++++++++++++++++++++++++ demos/mysql/my.cnf | 19 ++++++++++ demos/mysql/mysql.yaml | 34 +++++++++++++++++ demos/mysql/run_mysql_client.sh | 32 ++++++++++++++++ demos/mysql/run_mysql_server.sh | 42 +++++++++++++++++++++ 8 files changed, 237 insertions(+) create mode 100644 demos/mysql/.gitignore create mode 100644 demos/mysql/README.md create mode 100644 demos/mysql/apply-mysql-to-occlum.patch create mode 100755 demos/mysql/dl_and_build_mysql.sh create mode 100644 demos/mysql/my.cnf create mode 100644 demos/mysql/mysql.yaml create mode 100755 demos/mysql/run_mysql_client.sh create mode 100755 demos/mysql/run_mysql_server.sh diff --git a/demos/mysql/.gitignore b/demos/mysql/.gitignore new file mode 100644 index 00000000..2df61fd8 --- /dev/null +++ b/demos/mysql/.gitignore @@ -0,0 +1,4 @@ +occlum_instance +mysql_src +boost* +*tar* diff --git a/demos/mysql/README.md b/demos/mysql/README.md new file mode 100644 index 00000000..9e1599fd --- /dev/null +++ b/demos/mysql/README.md @@ -0,0 +1,28 @@ +# Run MySQL on Occlum + +[`MySQL`](https://www.mysql.com/) is a widely used open-source relational database management system (RDBMS). + +### Build and install +``` +./dl_and_build_mysql.sh +``` +This command downloads MySQL-8.0.31 source code and builds from it. +When completed, all MySQL related binaries and tools are installed. + +### Run server and client + +#### Initialize and start the MySQL server +``` +./run_mysql_server.sh +``` +This command initializes and runs the server (using `mysqld`) in Occlum. +When completed, the server starts to wait connections. + +#### Start the MySQL client and send simple queries +``` +./run_mysql_client.sh +``` +This command starts the client (using `mysql`) in Occlum and test basic query SQLs. + +The network protocol between client and server uses uds(unix domain socket) by default. +More configuration can be tuned and applied in `my.cnf`. diff --git a/demos/mysql/apply-mysql-to-occlum.patch b/demos/mysql/apply-mysql-to-occlum.patch new file mode 100644 index 00000000..946ad219 --- /dev/null +++ b/demos/mysql/apply-mysql-to-occlum.patch @@ -0,0 +1,28 @@ +--- mysql_src/mysys/my_rdtsc-origin.cc 2022-12-08 09:14:01.171126659 +0000 ++++ mysql_src/mysys/my_rdtsc.cc 2022-12-08 09:08:52.016352814 +0000 +@@ -299,16 +299,16 @@ + */ + + ulonglong my_timer_ticks(void) { +-#if defined(HAVE_SYS_TIMES_H) && defined(HAVE_TIMES) +- { +- struct tms times_buf; +- return (ulonglong)times(×_buf); +- } +-#elif defined(_WIN32) +- return (ulonglong)GetTickCount(); +-#else ++// #if defined(HAVE_SYS_TIMES_H) && defined(HAVE_TIMES) ++// { ++// struct tms times_buf; ++// return (ulonglong)times(×_buf); ++// } ++// #elif defined(_WIN32) ++// return (ulonglong)GetTickCount(); ++// #else + return 0; +-#endif ++// #endif + } + + /** diff --git a/demos/mysql/dl_and_build_mysql.sh b/demos/mysql/dl_and_build_mysql.sh new file mode 100755 index 00000000..542e47dd --- /dev/null +++ b/demos/mysql/dl_and_build_mysql.sh @@ -0,0 +1,50 @@ +#!/bin/bash +set -e + +BLUE='\033[1;34m' +NC='\033[0m' +echo -e "${BLUE}Start installing dependencies.${NC}" + +# Prepare environment +DEPS="libnuma-dev libboost-all-dev" + +apt-get update +apt-get install -y ${DEPS} + +BOOST="boost_1_77_0" +wget https://boostorg.jfrog.io/artifactory/main/release/1.77.0/source/${BOOST}.tar.bz2 +tar --bzip2 -xf ${BOOST}.tar.bz2 +pushd ${BOOST} +./bootstrap.sh --prefix=/usr --with-python=python3 && +./b2 stage -j4 threading=multi link=shared +./b2 install threading=multi link=shared +popd + +echo -e "${BLUE}Finish installing dependencies.${NC}" + +echo -e "${BLUE}Start building mysql from src.${NC}" + +# Download released tarball +VERSION="8.0.31" +TARBALL="mysql-${VERSION}.tar.gz" +wget https://github.com/mysql/mysql-server/archive/refs/tags/${TARBALL} +rm -rf mysql_src && mkdir mysql_src +tar -xf ${TARBALL} -C mysql_src --strip-components 1 + +# Make modification to +# 1. Disable `times` syscall +patch -s -p0 < apply-mysql-to-occlum.patch + +# Build and install +pushd mysql_src +mkdir bld && cd bld + +cmake -j$(nproc) .. -DCMAKE_CXX_FLAGS="-fpic -pie" -DCMAKE_C_FLAGS="-fpic -pie" + +CC="-fpic -pie" CXX="-fpic -pie" make -j$(nproc) + +make install -j$(nproc) +cd .. + +echo -e "${BLUE}Finish building mysql from src.${NC}" +popd diff --git a/demos/mysql/my.cnf b/demos/mysql/my.cnf new file mode 100644 index 00000000..6b905c62 --- /dev/null +++ b/demos/mysql/my.cnf @@ -0,0 +1,19 @@ +# MySQL configure file + +[client] +socket = /tmp/mysql.sock +port = 3306 +bind-address = 127.0.0.1 + +[mysqld] +socket = /tmp/mysql.sock +port = 3306 +bind-address = 127.0.0.1 +skip-networking = 0 +skip_ssl = 0 +mysqlx = 0 +wait_timeout = 60 +interactive_timeout = 120 + +[mysql] +connect_timeout = 2 diff --git a/demos/mysql/mysql.yaml b/demos/mysql/mysql.yaml new file mode 100644 index 00000000..ce524c6b --- /dev/null +++ b/demos/mysql/mysql.yaml @@ -0,0 +1,34 @@ +includes: + - base.yaml +# mysql +targets: + # copy bins + - target: /bin + copy: + - files: + # server tools + - /usr/local/mysql/bin/mysqld + # client tools + - /usr/local/mysql/bin/mysql + - /usr/local/mysql/bin/mysqladmin + - /usr/local/mysql/bin/mysqlshow + - target: /etc + copy: + - files: + - ../my.cnf + - target: /etc + copy: + - files: + - /etc/localtime + - target: /opt/occlum/glibc/etc + copy: + - files: + - /etc/localtime + - target: /opt/occlum/glibc/lib + copy: + - files: + - /usr/local/mysql/lib/mysqlrouter/private/libprotobuf-lite.so.3.19.4 + - target: / + copy: + - files: + - /usr/local/mysql/bin/mysqld diff --git a/demos/mysql/run_mysql_client.sh b/demos/mysql/run_mysql_client.sh new file mode 100755 index 00000000..ac431b28 --- /dev/null +++ b/demos/mysql/run_mysql_client.sh @@ -0,0 +1,32 @@ +#!/bin/bash +set -e + +GREEN='\033[1;32m' +NC='\033[0m' + +MYSQL=mysql +MYSQLSHOW=mysqlshow +MYSQLADMIN=mysqladmin + +# Need to wait until server is ready +echo -e "${GREEN}Need to wait mysql server to start${NC}" + +pushd occlum_instance + +echo -e "${GREEN}Run mysql client on Occlum${NC}" + +# Use unix domain socket +occlum exec /bin/${MYSQLADMIN} version + +occlum exec /bin/${MYSQLSHOW} + +occlum exec /bin/${MYSQL} -e "SELECT User, Host, plugin FROM mysql.user" + +echo -e "${GREEN}Run mysql client on host${NC}" + +# Use TCP/IP +/usr/local/mysql/bin/${MYSQLSHOW} -h 127.0.0.1 -P 3306 + +occlum stop + +popd diff --git a/demos/mysql/run_mysql_server.sh b/demos/mysql/run_mysql_server.sh new file mode 100755 index 00000000..2265cd72 --- /dev/null +++ b/demos/mysql/run_mysql_server.sh @@ -0,0 +1,42 @@ +#!/bin/bash +set -e + +GREEN='\033[1;32m' +NC='\033[0m' + +SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +bomfile=${SCRIPT_DIR}/mysql.yaml + +MYSQL=mysql +MYSQLD=mysqld + +# 1. Init Occlum instance +rm -rf occlum_instance && occlum new occlum_instance +pushd occlum_instance + +new_json="$(jq '.resource_limits.user_space_size = "3500MB" | + .resource_limits.kernel_space_heap_size ="3000MB" | + .resource_limits.max_num_of_threads = 96' Occlum.json)" && \ +echo "${new_json}" > Occlum.json + + +# 2. Copy files into Occlum instance and build +rm -rf image +copy_bom -f $bomfile --root image --include-dir /opt/occlum/etc/template + +occlum build + +# 3. Run the program +echo -e "${GREEN}Run mysql server (mysqld) on Occlum${NC}" + +occlum start + +echo -e "${GREEN}mysql server initialize${NC}" + +occlum exec /bin/${MYSQLD} --initialize-insecure --user=root + +echo -e "${GREEN}mysql server start${NC}" + +occlum exec /bin/${MYSQLD} --user=root + +popd