MySQL5.7yum安装并更改数据目录

安装  
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
rpm -ivh mysql57-community-release-el7-9.noarch.rpm
yum install mysql-server
systemctl start mysqld
找初始密码:
# grep "temporary password" /var/log/mysqld.log 
2020-07-01T01:37:50.408894Z 1 [Note] A temporary password is generated for root@localhost: ( ALTER USER 'root'@'localhost' IDENTIFIED BY '_Shanghai#31';
Query OK, 0 rows affected (0.01 sec)
开启远程连接:
show databases;
use mysql;
show tables;
update user set Host='%' where User='root';
grant all privileges on *.* to root@"192.168.%" identified by "_Shanghai#31";
flush privileges;
配置默认编码为utf8
vi /etc/my.cnf
# 添加 [mysqld] 
character_set_server=utf8 
init_connect='SET NAMES utf8'
更改mysql安装目录:
先创建目录
[root@dev-base1  /data]
# cd /data
# mkdir MySQLData
# chmod 777 /data/MySQLData
停止数据库
# systemctl stop mysqld
复制数据目录 & 备份原有数据目录
# cp -R /var/lib/mysql /data/MySQLData
# mv /var/lib/mysql /var/lib/mysql_bak
# chown -R mysql:mysql /data/MySQLData
修改配置文件:
# vim /etc/my.cnf
添加配置:
datadir=/data/MySQLData/mysql
socket=/data/MySQLData/mysql.sock
log-error=/data/MySQLData/mysql/mysqld.log
启动数据库&设置开机自启
# systemctl restart mysqld
# systemctl enable mysqld
查看启动进程:
[root@dev-base1  /data/MySQLData]
# ps -ef |grep mysql
mysql     1183     1  1 10:06 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root      1213 62664  0 10:07 pts/0    00:00:00 grep --color=auto mysql
登录测试:
[root@dev-base1  /data/MySQLData]
# mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
解决mysql.sock问题:
[root@dev-base1  /data/MySQLData]
# mkdir -p /var/lib/mysql
[root@dev-base1  /data/MySQLData]
# ln -s /data/MySQLData/mysql.sock /var/lib/mysql/mysql.sock
[root@dev-base1  /data/MySQLData]
# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

 

你可能感兴趣的:(mysql)