linux下MySQL安装_一入终端深似水的博客-程序员宅基地

技术标签: mysql  

MySql安装

(1) 检查当前系统是否安装过MySql

[[email protected] ~]$ rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[[email protected] ~]$ sudo yum -y remove mariadb-libs

(2) 将mysql安装包拷贝到Linux的软件包目录下##

[[email protected] software]$ ll
总用量 531112
-rw-rw-r-- 1 angusmyz angusmyz 543856640 615 09:14 mysql-5.7.34-1.el7.x86_64.rpm-bundle.tar

(3) 解压MySql安装包

[[email protected] software]$ tar xvf mysql-5.7.34-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-5.7.34-1.el7.x86_64.rpm
mysql-community-common-5.7.34-1.el7.x86_64.rpm
mysql-community-devel-5.7.34-1.el7.x86_64.rpm
mysql-community-embedded-5.7.34-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.34-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.34-1.el7.x86_64.rpm
mysql-community-libs-5.7.34-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.34-1.el7.x86_64.rpm
mysql-community-server-5.7.34-1.el7.x86_64.rpm
mysql-community-test-5.7.34-1.el7.x86_64.rpm
[[email protected] software]$ ll
总用量 1062228
-rw-rw-r-- 1 angusmyz angusmyz 543856640 615 09:14 mysql-5.7.34-1.el7.x86_64.rpm-bundle.tar
-rw-r--r-- 1 angusmyz angusmyz  26597364 327 00:49 mysql-community-client-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 angusmyz angusmyz    317444 327 00:49 mysql-community-common-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 angusmyz angusmyz   4119104 327 00:49 mysql-community-devel-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 angusmyz angusmyz  47479092 327 00:49 mysql-community-embedded-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 angusmyz angusmyz  23264364 327 00:49 mysql-community-embedded-compat-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 angusmyz angusmyz 131605536 327 00:49 mysql-community-embedded-devel-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 angusmyz angusmyz   2472844 327 00:49 mysql-community-libs-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 angusmyz angusmyz   1262704 327 00:49 mysql-community-libs-compat-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 angusmyz angusmyz 181833876 327 00:50 mysql-community-server-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 angusmyz angusmyz 124888148 327 00:50 mysql-community-test-5.7.34-1.el7.x86_64.rpm

(4) 在安装目录下执行rpm安装

[[email protected] software]$ sudo rpm -ivh mysql-community-common-5.7.34-1.el7.x86_64.rpm
[[email protected] software]$ sudo rpm -ivh mysql-community-libs-5.7.34-1.el7.x86_64.rpm
[[email protected] software]$ sudo rpm -ivh mysql-community-libs-compat-5.7.34-1.el7.x86_64.rpm
[[email protected] software]$ sudo rpm -ivh mysql-community-client-5.7.34-1.el7.x86_64.rpm
[[email protected] software]$ sudo rpm -ivh mysql-community-server-5.7.34-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.34-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
        net-tools 被 mysql-community-server-5.7.34-1.el7.x86_64 需要
[[email protected] software]$ sudo yum -y install net-tools
[[email protected] software]$ sudo rpm -ivh mysql-community-server-5.7.34-1.el7.x86_64.rpm

注意:按照顺序依次执行

如果Linux是最小化安装的,在安装mysql-community-server-5.7.34-1.el7.x86_64.rpm时可能会出现如下错误

[[email protected] software]$ sudo rpm -ivh mysql-community-server-5.7.34-1.el7.x86_64.rpm

警告:mysql-community-server-5.7.34-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY

错误:依赖检测失败:

​ libaio.so.1()(64bit) 被 mysql-community-server-5.7.34-1.el7.x86_64 需要

​ libaio.so.1(LIBAIO_0.1)(64bit) 被 mysql-community-server-5.7.34-1.el7.x86_64 需要

​ libaio.so.1(LIBAIO_0.4)(64bit) 被 mysql-community-server-5.7.34-1.el7.x86_64 需要

通过yum安装缺少的依赖,然后重新安装mysql-community-server-5.7.34-1.el7.x86_64 即可

[[email protected] software] yum install -y libaio

(5) 删除/etc/my.cnf文件夹中datadir指向的目录下的所有内容, 如果有内容情况如下:

查看datadir的值:

[mysqld]

datadir=/var/lib/mysql

删除/var/lib/mysql目录下的所有内容:

[[email protected] mysql]# cd /var/lib/mysql

[[email protected] mysql]# sudo rm -rf ./*   

(6) 初始化数据库

[[email protected] ~]$ sudo mysqld --initialize --user=mysql

(7) 查看生成的root用户密码

[[email protected] ~]$ sudo cat /var/log/mysqld.log
2021-06-15T01:50:51.591750Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-15T01:50:53.660213Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-06-15T01:50:54.000395Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-06-15T01:50:54.123893Z 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: 1ecbb407-cd7c-11eb-bfed-000c2932fba3.
2021-06-15T01:50:54.124628Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-06-15T01:50:54.680636Z 0 [Warning] CA certificate ca.pem is self signed.
2021-06-15T01:50:54.947242Z 1 [Note] A temporary password is generated for [email protected]: ,Jf0Z8N<5aRL

(8) 启动MySQL服务

[[email protected] ~]$ sudo systemctl start mysqld

(9) 登录MySQL数据库

[[email protected] ~]$ mysql -uroot -p
Enter password:输入临时生成的密码
  登录成功.

(10) 必须先修改root用户的密码, 否则执行其他的操作会报错

mysql> set password = password("新密码");

(11) 修改mysql库底下的user表中的root用户允许任意IP链接

mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

(12) 修改/etc/my.cnf文件, 加入如下内容

collation_server = utf8_general_ci
character_set_server = utf8
lower_case_table_names = 1
max_allowed_packet=1024M
[client]
default_character_set=utf8


(13) 查看数据库编码

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41327922/article/details/117920067

智能推荐

随便推点