预计数据文件存储目录为:/opt/mysql/data
1、文件下载:
安装文件下载链接:https://downloads.mysql.com/archives/community/
2、检查当前系统是否安装过MySQL
[root@cnic51 mysql]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@cnic51 mysql]# sudo rpm -e --nodeps mariadb-libs
警告:/etc/my.cnf 已另存为 /etc/my.cnf.rpmsave
[root@cnic51 mysql]# rpm -qa|grep mariadb
[root@cnic51 mysql]#
3、将MySQL安装包拷贝到linux服务器的/opt/temp/目录下
[root@hadoop54 temp]# ll
总用量 570706
-rw-r--r-- 1 root root 570705920 6月 20 13:45 mysql-5.7.44-1.el7.x86_64.rpm-bundle.tar [root@hadoop54 temp]#
4、解压MySQL安装包
[root@cnic51 temp]# tar -xvf mysql-5.7.44-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-5.7.44-1.el7.x86_64.rpm
mysql-community-common-5.7.44-1.el7.x86_64.rpm
mysql-community-devel-5.7.44-1.el7.x86_64.rpm
mysql-community-embedded-5.7.44-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.44-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.44-1.el7.x86_64.rpm
mysql-community-libs-5.7.44-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.44-1.el7.x86_64.rpm
mysql-community-server-5.7.44-1.el7.x86_64.rpm
mysql-community-test-5.7.44-1.el7.x86_64.rpm
[root@cnic51 temp]#
5、在安装目录下执行rpm安装,按顺序安装
[root@cnic51 temp]# sudo rpm -ivh mysql-community-common-5.7.44-1.el7.x86_64.rpm
警告:mysql-community-common-5.7.44-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-common-5.7.44-1.e################################# [100%]
[root@cnic51 temp]# sudo rpm -ivh mysql-community-libs-5.7.44-1.el7.x86_64.rpm
警告:mysql-community-libs-5.7.44-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-5.7.44-1.el7################################# [100%]
[root@cnic51 temp]# sudo rpm -ivh mysql-community-libs-compat-5.7.44-1.el7.x86_64.rpm
警告:mysql-community-libs-compat-5.7.44-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-compat-5.7.4################################# [100%]
[root@cnic51 temp]# sudo rpm -ivh mysql-community-client-5.7.44-1.el7.x86_64.rpm
警告:mysql-community-client-5.7.44-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-5.7.44-1.e################################# [100%]
[root@cnic51 temp]# sudo rpm -ivh mysql-community-server-5.7.44-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.44-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-5.7.44-1.e################################# [100%]
[root@cnic51 temp]#
6、修改/etc/my.cnf文件中datadir指向的目录下的所有内容,如果有内容的情况下:
查看datadir的值,将其修改为如下:
旧的内容为:
# For advice on how to change settings please see [mysqld] # Disabling symbolic-links is recommended to prevent assorted security risks log-error=/var/log/mysqld.log |
新的内容为:
# For advice on how to change settings please see [mysqld] # Disabling symbolic-links is recommended to prevent assorted security risks log-error=/opt/mysql/log/mysqld.log |
删除/var/lib/mysql目录下的所有内容(如果指定的目录不是默认的可以不用管):
[root@hadoop54 mysql]# cd /var/lib/mysql
[root@hadoop54 mysql]# ls
[root@hadoop54 mysql]# sudo rm -rf *
[root@hadoop54 mysql]#
7、初始化数据库
[root@hadoop51 mysql]# sudo chown -R mysql:mysql /opt /mysql
[root@hadoop51 mysql]# sudo mysqld --initialize --user=mysql
[root@hadoop51 mysql]#
8、查看临时生成的root帐号密码
2024-06-20T06:17:53.912194Z 0 [ERROR] Can't find error-message file '/opt/mysql/share/mysql/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2024-06-20T06:17:54.145587Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-06-20T06:17:54.220326Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-06-20T06:17:54.287423Z 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: d45d6ffd-2ecc-11ef-a74a-fa081328b000.
2024-06-20T06:17:54.295842Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-06-20T06:17:55.180876Z 0 [Warning]
2024-06-20T06:17:55.180891Z 0 [Warning]
2024-06-20T06:17:55.184006Z 0 [Warning] CA certificate ca.pem is self signed.
2024-06-20T06:17:55.315891Z 1 [Note] A temporary password is generated for root@localhost: 8<A.-MrijeiF
9、启动MySQL数据库
[root@hadoop51 mysql]# sudo systemctl start mysqld
10、登录MySQL数据库
[root@cnic51 mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> set password = password("123456");
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'dev'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'dev'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
必须先修改root用户的密码,否则执行其他的操作会报错
mysql> set password = password("123456");
Query OK, 0 rows affected, 1 warning (0.00 sec)
11、根据需要是否修改mysql库下的user表中的root用户允许任意ip连接
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)
新建账号及授权语句:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'dev'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'dev'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)