mysql修改datadir目录的方法总结(基于rpm安装)

场景

若mysql 安装初步使用rpm默认将db安装到/var/lib/mysql/路径后,想更改db(datadir)路径,怎么处理呢? 网站参考了很多,实验没有成功,看到有提SELinux的,研究了一下果然如此。现整理总结了一下,仅供参考。

测试整理步骤说明

  • 简单安装步骤
  • 更改db目录的2中方式

注意:

修改datadir,要Linux要关闭SELinux(参看附录)。有的文章说要关闭iptables,测试是不需要的。

(1)通过db初始化命令更改db存放位置:
/usr/bin/mysql_install_db --user=mysql --basedir=/usr/ --datadir=/data/public3306/db/

(2)通过将原来db目录内内容拷贝到新地址,再更改my.cnf 文件的datadir (类似oracle 冷备,更改控制文件 datafile指针…)

  • 卸载环境

一、安装步骤

--> 准备安装包
[root@lottery ~]# ll  
-rw-rw-rw-  1 root root  18360416 5月  19 10:54 MySQL-client-5.6.19-1.el6.x86_64.rpm
-rw-rw-rw-  1 root root  54592892 5月  19 10:54 MySQL-server-5.6.19-1.el6.x86_64.rpm
-->rpm -vhi 安装包
[root@lottery ~]# rpm -hvi MySQL-client-5.6.19-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:MySQL-client      ########################################### [100%] 
[root@lottery ~]# rpm -hvi MySQL-server-5.6.19-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:MySQL-server     ########################################### [100%]
-->启动mysql服务
[root@lottery ~]#  /etc/init.d/mysql start
Starting MySQL... SUCCESS!
-->初始化密码
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -uroot
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit 
# /etc/init.d/mysqld restart
# mysql -uroot -p
Enter password:
mysql>   show variables like '%datadir%';
+---------------+-----------------------+
| Variable_name | Value                           |
+---------------+-----------------------+
| datadir             | /var/lib/mysql/           |
+---------------+-----------------------+

这里命令可以使用“/etc/init.d/mysql start|stop|restart”,也可以使用“service mysql start|stop|restart”;

二、更改db目录的2中方式

通过db初始化命令更改db存放位置

/usr/bin/mysql_install_db --user=mysql --basedir=/usr/ --datadir=/data/public3306/db/

  • vim /etc/my.cnf

    user=mysql #必须有

    datadir=/usun/mysql/data #必须有

    log-error=/hskj/mysql/data/error.log

    pid-file=/hskj/mysql/data/mysql.pid

  • 将之前数据库停掉–用mysql_install_db 脚本 初始化(新建库)到/data/public3306/db/ 目录的数据库…
  • 若原数据库有新建的database,需要将database对应的目录cp -rp 到更改后的路径/data/public3306/db/ ;
  • 否则原新的database不会在新路径体现…
[root@lottery public3306]# /usr/bin/mysql_install_db --user=mysql --basedir=/usr/ --datadir=/data/public3306/db/
Installing MySQL system tables...OK
Filling help tables...OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
 /usr//bin/mysqladmin -u root password 'new-password'
 /usr//bin/mysqladmin -u root -h lottery password 'new-password'
Alternatively you can run:
 /usr//bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
 cd /usr ; /usr//bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
 cd mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
 http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
WARNING: Found existing config file /usr//my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as /usr//my-new.cnf,
please compare it with your file and take the changes you need.
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
[root@lottery public3306]#  /etc/init.d/mysql start
Starting MySQL.... SUCCESS! 
[root@lottery public3306]# cd db
[root@lottery db]# ll
总用量 2097172
-rw-rw---- 1 mysql mysql         56 5月  19 15:47 auto.cnf 
drwx------ 2 mysql mysql       4096 5月  19 15:46 mysql
drwx------ 2 mysql mysql       4096 5月  19 15:46 performance_schema
drwx------ 2 mysql mysql       4096 5月  19 15:46 test
[root@lottery db]# 

通过将原来db目录内内容拷贝到新地址,再更改my.cnf 文件的datadir

  • 测试之前将原database,执行create database lottery ,利于迁移后是否正确.
--> 原路径
[root@lottery mysql]# pwd
/var/lib/mysql/ 
--> 目录开头的为database
[root@lottery mysql]# ll 
-rw-rw---- 1 mysql mysql         56 5月  19 15:47 auto.cnf 
drwx------ 2 mysql mysql       4096 5月  19 15:52 lottery
drwx------ 2 mysql mysql       4096 5月  19 15:46 mysql
drwx------ 2 mysql mysql       4096 5月  19 15:46 performance_schema
drwx------ 2 mysql mysql       4096 5月  19 15:46 test 
--> 查看lottery 数据库的表
[root@lottery lottery]# ll  lottery/*
-rw-rw---- 1 mysql mysql    54 5月  19 15:47 db.opt
-rw-rw---- 1 mysql mysql  8560 5月  19 15:52 t1.frm    ---t1表 一张
-rw-rw---- 1 mysql mysql 98304 5月  19 15:52 t1.ibd
--> 创建新datadir目录
[root@lottery lottery ]# mkdir -p /data/public3306/db 
     --  参数-p: 级联创建
