mariadb多实例实现

时间:2021-01-07 00:05:21

环境:centos7,yum 安装mariadb5.5

  mkdir /mysqldb/{3306,3307.3308}/{etc,socket,pid,log,data} -pv

  chown -R mysql.mysql /mysqldb/

  mysql_install_db --datadir=/mysqldb/3306/data --user=mysql --basedir=/usr   datadir数据库路径 basedir程序路径

  mysql_install_db --datadir=/mysqldb/3307/data --user=mysql --basedir=/usr

  mysql_install_db --datadir=/mysqldb/3308/data --user=mysql --basedir=/usr

  mysql -e 'show variables like "basedir";'       查看basedir路径

  vim /mysqldb/3306/etc/my.cnf 

  cp /etc/my.cnf  /mysqldb/3306/etc/

[mysqld]
port=
datadir=/mysqldb//data
socket=/mysqldb//socket/mysql.sock symbolic-links= [mysqld_safe]
log-error=/mysqldb//log/mariadb.log
pid-file=/mysqldb//pid/mariadb.pid

  

  cp /mysqldb/3306/etc/my.cnf  /mysqldb/{3307,3308}/etc/

  sed -i 's/3306/3307/'  /mysqldb/3307/etc/my.cnf    将3306改为3307

  sed -i 's/3306/3307/'   /mysqldb/3308/etc/my.cnf              将3306改为3308

  cd /mysqldb/3306

vim mysqld      制作脚本

  

#!/bin/bash

port=
mysql_user="root"
mysql_pwd="" root的数据库密码,关闭数据库时调用,不用再输入,输错,关闭时会报错
cmd_path="/usr/bin"
mysql_basedir="/mysqldb"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock" function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf &> /dev/null &
else
printf "MySQL is running...\n"
exit
fi
} function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stopping MySQL..\n"
# ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
#此行注释的是关闭脚本时需要使用密码
${cmd_path}/mysqladmin -u ${mysql_user} -S ${mysql_sock} shutdown
fi
} function_restart_mysql()
{
printf "Restarting MySQL...\n"
funtions_stop_mysql
sleep
funtion_start_mysql
} case $ in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restat)
function_restart_mysql
;;
*)
printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac

  chmod 700 mysqld        设置高权限,脚本内会保存一个关闭服务的密码

  启动时带上路径

  /mysqldb/3306/mysqld  start|stop|restart

  /mysqldb/3307/mysqld  start|stop|restart

  /mysqldb/3308/mysqld  start|stop|restart

  进入数据库需要带上路径

  mysql -S /mysqldb/3306/socket/mysql.sock

  mysql -S /mysqldb/3307/socket/mysql.sock

  mysql -S /mysqldb/3308/socket/mysql.sock

  进入数据库为root增加密码

  update mysql.user set password=password("centos") where user='root'; 为数据库mysql的用户增加密码,并加密密码,用户名为root

  flush privileges   刷新权限,启用密码

  mysql -S /mysqldb/3306/socket/mysql.sock -uroot -pcentos 在数据库外设定root密码,在数据库里面设定密码后需要刷新权限,否则还是用旧密码进入,刷新后按数据库内新密码进入

  增加密码后数据库连接,需要加上账号和密码

  mysql -S /mysqldb/3306/socket/mysql.sock -uroot -pcentos

  

  select user,password,host from mysql.user;   查看mysql数据库的user,password,host,信息,以user为索引

  drop user ''@'localhost';

  drop user ''@'localhost.localdomain';

  drop user'root'@'localhost.localdomain';             删除空账号,删除几个ipv6地址账号

  drop user'root'@'::1';

  show databases;             查看本Mysql里的数据库文件

  drop database test;           删除无用的test数据库

  

破解登陆mysql密码:

  vim /mysqldb/3306/etc/my.cnf

[mysqld]
port=
datadir=/mysqldb//data
socket=/mysqldb//socket/mysql.sock
skip_grant_tables 添加此行
symbolic-links= [mysqld_safe]
log-error=/mysqldb//log/mariadb.log
pid-file=/mysqldb//pid/mariadb.pid

  重启服务,即可空口令进入