使用ansible源码编译安装Nginx1.28和二进制安装MySQL8.0

发布于:2025-07-05 ⋅ 阅读:(16) ⋅ 点赞:(0)

实验环境

一、编译安装Nginx1.28

192.168.10.40:

上传nginx-1.28.0.tar.gz源码包到/root目录

ansible -m copy -a 'src=/root/nginx-1.28.0.tar.gz dest=/root' 192.168.10.41

ansible -m dnf -a 'name=gcc,gcc-c++,make,automake,pcre,pcre-devel,zlib,zlib-devel,openssl-devel state=present' 192.168.10.41

ansible -m unarchive -a 'src=/root/nginx-1.28.0.tar.gz dest=/root remote_src=yes ' 192.168.10.41

ansible -m shell -a 'chdir=/root/nginx-1.28.0 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_stub_status_module && make && make install' 192.168.10.41

创建nginx的systemctl管理文件

vim nginx.service

添加:

[Unit]

Description=NGINX HTTP and reverse proxy server

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PIDFile=/usr/local/nginx/logs/nginx.pid

PrivateTmp=true

[Install]

WantedBy=multi-user.target

ansible -m copy -a 'src=/root/nginx.service dest=/etc/systemd/system/nginx.service' 192.168.10.41

ansible -m shell -a 'systemctl daemon-reload' 192.168.10.41

ansible -m service -a 'name=nginx state=started' 192.168.10.41

使用浏览器访问

192.168.10.41

二、二进制安装MySQL8.0

192.168.10.40:

上传mysql-8.0.42-linux-glibc2.17-x86_64-minimal.tar.xz二进制包到/root目录

ansible -m copy -a 'src=/root/mysql-8.0.42-linux-glibc2.17-x86_64-minimal.tar.xz dest=/root' 192.168.10.41

ansible -m unarchive -a 'src=/root/mysql-8.0.42-linux-glibc2.17-x86_64-minimal.tar.xz dest=/root remote_src=yes' 192.168.10.41

ansible -m shell -a 'mv /root/mysql-8.0.42-linux-glibc2.17-x86_64-minimal /usr/local/mysql' 192.168.10.41

ansible -m shell -a 'echo -e "export MYSQL_HOME=/usr/local/mysql\nexport PATH=\$MYSQL_HOME/bin:\$PATH" >> /etc/profile' 192.168.10.41

ansible -m shell -a 'source /etc/profile' 192.168.10.41

ansible -m shell -a 'mkdir -p /usr/local/mysql/data' 192.168.10.41

ansible -m group -a 'name=mysql state=present system=yes' 192.168.10.41

ansible -m user -a 'name=mysql system=yes shell=/bin/false group=mysql create_home=false' 192.168.10.41

ansible -m shell -a 'chown -R mysql:mysql /usr/local/mysql/' 192.168.10.41

创建MySQL配置文件

vim my.cnf

添加:

[mysqld]

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

pid-file=/usr/local/mysql/data/mysqld.pid

log-error=/usr/local/mysql/data/mysql.err

socket=/tmp/mysql.sock

ansible -m copy -a 'src=/root/my.cnf dest=/etc' 192.168.10.41

ansible -m shell -a '/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data' 192.168.10.41

ansible -m shell -a '/usr/local/mysql/support-files/mysql.server start' 192.168.10.41

ansible -m dnf -a 'name=ncurses-compat-libs' 192.168.10.41

ansible -m shell -a 'temp_pass=$(grep "temporary password" /usr/local/mysql/data/mysql.err | awk "{print \$NF}"); /usr/local/mysql/bin/mysqladmin -uroot -p"$temp_pass" password "123456"' 192.168.10.41

192.168.10.41:

登录验证

mysql -uroot -p123456