1.去官网下载安装包
下载链接:
https://dev.mysql.com/downloads/mysql/
如果你的系统是32位选择第一个,64位选择第二个
也可以用wget 下载
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-linux-glibc2.12-i686.tar.gz
官网安装说明链接:
https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html
解压文件
tar -zxvf mysql-8.0.11-linux-glibc2.12-i686.tar.gz
2 移动压缩包到usr/local目录下,并重命名文件(或使用软连接到/usr/local/mysql)
mv /root/mysql-8.0.11-linux-glibc2.12-i686 /usr/local/mysql
3.在MySQL根目录下新建一个文件夹data,用于存放数据
mkdir data
4.创建 mysql 用户组和 mysql 用户
groupadd mysql
useradd -g mysql mysql
5.改变 mysql 目录权限
chown -R mysql.mysql /usr/local/mysql/
或者
chown -R mysql .
chgrp -R mysql .
#注意最后有一点
6.初始化数据库
#安装依赖包
yum install perl perl-devel perl-Data-Dumper libaio-devel -y
#初始化
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 (mysqld 8.0.11) initializing of server in progress as process 5826
[Server] A temporary password is generated for root@localhost: **twi=Tlsi<0O!** #临时密码
/usr/local/mysql/bin/mysqld (mysqld 8.0.11) initializing of server has completed
记录好自己的临时密码:
twi=Tlsi<0O!
7.mysql配置,修改/etc/my.cnf文件
/etc/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
8.设置MySQL服务开机启动
#(生产不建议设置自启动)
chkconfig --add mysql
cp -a ./support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
#检查服务是否生效
chkconfig --list mysqld
9.配置全局环境变量
编辑 / etc/profile 文件
在 profile 文件底部添加如下两行配置,保存后退出
PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
export PATH
设置环境变量立即生效
source /etc/profile
10.启动MySQL服务
service mysql start
查看初始密码,如果在my.cnf中配置了logs目录,则去配置的目录中找
cat /usr/local/mysql/logs/error.log |grep root
11.登录MySQL
mysql -uroot -p密码
修改密码:
#低版本
SET PASSWORD FOR 'root'@localhost=PASSWORD('123456'); #对应的换成你自己的密码即可了。
#5.7以上版本
set password=password('123456');
#8.0.13及以上版本
alter user 'root'@'localhost' identified by '123456';
12设置可以远程登录
mysql>use mysql
mysql>update user set host='%' where user='root' limit 1;
#刷新权限
mysql>flush privileges;
然后检查3306端口是否开放
netstat -nupl|grep 3306
开放3306端口
firewall -cmd --permanent --add-prot=3306/tcp
根据需要重启防火墙
firewall -cmd --reload
可能遇到的问题:
Active: failed (Result: exit-code) since Sat 2019-10-12 00:21:10 CST; 8s ago
Docs: man:systemd-sysv-generator(8)
Process: 46087 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
Process: 46937 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=1/FAILURE)
Oct 12 00:21:07 bigdata systemd[1]: Starting LSB: start and stop MySQL...
Oct 12 00:21:07 bigdata mysqld[46937]: Starting MySQL.Logging to '/usr/local/mysql/data/bigdata.err'.
Oct 12 00:21:10 bigdata mysqld[46937]: .. ERROR! The server quit without updating PID file (/usr/local/mysql/data/bigdata.pid).
Oct 12 00:21:10 bigdata systemd[1]: mysqld.service: control process exited, code=exited status=1
Oct 12 00:21:10 bigdata systemd[1]: Failed to start LSB: start and stop MySQL.
Oct 12 00:21:10 bigdata systemd[1]: Unit mysqld.service entered failed state.
Oct 12 00:21:10 bigdata systemd[1]: mysqld.service failed.
解析:
查证文件夹【/usr/local/mysql】权限问题