--> 整个mysql datadir进行拷贝
[root@lottery lottery ]# cp -pr /var/lib/mysql/ /data/public3306/db 
     --  参数-p:连带属性拷贝,-r是目录拷贝
--> 检查cp 的文件内容
[root@lottery lottery  ]# cd /data/public3306/db 
[root@lottery db]# ll
-rw-rw---- 1 mysql mysql         56 5月  19 15:47 auto.cnf 
drwx------ 2 mysql mysql       4096 5月  19 15:52 lottery
drwx------ 2 mysql mysql       4096 5月  19 15:46 mysql
drwx------ 2 mysql mysql       4096 5月  19 15:46 performance_schema
drwx------ 2 mysql mysql       4096 5月  19 15:46 test 
--> 更改my.cnf 的datadir参数,将原来的/var/lib/mysql 改成 /data/public3306/db 
[root@lottery db]# vi /etc/my.cnf
--> 启动mysql服务
[root@lottery db]# /etc/init.d/mysql start
Starting MySQL..... SUCCESS!

--> 查看更改后datadir是否有效的2种方式
--> OS层查看
[root@lottery db]# ps -ef | grep mysql 
root     11346     1  0 16:07 pts/6    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/data/public3306/db --pid-file=/data/public3306/mysqld.pid
mysql    12007 11346 17 16:07 pts/6    00:00:03 /usr/sbin/mysqld --basedir=/usr --datadir=/data/public3306/db --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/data/public3306/err.log --pid-file=/data/public3306/mysqld.pid --socket=/data/public3306/mysql.sock --port=3306 
--> DB层查看  
mysql> show variables like '%datadir%';
+---------------+-----------------------+
| Variable_name | Value                 |
+---------------+-----------------------+
| datadir            | /data/public3306/db/ |
+---------------+-----------------------+ 

mysql>

--> 检查下原来database lottery 中的t1表 再新的环境中是否存在
mysql> use lottery;
Database changed 
mysql> show tables;
+-------------------+
| Tables_in_lottery |
+-------------------+
| t1                |
+-------------------+ 
mysql>

卸载环境

--> 查看系统中是否以rpm包安装的mysql
[root@lottery db]# rpm -qa | grep -i mysql
MySQL-client-5.6.19-1.el6.x86_64
MySQL-server-5.6.19-1.el6.x86_64
--> 通过rpm -e 卸载软件包
[root@lottery db]# rpm -e MySQL-client-5.6.19-1.el6.x86_64
[root@lottery db]# rpm -e MySQL-server-5.6.19-1.el6.x86_64
--> 查看是否有mysql服务,删除 mysql服务命令:chkconfig --del mysql
[root@lottery db]# chkconfig --list | grep -i mysql
--> mysql程序命令进行删除 
[root@lottery db]# whereis mysql 
mysql: /usr/lib64/mysql
[root@lottery db]# rm  -rf /usr/lib64/mysql

附录:SELinux

安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统。SELinux 的结构及配置非常复杂,而且有大量概念性的东西,要学精难度较大。很多 Linux 系统管理员嫌麻烦都把 SELinux 关闭了。

查看使用:

# getenforce

一般会返回三种状态:

  • Enforcing (记录警告且阻止可疑行为)
  • Permissive (代表仅记录安全警告但不阻止可疑行为)
  • Disabled (禁用状态)

也可使用以下命令查看状态:

/usr/sbin/sestatus -v

若SELinux status: enabled 则表示SELinux是开启状态。

临时关闭,重启机器后恢复原状态:

setenforce 0 #设置SELinux成为permissive模式

(setenforce 1 #设置SELinux 成为enforcing模式)

永久改变 SELinux 状态:

  • #vim /etc/sysconfig/selinux
    -将SELINUX=enforcing,改为SELINUX=disabled

  • #shutdown -r now -重新启动

  • #getenforce -查看返回Disabled。

主从同步

配置简单,参考这一篇文章即可:
https://blog.csdn.net/weixin_44187730/article/details/85706686#3_10

常用命令

  • 创建用户:allowsalve,密码为:147258,并授权。

GRANT REPLICATION SLAVE ON . TO ‘allowsalve’@‘192.168.44.%’ IDENTIFIED BY ‘147258’;

#刷新用户权限

flush privileges;

  • 查询用户和授权

select host,user,authentication_string from mysql.user;

  • 从库链接主服务器命令

stop slave

CHANGE MASTER TO
MASTER_HOST=‘192.168.44.125’,
MASTER_USER=‘allowsalve’,
MASTER_PASSWORD=‘147258’,
MASTER_LOG_FILE=‘mysql-bin.000011’,
MASTER_LOG_POS=247;

start slave

#主节点(master)
show master status;

#从节点(slave)
show slave status\G;[可以看主从同步的问题]

参考:
https://jingyan.baidu.com/article/020278115258441bcd9ce57e.html

https://yq.aliyun.com/articles/52238

http://blog.itpub.net/22390488/viewspace-2122857/

臭味相投的朋友们,我在这里:

猿in小站:http://www.yuanin.net

csdn博客:https://blog.csdn.net/jiabeis

简书:https://www.jianshu.com/u/4cb7d664ec4b

微信免费订阅号“猿in”
猿in

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值