Kali中安装mysql自动安装脚本
查看创建的mysql用户:
cat /etc/passwd | grep mysql
安装步骤:
1.先删除mariadb
apt-get autoremove --purge mariadb-client-core-10.3 apt-get autoremove --purge
2.把mysql的压缩包文件放入到/usr/local
3.执行解压命令
tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
4.修改文件名
mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql
5.创建用户:
userdel mysql groupadd mysql useradd -r -g mysql mysql chown -R mysql:mysql mysql
6.切换到mysql用户:
su mysql
7.进入到安装目录:
cd mysql
8.执行初始数据命令:(这里安装的5.7 其他版本可能不一样)参考:https://www.cnblogs.com/xuanzhi201111/p/5148113.html
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
9.exit退出mysql用户
10.拷贝文件:
cp support-files/mysql.server /etc/init.d/mysql cp support-files/my-default.cnf /etc/my.cnf (5.7没有这个文件,自己找一个) cp bin/mysql /usr/bin/
11.创建log文件输出路径:
mkdir log touch log/mysqld.log chown -R mysql:mysql log/ chmod -R 755 log/mysqld.log
12.修改配置文件vim /etc/init.d/mysql
basedir=/usr/local/mysql datadir=/usr/local/mysql/data (这个拷贝文件中如果添加,就不需要改了)
修改下配置文件:vim /etc/my.cnf
[client] default-character-set=utf8 [mysqld] character-set-server=utf8 datadir=/usr/local/mysql/data basedir=/usr/local/mysql socket=/tmp/mysql.sock max_heap_table_size = 512M [mysqld_safe] log-error=/usr/local/mysql/log/mysqld.log 另外将被注释掉的innodb_buffer_pool_size = 128M放开(缺省值是8M,在批量插入数据过多时会产生错误,memsql设置128即可,MySQL需要设置为9G)
启动数据库:
service mysql start
运行命令:vi /etc/my.cnf,编辑文件
在文件的[mysqld]标签下添加一句:skip-grant-tables
service mysql stop
service mysql start
进入数据库:mysql -u root
使用数据库:use mysql;
运行:
update mysql.user set authentication_string=password('root') where user='root'; root_password替换成你想要的密码
exit退出
把my.conf的skip-grant-tables删除,重启数据库,
修改自己的账号:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
刷新权限
flush privileges;
退出:
quit;
设置访问权限:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
刷新访问权限表
flush privileges;
刷新服务:
service mysql restart
重启服务:
service mysql restart;
添加用户:
CREATE USER '用户名'@'localhost' IDENTIFIED BY '密码';
自动安装脚本:网上的好多都不能用。
#!/bin/bash #coding: utf-8 #filename: mysql_install.sh #version: 1.0 #desc: mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz 安装脚本 #attention: 需要 root 用户执行 echo '1、判断当前用户是否为root用户' if [ $(id -u) != "0" ];then echo "error: you must be root run this script" exit 1 fi echo '2、定义变量' WKDIR=$(pwd) INSTALL_HOME="/opt/module" MYSQL_HOME="$INSTALL_HOME/mysql5.7" MYSQL_DATA="$MYSQL_HOME/data" MYSQLD_HOME="/var/run/mysqld/mysqld.sock" [ -d $INSTALL_HOME ] || mkdir -p $INSTALL_HOME echo ' 3、检验安装目录' if [ -d $MYSQL_HOME ];then echo "erro: your system have installed mysql" exit 1 fi echo ' 4、卸载系统自带数据库' apt-get remove mariadb* -y apt-get remove mysql-common -y echo '5、开始安装' echo "start installing mysql" echo ' 解压' echo "start tar..." tar -xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.28-linux-glibc2.12-x86_64 $MYSQL_HOME echo "tar complete" [ -d $MYSQL_DATA ] || mkdir -p $MYSQL_DATA echo ' 创建用户' id mysql > /dev/null 2>&1 if [ $? -ne 0 ];then useradd -s /sbin/nologin -M mysql fi echo ' 创建配置文件及日志目录' mkdir -p $MYSQL_HOME/etc mkdir -p $MYSQL_HOME/logs mkdir -p $MYSQL_HOME/tmp mkdir -p /var/run/mysqld chown -R mysql:mysql /var/run/mysqld/ echo ' 更改 mysql 目录所属用户和组' chown -R mysql:mysql $MYSQL_HOME echo "initializing mysql" echo ' 初始化 mysql 数据库' $MYSQL_HOME/bin/mysqld --initialize --basedir=$MYSQL_HOME --datadir=$MYSQL_DATA --user=mysql echo ' 创建配置文件' cat >$MYSQL_HOME/etc/my.cnf<<EOF [mysqld] daemonize = on user = mysql port = 3306 skip-grant-tables basedir = /opt/module/mysql5.7 datadir = /opt/module/mysql5.7/data socket = /var/run/mysqld/mysqld.sock pid-file = /opt/module/mysql5.7/tmp/mysqld.pid log-error = /opt/module/mysql5.7/logs/mysqld.log character-set-server=utf8 collation-server=utf8_general_ci innodb_buffer_pool_size = 10G innodb_log_file_size = 512M max_heap_table_size=5120M max_allowed_packet=100M max_connections=1000 event_scheduler=ON lower_case_table_names=1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [client] socket = /var/run/mysqld/mysqld.sock default-character-set=utf8 EOF echo ' 注册服务' cat >/lib/systemd/system/mysqld.service<<EOF [Unit] Description=MySQL Server Documentation=man:mysqld(8) After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql Type=forking TimeoutSec=0 PermissionsStartOnly=true ExecStart=/opt/module/mysql5.7/bin/mysqld --defaults-file=/opt/module/mysql5.7/etc/my.cnf LimitNOFILE = 5000 Restart=on-failure PrivateTmp=false EOF echo ' 配置环境变量' cat > /etc/profile.d/mysql.sh<<EOF export PATH=$PATH:/opt/module/mysql5.7/bin EOF echo ' 使配置的环境变量生效' source /etc/profile.d/mysql.sh cp $MYSQL_HOME/bin/mysql /usr/bin/ echo ' 启动服务' systemctl start mysqld systemctl enable mysqld #按照hydra cd $WKDIR dpkg -i libmariadb3_1%3a10.3.22-1_amd64.deb mysql-common_5.8+1.0.5_all.deb libmariadb3_1%3a10.3.22-1_amd64.deb mariadb-common_1%3a10.3.22-1_all.deb hydra_9.0-1_amd64.deb #设置开机启动 systemctl enable /lib/systemd/system/mysqld.service echo "=======进入数据库=======" mysql --socket=$MYSQLD_HOME -uroot -e "update mysql.user set authentication_string=password('root') where user='root';" echo "======删除my.conf中的skip-grant-tablese删除=====" sed -i '5d' /opt/module/mysql5.7/etc/my.cnf; service mysqld restart echo "=====设置root的访问权限=====" mysql --socket=$MYSQLD_HOME -uroot -proot --connect-expired-password -e "alter user 'root'@'localhost' identified by '';" mysql --socket=$MYSQLD_HOME -uroot -pblessme -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;" echo "=====添加新的用户======" mysql --socket=$MYSQLD_HOME -uroot -pblessme -e "CREATE USER 'oms'@'localhost' IDENTIFIED BY '1234';" mysql --socket=$MYSQLD_HOME -uroot -pblessme -e "GRANT GRANT OPTION ON *.* TO 'oms'@'localhost';" mysql --socket=$MYSQLD_HOME -uroot -pblessme -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON *.* TO 'oms'@'localhost';" mysql --socket=$MYSQLD_HOME -uroot -pblessme -e "ALTER USER 'oms'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234';" service mysqld restart echo "开始创建 数据库"; mysql --socket=$MYSQLD_HOME -uoms -p1234 -e "create database dris_common default charset 'utf8' collate 'utf8_bin';" mysql --socket=$MYSQLD_HOME -uoms -p1234 -e "create database dris_state default charset 'utf8' collate 'utf8_bin';" mysql --socket=$MYSQLD_HOME -uoms -p1234 -e "create database dris_history default charset 'utf8' collate 'utf8_bin';" mysql --socket=$MYSQLD_HOME -uoms -p1234 -e "create database dris_asset default charset 'utf8' collate 'utf8_bin';" mysql --socket=$MYSQLD_HOME -uoms -p1234 -e "create database syslog default charset 'utf8' collate 'utf8_bin';" mysql --socket=$MYSQLD_HOME -uoms -p1234 <<EOF use dris_common; source sql/dris_common.sql; use dris_state; source sql/dris_state.sql; use dris_history; source sql/dris_history.sql; use dris_asset; source sql/dris_asset.sql; EOF echo "开始创建 数据库完成";