首页 > 数据库 >二. 数据库的安装(Ubuntu系统)

二. 数据库的安装(Ubuntu系统)

时间:2022-12-06 20:57:22浏览次数:62  
标签:src os 安装 数据库 Ubuntu etc usr mysql local

1. shell脚本

需要用source install_mysql_8026.sh

点击查看代码
#!/bin/bash
Dir=/usr/local/src
function AddGroup(){
	group=mysql
	egrep "^$group" /etc/group
	if [ $? -ne 0 ]
	then
		groupadd -r mysql
	fi  
}

function AddUser(){
	user=mysql
	egrep "^${user}" /etc/passwd
	if [ $? -ne 0 ]
	then
	    useradd -r -g mysql -s /bin/false mysql
  fi
}

function Unpackage(){
  cd ${Dir} && tar xf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz \
	     && mv mysql-8.0.26-linux-glibc2.12-x86_64 mysql8026 \
       && rm -fr ${Dir}/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz \
	     && ln -s /usr/local/src/mysql8026 /usr/local/src/mysql
}

function ChangeEnvironment(){
  echo 'export PATH=/usr/local/src/mysql/bin:$PATH' >> /etc/profile
  source /etc/profile
  mysql -V
  if [ $? -eq 0 ]
  then
	  echo "安装成功"
  else
	  echo "安装失败" &&  exit 1
  fi
}

