背景
之前一直使用yum方式安装,非常easy,具体命令为:
yum install -y mysql-server mysql
(同时安装mysql服务端和客户端)
这种方式安装的mysql版本是不可控的,不见得是你希望的版本,我这边安装后的mysql版本是5.1,太老了,所以希望只能尝试手动来安装指定版本了
具体步骤
到官网下载指定版本
官网地址:
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
(需要提前注册一个oracle帐号,即可免费下载)
官网是英文,故附带三张图指导大家选择到期望的版本
这张图可以带大家找到相应的mysql版本
不同操作系统下的Mysql安装包是不一样的,所以还需要进一步选择对应的操作系统版本
在明确了操作系统版本与Mysql版本后,选择对应的rpm安装包,原则上下载如下4个红框框的内容即可(如果你觉得麻烦也可以下载第一个mysql-5.7.23-1.el6.x86_64.rpm-bundle.tar,然后进行解压也可以提到红框框中的内容)
安装mysql
- 检查系统自带的MySQL及相关RPM包,是否安装
如果有安装,则移除(检查是否已经安装的命令:rpm -qa|grep mysql)
yum -y remove mysql
小点心:如果这种方式删除后再检测还发现有mysql安装包,则可以进一步执行 rpm -e --nodeps 进行删除(示例:rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64)
2. 创建用户和组(如果已经创建则跳过)
groupadd mysql
useradd -r -g mysql mysql
3. 进入安装包所在的目录,依次安装
rpm -ivh mysql-community-common-5.7.23-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.23-1.el6.x86_64.rpm
rpm -ivh mysql-community-client-5.7.23-1.el6.x86_64.rpm
rpm -ivh mysql-community-server-5.7.23-1.el6.x86_64.rpm
到此,mysql已经安装完成。
启动Mysql
启动命令:
service mysqld start
或者 /etc/init.d/mysqld start
确认是否启动成功
# ps aux|grep mysql
root 597 0.0 0.0 106248 1544 pts/0 S 11:28 0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 791 0.0 0.0 1202736 195696 pts/0 Sl 11:28 0:02 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
验证Mysql
mysql5.7给root用户添加了默认密码,密码内容可以通过日志文件/var/log/mysqld.log来进行查看。
[root@4ee589478b4b /]# more /var/log/mysqld.log |grep -i pass
2018-11-04T13:03:09.372759Z 1 [Note] A temporary password is generated for root@localhost: N:sZXF.h33oH
2018-11-04T13:04:46.308722Z 0 [Note] Shutting down plugin 'sha256_password'
2018-11-04T13:04:46.308725Z 0 [Note] Shutting down plugin 'mysql_native_password'
另外还要求进行执行任何sql语句前必须修改密码
[root@4ee589478b4b /]# mysql -p',b7yF!hp4wqB'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.23
Copyright (c) 2000, 2018, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改root密码的命令
mysql> update mysql.user set authentication_string = password('123456'), password_expired = 'N', password_last_changed = now() where user = 'root';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
如果上面那种修改密码的方式不好使,就使用下面这个办法
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)