系统如果自带 MariaDB,需先卸载。
查询已安装的 MariaDB 安装包:
[root@localhost ~]# rpm -qa|grep mariadb
执行卸载命令,如:
[root@localhost ~]# yum -y remove mariadb-5.5.56-2.el7.x86_64
这里下载的是 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz 安装包,并将其直接放在了 root 目录下
将 MySQL 安装包解压到 /usr/local/ 目录:
[root@localhost ~]# tar -zxvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
进入 /usr/local/ 目录:
[root@localhost ~]# cd /usr/local
重命名 MySQL 安装包为 mysql
[root@localhost local]# mv mysql-5.7.30-linux-glibc2.12-x86_64 mysql
[root@localhost local]# groupadd mysql
[root@localhost local]# useradd -g mysql mysql
同时新建 /usr/local/mysql/data 目录,后续备⽤
[root@localhost local]# cd mysql
[root@localhost mysql]# mkdir data
修改MySQL目录的归属⽤户
[root@localhost mysql]# chown -R mysql:mysql ./
在 /etc 目录下新建 my.cnf 文件
[root@localhost ~]# vim /etc/my.cnf
添加如下配置:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
#设置3306端⼝
port = 3306
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装⽬录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放⽬录
datadir=/usr/local/mysql/data
# 允许最⼤连接数
max_connections=200
# 服务端使⽤的字符集默认为8⽐特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使⽤的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
同时使用如下命令创建 /var/lib/mysql 目录,并修改权限:
[root@localhost ~]# mkdir /var/lib/mysql
[root@localhost ~]# chmod 777 /var/lib/mysql
执行如下命令开始安装:
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
注意:最后一行打印出来的 root 密码,需复制出来首次登录使用
执行如下命令复制:
[root@localhost mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
修改 /etc/init.d/mysqld :
[root@localhost mysql]# vim /etc/init.d/mysqld
修改其中的 basedir 和 datadir 为实际对应目录:
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
首先增加 mysqld 服务控制脚本执行权限:
[root@localhost ~]# chmod +x /etc/init.d/mysqld
将 mysqld 服务加入到系统服务:
[root@localhost ~]# chkconfig --add mysqld
检查 mysqld 服务是否已经生效:
[root@localhost ~]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
mysqld 服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,可以直接使用 service 命令控制 mysql 的启停。
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/***.err'.
SUCCESS!
编辑 ~/.bash_profile 文件:
[root@localhost ~]# vim ~/.bash_profile
在末尾处追加如下信息:
export PATH=$PATH:/usr/local/mysql/bin
执行如下命令使环境变量生效:
[root@localhost ~]# source ~/.bash_profile
以 root 账户登录 mysql ,使用上文安装完成提示的密码进行登录:
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
密码换成你的密码,这里密码是“123456”
mysql> alter user user() identified by "123456";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> use mysql;
mysql> update user set user.Host='%' where user.User='root';
mysql> flush privileges;