function MakeDir(){
  mkdir /data/mysql/data -p  
  chown -R mysql:mysql /data/*
}

function EditFile(){
cat >>/etc/my.cnf<< EOF 
[mysqld] 
user=mysql
basedir=/usr/local/src/mysql8026
datadir=/data/mysql/data 
socket=/tmp/mysql.sock 
server_id=20   
port=3306  
[mysql]
socket=/tmp/mysql.sock 
EOF
}

function InitMysql(){
  apt install libaio1 -y
  mysqld --initialize-insecure
}

function EditService(){
cat >/lib/systemd/system/mysqld.service<<EOF
[Unit] 
Description=MySQL Server 
Documentation=man:mysqld(8) 
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html 
After=network.target 
After=syslog.target 
[Install] 
WantedBy=multi-user.target 
[Service] 
User=mysql 
Group=mysql 
ExecStart=/usr/local/src/mysql8026/bin/mysqld --defaults-file=/etc/my.cnf 
LimitNOFILE=5000
EOF
}

function StartMysql(){
systemctl start mysqld.service && systemctl enable mysqld.service && systemctl status mysqld.service
}

main(){
 AddGroup
 AddUser
 Unpackage
 ChangeEnvironment
 MakeDir
 EditFile
 InitMysql
 EditService
 StartMysql
}

main

2. py脚本

点击查看代码
#!/bin/python
import os
from pathlib import Path
import grp
import pwd
import tarfile
import sys
def AddGroup():
    group_name = "mysql"
    all_groups_list = grp.getgrall()
    list1 = []
    for i in all_groups_list:
        list1.append(i[0])
    if group_name in list1:
        print("{} 组已经存在".format(group_name))
        sys.exit()
    else:
        print("{}组不存在,即将创建".format(group_name))
        os.system("groupadd -r {}".format(group_name))
def AddUser():
    user="mysql"
    list1=[]
    username_list=pwd.getpwall()
    for i in username_list:
        list1.append(i[0])
    if user in list1:
        print("{}用户已经存在,请使用别的用户".format(user))
        sys.exit()
    else:
        print("{}用户不存在,即将创建".format(user))
        os.system("useradd -r -g mysql -s /bin/false mysql")

def Unpackage():
    p = Path("/usr/local/src/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz")
    with tarfile.open(p, "r", encoding="utf-8") as f:
        f.extractall(p.parent)
    os.remove(p)
    p=Path("/usr/local/src/mysql-8.0.26-linux-glibc2.12-x86_64")
    p.rename("/usr/local/src/mysql8026")
    

def SetEnv():
    p = Path("/etc/profile")
    with open(p,mode="a",encoding="utf-8") as f:
        f.write("export PATH=/usr/local/src/mysql8026/bin:$PATH" + "\n")
    os.system('. /etc/profile')


def MakeDir():
    Path("/data/mysql/data").mkdir(parents=True, exist_ok=True)
    os.system("chown -R mysql:mysql /data/*")

def EditFile():
    filename = Path("/etc/my.cnf")
    with open(filename,"w",encoding="utf-8") as f:
        f.write("[mysqld]\n"
                "user=mysql\n"
                "basedir=/usr/local/src/mysql8026\n"
                "datadir=/data/mysql/data\n"
                "socket=/tmp/mysql.sock\n"
                "server_id=20\n"
                "port=3306\n"
                "[mysql]\n"
                "socket=/tmp/mysql.sock\n"
              )

def InitMysql():
    os.system("apt install libaio1 -y")
    os.system("/usr/local/src/mysql8026/bin/mysqld --initialize-insecure")

def EditService():
    filename=Path("/lib/systemd/system/mysqld.service")
    with open(filename,"w",encoding="utf-8") as f:
        f.write("[Unit]\n"
                "After=network.target\n"
                "After=syslog.target\n"
                "[Install]\n"
                "WantedBy=multi-user.target\n"
                "[Service]\n"
                "User=mysql\n"
                "Group=mysql\n"
                "ExecStart=/usr/local/src/mysql8026/bin/mysqld --defaults-file=/etc/my.cnf\n"
                "LimitNOFILE=5000\n"
                )
def StartMysql():
    os.system("systemctl start mysqld.service && systemctl enable mysqld.service && systemctl status mysqld.service")

if __name__ == '__main__':
  AddGroup()
  AddUser()
  Unpackage()
  SetEnv()
  MakeDir()
  EditFile()
  InitMysql()
  EditService()
  StartMysql()
执行玩以后需要执行source /etc/profile

标签:src,os,安装,数据库,Ubuntu,etc,usr,mysql,local
From: https://www.cnblogs.com/yufenchi/p/16960491.html

相关文章

  • 数据库(js部分讲解)
    BOM操作BOM(BrowserObjectModel)是指浏览器对象模型,它使JavaScript有能力与浏览器进行“对话”window.open() window.open('https://www.sogo.com/','','width=800......
  • ubuntu 扩充交换空间
    1、关闭交换空间sudoswapoff-a2、扩充交换空间大小,count=8就是8G=1Gx8sudoddif=/dev/zeroof=/swapfilebs=1Gcount=83、设置权限sudochmod600/swapf......
  • C#连接MySQL数据库,进行增删改查
    一、创建数据库学生表createdatabasetest;createtablestudent( sNovarchar(50)primarykey, sNamevarchar(50)notnull)补充:数据的增删改查相关操......
  • idea java 关于mysql数据库连接的相关步骤
    1.首先在你的项目中建立一个lib文件夹(表示加入依赖)(资源在上面)       2.选择这个项目的Properties在下面页面中添加Libraries进入这个项目jar配置到项......
  • Mysql8.0.25安装过程
    步骤一.下载mysql8.0.25步骤二.下载完解压后如下图:  图片中显示可知,并没有exe用来安装,那么请看第三步步骤三.创建一个txt文本文件,将下边的......
  • vue安装node-sass总是报错
    第一种方式安装会直接安装最新版,sass-loader和node-sass版本可能会匹配报错,因为sass-loader可能会版本过高。如果报错可以指定以下版本则会匹配成功npminstallsass--s......
  • 使用docker安装RocketMQ
    1.创建namesrv服务拉取镜像dockerpullrocketmqinc/rocketmq创建namesrv数据存储路径mkdir-p/docker/rocketmq/data/namesrv/logs/docker/rocketmq/data/namesrv/st......
  • Ubuntu22.04下安装docker启动失败 报:start request repeated too quickly for docker.
      在网上查了解决办法,基本都是/etc/docker/daemon.json里面出现格式错误或者docker的数据目录是挂载出来的但是没有自动挂载,重启服务器后,docker无法启动。确实大部......
  • Navicat Premium 16 (mac,windows均有)安装破解激活图文教程【2022年12月6日亲测有效】
    Navicat16破解Navicat16Mac和Windows最新版破解教程【永久激活,2022年12月16日亲测有效】windows版本破解下载安装包和破解工具navicat16最新版:点击前往下载navicat1......
  • docker 容器安装 centos7 部署程序打包成镜像
    1、安装dockeryum install dockerecho"{"registry-mirrors":["https://01sy6s7g.mirror.aliyuncs.com"]}" >  /etc/docker/daemon.jsonsystemctl restart ......