【教程】CentOS安装MySQL
一、MySQL下载
链接:
https://dev.mysql.com/downloads/mysql/
选择查看过往版本
选择小红帽(CentOS内核就是Red Hat),并在下方选择适合自己的版本,我这里是CentOS 6,所以选择linux 6
选这个下载,之后等着就行了
下载完成后将其上传到CentOS上(如果此处有问题可以留言)
二、环境准备
注:后续所有步骤都要使用root用户
建议:安装mysql前拍个快照~
[hadoop@bigdata ~]$ su root
Password:
[root@bigdata hadoop]#
在根目录创建一个文件夹,并将mysql移动此处,路径因人而异
[root@bigdata ~]# cd ~
[root@bigdata ~]# mkdir mysqlrpm
[root@bigdata ~]# cd mysqlrpm/
[root@bigdata mysqlrpm]# mv /opt/tools/mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar ./
[root@bigdata mysqlrpm]# ll
total 478276
-rw-rw-r-- 1 hadoop hadoop 489748480 Oct 4 17:27 mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar
将压缩包所有者更改为root(如果你一直使用root用户进行操作,则可以忽略此指令)
[root@bigdata mysqlrpm]# chown root:root mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar
将压缩包解压到此处
[root@bigdata mysqlrpm]# tar -xf mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar
[root@bigdata mysqlrpm]# ll
total 956560
-rw-rw-r-- 1 root root 489748480 Oct 4 17:27 mysql-5.7.31-1.el6.x86_64.rpm-bundle.tar
-rw-r--r-- 1 7155 31415 25820440 Jun 3 18:07 mysql-community-client-5.7.31-1.el6.x86_64.rpm
-rw-r--r-- 1 7155 31415 378784 Jun 3 18:07 mysql-community-common-5.7.31-1.el6.x86_64.rpm
-rw-r--r-- 1 7155 31415 3891548 Jun 3 18:07 mysql-community-devel-5.7.31-1.el6.x86_64.rpm
-rw-r--r-- 1 7155 31415 39565156 Jun 3 18:07 mysql-community-embedded-5.7.31-1.el6.x86_64.rpm
-rw-r--r-- 1 7155 31415 137363700 Jun 3 18:07 mysql-community-embedded-devel-5.7.31-1.el6.x86_64.rpm
-rw-r--r-- 1 7155 31415 2511392 Jun 3 18:07 mysql-community-libs-5.7.31-1.el6.x86_64.rpm
-rw-r--r-- 1 7155 31415 1761464 Jun 3 18:07 mysql-community-libs-compat-5.7.31-1.el6.x86_64.rpm
-rw-r--r-- 1 7155 31415 169502800 Jun 3 18:08 mysql-community-server-5.7.31-1.el6.x86_64.rpm
-rw-r--r-- 1 7155 31415 108944888 Jun 3 18:08 mysql-community-test-5.7.31-1.el6.x86_64.rpm
卸载冲突组件
[root@bigdata mysqlrpm]# rpm -qa | grep postfix
postfix-2.6.6-8.el6.x86_64
[root@bigdata mysqlrpm]# rpm -e --nodeps postfix-2.6.6-8.el6.x86_64
[root@bigdata mysqlrpm]# rpm -qa | grep postfix
[root@bigdata mysqlrpm]#
安装依赖
[root@bigdata mysqlrpm]# yum -y install libaio
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirrors.neusoft.edu.cn
* extras: mirrors.neusoft.edu.cn
* updates: mirrors.neusoft.edu.cn
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
Package libaio-0.3.107-10.el6.x86_64 already installed and latest version
Nothing to do
[root@bigdata mysqlrpm]#
检查并卸载CentOS自带的mysql
[root@bigdata mysqlrpm]# rpm -qa | grep mysql
mysql-libs-5.1.73-8.el6_8.x86_64
[root@bigdata mysqlrpm]# rpm -e --nodeps mysql-libs-5.1.73-8.el6_8.x86_64
[root@bigdata mysqlrpm]# rpm -qa | grep mysql
[root@bigdata mysqlrpm]#
三、安装
注:安装顺序务必与我一样
安装mysql核心
[root@bigdata mysqlrpm]# rpm -ivh mysql-community-common-5.7.31-1.el6.x86_64.rpm
warning: mysql-community-common-5.7.31-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-common ########################################### [100%]
[root@bigdata mysqlrpm]#
安装mysql的库文件
[root@bigdata mysqlrpm]# rpm -ivh mysql-community-libs-5.7.31-1.el6.x86_64.rpm
warning: mysql-community-libs-5.7.31-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-libs ########################################### [100%]
[root@bigdata mysqlrpm]#
安装mysql客户端
[root@bigdata mysqlrpm]# rpm -ivh mysql-community-client-5.7.31-1.el6.x86_64.rpm
warning: mysql-community-client-5.7.31-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-client ########################################### [100%]
[root@bigdata mysqlrpm]#
安装mysql服务端
[root@bigdata mysqlrpm]# rpm -ivh mysql-community-server-5.7.31-1.el6.x86_64.rpm
warning: mysql-community-server-5.7.31-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ########################################### [100%]
1:mysql-community-server ########################################### [100%]
[root@bigdata mysqlrpm]#
初始化mysql服务
[root@bigdata mysqlrpm]# service mysqld status
mysqld is stopped
[root@bigdata mysqlrpm]# service mysqld start
Initializing MySQL database: [ OK ]
Starting mysqld: [ OK ]
[root@bigdata mysqlrpm]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@bigdata mysqlrpm]#
查看log,找到初始化的密码(我的密码:hgfudE2pg/BC,每个人的都不一样,完全随机并且可能带符号)
[root@bigdata mysqlrpm]# grep password /var/log/mysqld.log
2020-10-04T09:56:00.958460Z 1 [Note] A temporary password is generated for root@localhost: hgfudE2pg/BC
2020-10-04T09:56:06.111361Z 2 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
2020-10-04T09:56:25.898557Z 0 [Note] Shutting down plugin 'validate_password'
2020-10-04T09:56:27.542885Z 0 [Note] Shutting down plugin 'sha256_password'
2020-10-04T09:56:27.542895Z 0 [Note] Shutting down plugin 'mysql_native_password'
2020-10-04T09:56:28.974530Z 2 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'@'localhost' (using password: NO)
使用初始密码(上面日志中找到的)登录mysql
[root@bigdata mysqlrpm]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31
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>
修改密码
mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
之后退出mysql重启mysql服务使密码生效
mysql> exit
Bye
[root@bigdata mysqlrpm]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
重新登录一下,测试新密码
[root@bigdata mysqlrpm]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 MySQL Community Server (GPL)
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>
设置mysql服务开机自启
mysql> exit
Bye
[root@bigdata mysqlrpm]# chkconfig mysqld on
[root@bigdata mysqlrpm]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
登录mysql,设置允许root远程登陆
[root@bigdata mysqlrpm]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31 MySQL Community Server (GPL)
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
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
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql>
退出并重启mysql服务,使配置生效
mysql> exit;
Bye
[root@bigdata mysqlrpm]# serivce mysqld restart
bash: serivce: command not found
[root@bigdata mysqlrpm]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@bigdata mysqlrpm]#
在windows环境下远程连接CentOS中Mysql,测试一下
PS C:\Program Files\MySQL\MySQL Server 8.0\bin> ./mysql -h 你的ip或host -P 3306 -u root -p
Enter password: **********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 MySQL Community Server (GPL)
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>