Linux安装MySQL数据库

Linux安装MySQL数据库

1. 检查是否已经安装MySQL

rpm -qa | grep mysql

如果已经安装必须进行卸载之后在进行安装,可以使用下面命令将其删除(xxx 为文件全名)

rpm -e xxx

2. 安装MySQL

2.1 下载安装包

下载mysql   

MySQL :: Download MySQL Community Server (Archived Versions)

选择对应的包 如下5.7包。

2.2 上传解压

tar -zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz

2.3 创建用户组和用户

先检查是否有mysql用户组和mysql用户,没有就添加有就忽略:

[mysql@mysql soft]$ groups mysql 
mysql : mysql

添加用户组和用户:

groupadd mysql && useradd -r -g mysql mysql

2.4 创建数据目录

4.创建数据目录并赋予权限

mkdir -p /data/mysql
chown mysql:mysql -R /data/mysql

2.5 修改配置文件

修改/etc/my.cnf文件,如果没有就新建这个文件。

[root@mysql soft]# cat /etc/my.cnf
[mysqld]
# Disabling symbolic-links is recommended to prevent assorted security risks
# 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
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

2.6 初始化

解压后的mysql-5.7.35-linux-glibc2.12-x86_64文件移动到/usr/local/mysql (文件夹名称修改为mysql不带版本号信息)

cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize

2.7 查看初始密码

cat /data/mysql/mysql.err

[root@mysql bin]# cat /data/mysql/mysql.err
2024-09-05T09:47:39.927022Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-09-05T09:47:39.971064Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-09-05T09:47:40.303241Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e405c6ce-6b6b-11ef-ae6c-000c2942780c.
2024-09-05T09:47:40.315850Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-09-05T09:47:40.943488Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-09-05T09:47:40.943496Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-09-05T09:47:40.943940Z 0 [Warning] CA certificate ca.pem is self signed.
2024-09-05T09:47:41.135313Z 1 [Note] A temporary password is generated for root@localhost: ISp+>%t?i161

2.8 启动MySQL

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
service mysql start

3. 修改密码

3.1 开启免密码登陆

修改my.cnf文件 默认在/etc/my.cnf

# 在【mysqld】模块下面添加:skip-grant-tables 保存退出。
vim /etc/my.cnf

3.2 重启服务,

重启mysql服务,使配置生效 。

service mysql restart

3.3 登陆

# 不输入密码直接敲回车键
/usr/local/mysql/bin/mysql -u root -p

3.4 刷新规则允许外部访问

# 选择访问mysql库
use mysql
# 使root能再任何host访问
update user set host = '%' where user = 'root';
# 刷新 
FLUSH PRIVILEGES;

3.5 修改密码

ALTER USER "root"@"%" IDENTIFIED  BY "123456";
# 刷新 
FLUSH PRIVILEGES;

3.6 关闭免密登录

退出数据库,修改my.cnf文件关闭免密登录,再次重启mysql服务。

# 退出
mysql> quit
# 把/etc/my.cnf免密删掉。
vim /etc/my.cnf
# 重启服务    
service mysql restart

3.7 再次登陆

# 输入刚修改的密码123456敲回车键
/usr/local/mysql/bin/mysql -u root -p

4. 创建新用户并给授权指定的数据库权限

4.1 创建mysql新用户

CREATE USER 'test'@'%' IDENTIFIED BY '123456';

备注上面@后的命令解释:

'%' - 所有情况都能访问
‘localhost’ - 本机才能访问
’111.222.33.44‘ - 指定 ip 才能访问

4.2 创建数据库

mysql> create database mydb;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

4.3 给用户授权

grant all on 数据库名.数据库表 to 用户名@'%' identified by "密码";
grant all on mydb.* to 'test'@'%' identified by "123456";

备注:

all 可以替换为 select,delete,update,create,drop
数据库名 所有的 用*
数据库表 所有的 用*
posted @ 2024-09-11 14:57  零の守墓人  阅读(15)  评论(0编辑  收藏  举报