删除自带的Mysql或MariaDB
for i in `rpm -qa|grep -i -e mariadb`;do rpm -e $i --nodeps; done
安装openssl
yum install openssl
下载msyql安装包
将文件解压到/usr/local/目录下
tar -xvf mysql-8.0.21-el7-x86_64.tar.gz -C /usr/local
切换到/usr/local/目录,将mysql-8.0.21-el7-x86_64重命名为mysql
cd /usr/local
mv mysql-8.0.21-el7-x86_64 mysql
切换到/usr/local/mysql目录,创建data文件夹,mysql用户组以及用户,重新设置mysql文件夹的文件所有者和文件关联组,修改文件夹权限
cd /usr/local/mysql
mkdir -p data
groupadd mysql
useradd -r -g mysql mysql
cd /usr/local/
chown -R mysql:mysql /usr/local/mysql/
chmod -R 755 /usr/local/mysql
在/etc/目录下创建my.cnf文件,编辑内容如下
[mysqld]
server_id=1001
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/usr/local/mysql/data/mysql.sock
user=mysql
port=3306
character-set-server=utf8
symbolic-links=0
[mysqld safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
port=3306
socket=/usr/local/mysql/data/mysql.sock
初始化MySQL,保存生成的初始密码
cd /usr/local/mysql/bin
./mysqld --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --initialize
此时控制台会打印出数据库密码,保存一下
开启服务,设置开机自启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
/usr/lib/systemd/systemd-sysv-install enable mysqld
添加环境变量,在/etc/profile文件末尾添加如下内容
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
环境变量生效
source /etc/profile
开启MySQL服务,查看状态
systemctl start mysqld
systemctl status mysqld
数据库操作
修改密码
mysql -uroot -p
alter user 'root'@'localhost' identified by 'root';
设置允许远程连接
create user 'root'@'%' identified by 'root'; -- 创建用户
grant all privileges on *.* to 'root'@'%' ; -- 授权
flush privileges; -- 刷新权限