查看有没有安装过mysql ,如果没有安装继续往下。如果已有mysql服务,可选择卸载或者退出此次安装
[root@localhost ~]## yum list installed mysql*
[root@localhost ~]# rpm -qa | grep mysql*
安装依赖
[root@localhost ~]# yum install -y cmake make gcc gcc-c++ libaio ncurses ncurses-devel install libncurses*
下载 MySQL
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz
解压的 MySQL 包
[root@localhost local]# tar xvJf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
修改安装目录
[root@localhost local]# mv /usr/local/mysql-8.0.18-linux-glibc2.12-x86_64 /usr/local/mysql
创建 MySQL 组
[root@localhost local]# groupadd mysql
创建 MySQL 用户
[root@localhost local]# useradd -r -g mysql mysql
创建 data 目录
[root@localhost local]# mkdir /usr/local/mysql/data
进入 MySQL 目录
[root@localhost local]# cd /usr/local/mysql
赋予安装目录权限
[root@localhost mysql]# chown -R mysql:mysql ./
安装数据库
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
输出信息:"I4CGGpo62i?j" 是安装完成生成的默认密码
2023-11-02T01:57:42.133841Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2023-11-02T01:57:42.134023Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.18) initializing of server in progress as process 1888
2023-11-02T01:57:46.913455Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: I4CGGpo62i?j
修改 my.cnf 配置文件
[root@localhost local]vim /etc/my.cnf
将下段代码写入 my.cnf 配置文件
[mysqld]
datadir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
添加开机启动
[root@localhost mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
修改 MySQL , 在46行修改 mysql 路径
[root@localhost ~]# vim /etc/init.d/mysqld
baserdir=/usr/local/mysql
datadir=/usr/local/mysql/data
配置环境变量 /etc/profile
末尾添加:export PATH=$PATH:/usr/local/mysql/bin,如果之前配置过 PATH 变量则在后面追加 :/usr/local/mysql/bin
使 profile 生效
[root@localhost ~]# source /etc/profile
加入开机启动
[root@localhost ~]# chkconfig --add mysqld
启动 MySQL
[root@localhost ~]# service mysqld start
如出现如下错误,可使用此命令解决:mkdir /etc/my.cnf.d/。没有则跳过
my_print_defaults: Can't read dir of '/etc/my.cnf.d' (OS errno 2 - No such file or directory)
my_print_defaults: [ERROR] Fatal error in defaults handling. Program aborted!
my_print_defaults: Can't read dir of '/etc/my.cnf.d' (OS errno 2 - No such file or directory)
my_print_defaults: [ERROR] Fatal error in defaults handling. Program aborted!
ERROR! MySQL server PID file could not be found!
my_print_defaults: Can't read dir of '/etc/my.cnf.d' (OS errno 2 - No such file or directory)
my_print_defaults: [ERROR] Fatal error in defaults handling. Program aborted!
Starting MySQL.my_print_defaults: Can't read dir of '/etc/my.cnf.d' (OS errno 2 - No such file or directory)
my_print_defaults: [ERROR] Fatal error in defaults handling. Program aborted!
my_print_defaults: Can't read dir of '/etc/my.cnf.d' (OS errno 2 - No such file or directory)
my_print_defaults: [ERROR] Fatal error in defaults handling. Program aborted!
Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).
使用生成的默认密码登录 MySQL
[root@localhost ~] mysql -u root -p'I4CGGpo62i?j'
修改 MySQL 密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> exit;
开放端口 3306
[root@localhost ~] firewall-cmd --zone=public --add-port=3306/tcp --permanent
重启防火墙
[root@localhost ~] systemctl restart firewalld
开启远程连接
[root@localhost ~] mysql -u root -p123456
选择 mysql
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
修改 host
mysql> update 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.01 sec)