1. 下载 https://dev.mysql.com/downloads/mysql/
选择
2.上传至服务器
3.配置hosts文件(通过别名访问)
服务IP地址 别名 别名 比如:192.168.1.52 alq alq
4.卸载主机自带的mysql
rpm -qa|grep mysql rpm -e --nodeps *mysql* rpm -qa|grep mariadb rpm -e --nodeps mariadb*
5.解压并安装
tar -xvf mysql-8.0.28-linux-glibc2.12-x86_64.tar rm -f mysql-8.0.28-linux-glibc2.12-x86_64.tar xz -d mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz tar -xvf mysql-8.0.28-linux-glibc2.12-x86_64.tar mv mysql-8.0.28-linux-glibc2.12-x86_64 /usr/local/ mv /usr/local/mysql-8.0.28-linux-glibc2.12-x86_64 /usr/local/mysql mkdir /var/lib/mysql mkdir /usr/local/mysql/log mkdir /usr/local/mysql/data ### 授权 查看用户组mysql是否存在 id mysql 如果存在先卸载掉 userdel mysql groupdel mysql 添加用户组mysql groupadd mysql 添加不可登陆的用户mysql useradd -r -g mysql -s /bin/false mysql 授权 mysql组mysql用户 chown -R mysql:mysql /var/lib/mysql chown -R mysql:mysql /usr/local/mysql
6.配置环境变量
vi ~/.bash_profile path追加 PATH=$PATH:/usr/local/mysql/bin
使环境变量生效
source ~/.bash_profile ###检测安装目录可以查到就说明生效了 which mysql
7.创建参数文件(因为mysql8 没有自动生成mysql.cnf)
vi /etc/mysql.cnf ### 添加 其中alq字样 是服务器别名 [mysql] default-character-set=utf8mb4 socket=/var/lib/mysql/mysql.sock [mysqld] port=3306 socket=/var/lib/mysql/mysql.sock basedir=/usr/local/mysql character-set-server=utf8mb4 default-storage-engine=INNODB innodb_buffer_pool_size = 200M max_allowed_packet=16M explicit_defaults_for_timestamp=1 log-output=FILE general_log =0 general_log_file=/usr/local/mysql/log/alq-general.err slow_query_log = ON slow_query_log_file=/usr/local/mysql/log/alq-query.err long_query_time=10 log-error=/usr/local/mysql/log/alq-error.err default-authentication-plugin=mysql_native_password
8.mysql数据库初始化
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
然后在 /usr/local/mysql/log 下 alq-error.err中查看是否初始化成功
有个重要信息是初始密码
这个一定要记录下来
9.配置启动和停止脚本
vi /usr/lib/systemd/system/mysqld.service ###添加 [Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf LimitNOFILE=65536 LimitNPROC=65536
添加可执行权限
chmod u+x /usr/lib/systemd/system/mysqld.service
10.systemctl启动和停止方法
systemctl daemon-reload # 装载 systemctl stop mysqld systemctl start mysqld systemctl enable mysqld systemctl status mysqld
11.修改初始密码
mysql -uroot -p Enter password: #步骤8的初始密码 mysql>use mysql; # 会报初始密码过期 mysql>ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root' #修改root用户的密码为root
12.允许root远程登录
mysql>use mysql; mysql>select user,host from user; mysql>grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option; #root帐户和密码(root) 远程访问测试 mysql -uroot -p -h192.168.1.52
关闭防火墙
firewall-cmd --state systemctl stop firewalld.service systemctl disable firewalld.service