总结下离线版 MySQL 5.7.17 的安装步骤:
1.从官网下载mysql 离线版版安装包 mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz;
2.上传安装包mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz 到 centos 7 的 /usr/tool/ 目录 ;
3.进入/usr/tool/ ---> cd /usr/tool;
4.解压安装包:tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz;
5.安装依赖: yum -y install perl perl-devel autoconf libaio;
6.进入/usr/local/mysql/ --->cd /usr/local/mysql/;
7.创建data目录:mkdir /usr/local/mysql/data;
8.添加mysql用户组和用户:
groupadd mysql;
useradd -r -g mysql -s /bin/false mysql;
9. 修改目录拥有者为mysql:chown -R mysql:mysql ./
10.初始化数据(下面是一条语句):
./bin/mysqld --initialize --user=mysql
--basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data
--pid-file=/usr/local/mysql/data/mysql.pid
--tmpdir=/tmp
11.复制生成的密码(登录mysql时用):
12.复制配置文件到/etc/my.cnf:cp -a ./support-files/my-default.cnf /etc/my.cnf (选择y)
13.mysql的服务脚本放到系统服务中:
cp -a ./support-files/mysql.server /etc/init.d/mysqld
14.赋予可执行权限:chmod +x /etc/init.d/mysqld
15.添加服务:chkconfig --add mysqld (可用chkconfig --list命令查看,3,4,5都是ON证明已启动)
16.修改my.cnf文件:vi /etc/my.cnf, 编辑添加如下内容:
log-bin = /home/data/logs/mysql/binlog/mysql-bin #开启二进制日志功能,binlog数据位置
log-bin-index = /home/data/logs/mysql/binlog/mysql-bin.index
log_bin_trust_function_creators = 1
binlog_cache_size = 1M
binlog_format = ROW
expire_logs_days = 30 #超过30天的binlog删除
max_connections = 500 #最大连接数
port = 3306
socket = /tmp/mysql.sock
pid-file = /usr/local/mysql/mysql.pid
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
log_error =/home/data/logs/mysql/mysql-error.log
user = mysql
character-set-server = utf8
back_log = 1024
server-id = 1
long_query_time = 1 #开启慢查询
slow_query_log = 2 #慢查询时间(s)
slow_query_log_file = /home/data/logs/mysql/mysql-slow.log
17.创建相应的日志目录和文件:
mkdir /home/data/logs (存在则不创建)
mkdir /home/data/logs/mysql
mkdir /home/data/logs/mysql/binlog
touch /home/data/logs/mysql/mysql-slow.log
mkdir /home/data/logs/mysql/binlog/mysql-bin
touch /home/data/logs/mysql/binlog/mysql-bin.index
chown -R mysql:mysql /home/data/logs
18.把mysql客户端放到默认路径:
ln -s /usr/local/mysql/bin/mysqld /usr/local/bin/mysqld
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
19.启动服务:service mysqld start (ps -ef |grep mysqld查看是否启动)
20.登录mysql:mysql -uroot -p,输入前面复制的密码
21.使用mysql库: use mysql:
修改root密码:set password for root@localhost = password('要设置的密码admin123');
创建mystest用户:CREATE USER 'mystest'@'localhost' IDENTIFIED BY '要设置的密码admin123'';
查看是否创建成功:SELECT host, user FROM user WHERE user='soonfor';
赋权限(除了DROP权限):
GRANT ALL ON *.* TO 'mystest'@'localhost'; //全部
REVOKE DROP ON *.* FROM 'mystest'@'localhost'; //删除DROP权限
SHOW GRANTS FOR 'mystest'@'localhost'; //查看权限
22.设置远程访问:
update user set host='%' where user='root' limit 1;
update user set host='%' where user='mytest' limit 1;
刷新权限:flush privileges;
23.安装完成,可以使用创建的用户登录。
由于开始安装的时候没有注意到 Linux 磁盘大小的问题,没有过久硬盘就爆了,需要挂载新的数据盘,将 MySQL
数据迁移到新的挂载盘上。
查看 MySQL 的安装路径
执行查询 SQL
show variables like '%dir%';
datadir 就是数据路径
转移操作
新建 MySQL 数据目录: mkdir -p /home/mysqldata
# 改变数据库的归属为 mysql
chown -R mysql.mysql /data/mysql
停止 SQL 服务: service mysqld stop
把原来 MySQL 安装的数据路径复制到新的文件目录下: cp -rf /usr/local/mysql/data /data/mysqlData/
修改 MySQL 的 my.cnf 文件: cp /etc/my.cnf /etc/my.cnf.bak
vi /etc/my.cnf
# 文件内容如下, 需要修改 datadir , socket 对应的目录
log-bin = /home/data/logs/mysql/binlog/mysql-bin #开启二进制日志功能,binlog数据位置
log-bin-index = /home/data/logs/mysql/binlog/mysql-bin.index
log_bin_trust_function_creators = 1
binlog_cache_size = 1M
binlog_format = ROW
expire_logs_days = 30 #超过30天的binlog删除
max_connections = 500 #最大连接数
port = 3306
socket = /tmp/mysql.sock
pid-file = /usr/local/mysql/mysql.pid
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
log_error =/home/data/logs/mysql/mysql-error.log
user = mysql
character-set-server = utf8
back_log = 1024
server-id = 1
long_query_time = 1 #开启慢查询
slow_query_log = 2 #慢查询时间(s)
slow_query_log_file = /home/data/logs/mysql/mysql-slow.log
修改 MySQL 启动脚本: vi /etc/init.d/mysqld
重启 MySQL 服务: service mysqld restart
最后执行下 show variables like '%dir%' :