查看系统版本:
cat /etc/centos-release CentOS Linux release 8.0.1905 (Core)
根据系统版本到mysql官网下载资源包
版本:Linux - Generic
Linux - Generic (glibc 2.12) (x86, 64-bit), Compressed TAR Archive
下载安装不说了
安装上传工具包
yum install -y lrzsz
上传文件:
rz
解压文件:
xz -d mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz tar -xvf mysql-8.0.19-linux-glibc2.12-x86_64.tar
移动文件:
mv mysql-8.0.19-linux-glibc2.12-x86_64 /usr/local/mysql
创建data目录:
mkdir data
创建 mysql 用户组和 mysql 用户
groupadd mysql
useradd -g mysql mysql
改变 mysql 目录权限
chown -R mysql.mysql /usr/local/mysql/ 或者 chown -R mysql . chgrp -R mysql .
因为不会自动生成my.cnf说以手动创建:
touch /etc/my.cnf
修改内容:
vim my.cnf
[mysqld] basedir = /usr/local/mysql datadir = /usr/local/mysql/data socket = /usr/local/mysql/mysql.sock character-set-server=utf8 port = 3306 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [client] socket = /usr/local/mysql/mysql.sock default-character-set=utf8
ESC 保存
:wq 退出
更改目录权限
chown -R mysql:mysql /usr/local/mysql
初始化数据库:
方式一:
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data //初始化数据库
方式二:
/usr/local/mysql/bin/mysqld --initialize --user=mysql
我用的方式二:
/usr/local/mysql/bin/mysqld --initialize --user=mysql A temporary password is generated for root@localhost: x2+JhQ1=?n/W
A temporary password 后的临时密码要记住:x2+JhQ1=?n/W
配置MySql服务:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld //好像报错,继续执行下面的
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --list mysqld
配置全局环境变量:
vi /etc/profile 在 profile 文件底部添加如下两行配置,保存后退出 export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib export PATH
保存退出后执行:
source /etc/profile
启动MySql服务:
service mysql start
或
/usr/local/mysql/support-files/mysql.server start
查看MySql启动状态:
service mysql status
密码登录:
mysql -uroot -p密码
修改密码:
update user set authentication_string='' where user='root';
FLUSH PRIVILEGES;
重新登陆:
alter user 'root'@'localhost' identified by '新密码';
FLUSH PRIVILEGES;
设置可以远程登录
mysql>use mysql mysql>update user set host='%' where user='root' limit 1; mysql>flush privileges;
防火墙安装:
yum install firewalld
开通3306端口:
firewall -cmd --permanent --add-prot=3306/tcp
在/etc/my.cnf [mysqld] 下加 skip-grant-tables 免密码登录
service mysqld restart