【超详细】Centos7安装mysql8(mysql-8.0.20-el7-x86_64.tar.gz)
程序员文章站
2022-05-27 19:17:55
...
安装前
1.安装mysql前,需将系统自带的mariadb卸载。
[[email protected] ~]# rpm ‐qa|grep mariadb
mariadb‐libs‐5.5.52‐1.el7.x86_643
[[email protected] ~]# rpm ‐e ‐‐nodeps mariadb‐libs‐5.5.52‐1.el7.x86_644
[[email protected] ~]# rpm ‐qa|grep mariadb
2. 下载mysql8.0.20
[[email protected] src]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.20-el7-x86_64.tar.gz
开始安装
解压mysql包
[[email protected] src]# tar -zxvf mysql-8.0.20-el7-x86_64.tar.gz
创建安装目录
[[email protected] src]# mkdir -p /usr/local/mysql
[[email protected] src]# mv mysql-8.0.20-el7-x86_64/* /usr/local/mysql/
创建mysql用户和组
[[email protected] src]# groupadd mysql
[[email protected] src]# useradd -r -g mysql -s /sbin/nologin mysql
创建数据目录并授权
[[email protected] src]# cd /usr/local/mysql/
[[email protected] mysql]# mkdir data logs
[[email protected] mysql]# chown -R mysql:mysql /usr/local/mysql/
在etc下新建配置文件my.cnf,加入下面配置
[mysqld]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/logs/mysql.log
socket=/tmp/mysql.sock
event_scheduler=ON
slow_query_log = ON
slow_query_log_file = /usr/local/mysql/logs/mysql-slow.log
long_query_time = 1
innodb_buffer_pool_size = 6G
default-authentication-plugin=mysql_native_password
server-id=10
log-bin=/usr/local/mysql/logs/mysql-bin
max_connect_errors=1000
log-error=/usr/local/mysql/logs/mysql.log
pid-file=/usr/local/mysql/data/mysql.pid
安装数据库,生成初始密码并查看
[[email protected] mysql]# cat logs/mysql | grep password
[email protected] mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
配置mysql服务
[[email protected] mysql]# vim /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
[[email protected] mysql]# systemctl daemon-reload #重新加载
软链接
[[email protected] mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin/
[[email protected] mysql]# ln -s /usr/local/mysql/bin/mysqldump /usr/bin/
启动服务并设置开机自启
[[email protected] mysql]# systemctl stop mysqld
[[email protected] mysql]# systemctl start mysqld
[[email protected] mysql]# systemctl enable mysqld
查看运行状态
[[email protected] mysql]# ps -ef | grep mysqld
[[email protected] mysql]# systemctl status mysqld
修改root密码,并设置远程登录
[[email protected] mysql]# mysql -uroot -p8RA__Li6TPt?
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)
mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)