安装前准备
- 1.查看已经安装的mysql mariadb
[ll@ll ~]$ rpm -aq | grep mysql
qt-mysql-4.8.5-13.el7.x86_64
akonadi-mysql-1.9.2-4.el7.x86_64
- 2.卸载旧的版本
rpm -e mysql --nodeps --allmatches (不理会依赖关系,删除所有上一步查出来的相同的mysql)
[root@ll ll]# rpm -aq | grep mysql
qt-mysql-4.8.5-13.el7.x86_64
akonadi-mysql-1.9.2-4.el7.x86_64
[root@ll ll]# rpm -e qt-mysql-4.8.5-13.el7.x86_64 --nodeps --allmatches
[root@lyh lyh]# rpm -e akonadi-mysql-1.9.2-4.el7.x86_64 --nodeps --allmatches
[root@lyh lyh]# rpm -aq | grep mysql
[root@lyh lyh]#
- 3.手工删除老版本的几个文件
[root@ll ll]# rm -f /etc/my.cnf
[root@ll l]# rm -f /etc/my.cnf.d
rm: cannot remove ‘/etc/my.cnf.d’: Is a directory
[root@ll ll]# rm -rf /etc/my.cnf.d
[root@ll ll]# rm -rf /var/lib/mysql
[root@ll ll]# rm -rf /var/share/mysql
[root@ll ll]# rm -rf /usr/bin/mysql*
[root@ll ll]#
下载
网址:https://dev.mysql.com/downloads/mysql/
点击 No thanks, just start my download. 进行下载
安装
- 解压
[ll@ll mysql]$ sudo tar -xvf mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.0.22-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm
mysql-community-common-8.0.22-1.el7.x86_64.rpm
mysql-community-devel-8.0.22-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.22-1.el7.x86_64.rpm
mysql-community-libs-8.0.22-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.22-1.el7.x86_64.rpm
mysql-community-server-8.0.22-1.el7.x86_64.rpm
mysql-community-test-8.0.22-1.el7.x86_64.rpm
[ll@ll mysql]$
- 安装 common
[ll@ll mysql]$ sudo rpm -ivh mysql-community-common-8.0.22-1.el7.x86_64.rpm --nodeps --force
warning: mysql-community-common-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-common-8.0.22-1.e################################# [100%]
[ll@ll mysql]$
- 安装 libs
[ll@ll mysql]$ sudo rpm -ivh mysql-community-libs-8.0.22-1.el7.x86_64.rpm --nodeps --force
warning: mysql-community-libs-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-libs-8.0.22-1.el7################################# [100%]
[ll@ll mysql]$
- 安装 client
[ll@ll mysql]$ sudo rpm -ivh mysql-community-client-8.0.22-1.el7.x86_64.rpm --nodeps --force
warning: mysql-community-client-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-client-8.0.22-1.e################################# [100%]
[ll@ll mysql]$
- 安装 server 及查看安装包
[ll@ll mysql]$ sudo rpm -ivh mysql-community-server-8.0.22-1.el7.x86_64.rpm --nodeps --force
warning: mysql-community-server-8.0.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-server-8.0.22-1.e################################# [100%]
[ll@ll mysql]$ sudo rpm -qa | grep mysql
mysql-community-libs-8.0.22-1.el7.x86_64
mysql-community-server-8.0.22-1.el7.x86_64
mysql-community-common-8.0.22-1.el7.x86_64
mysql-community-client-8.0.22-1.el7.x86_64
[ll@ll mysql]$
- mysql 数据库的初始化
[ll@ll mysql]$ sudo mysqld --initialize
[ll@ll mysql]$ sudo chown mysql:mysql /var/lib/mysql -R
[ll@ll mysql]$ sudo systemctl start mysqld.service
[ll@ll mysql]$ sudo systemctl enable mysqld
[ll@ll mysql]$
登录
- 密码
oracle收购Mysql后,新版的mysql刚安装完mysql后不能直接输入mysql登录数据库,
而是设置了默认密码,通过cat /var/log/mysqld.log | grep password 可以查询密码,登陆后需要修改密码
[ll@ll mysql]$ sudo cat /var/log/mysqld.log | grep password
2020-10-27T15:13:34.701604Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: qlvk<Cjl7Fnk
[ll@ll mysql]$
- 登录修改密码
[ll@ll mysql]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22
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> set password='123456';
Query OK, 0 rows affected (0.00 sec)
`
- 推出再次登录
mysql> quit
Bye
[ll@ll mysql]$ mysql -uroot -p123456
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 10
Server version: 8.0.22 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> select now() from dual;
+---------------------+
| now() |
+---------------------+
| 2018-10-27 23:37:03 |
+---------------------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
常用命令
服务
开启服务:service mysql start
关闭服务:service mysql stop
重启服务:service mysql restart
登录
- 密码明文
语法:mysql -u用户名 -p用户密码
举例:mysql -uroot -p123456
- 密码密文
语法:mysql -u用户名 -p+回车,然后输入密码
举例:mysql -uroot -p
[ll@ll mysql]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.22 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>
修改密码
- SET PASSWORD命令(需登录)
语法:mysql> set password for 用户名@localhost = password(‘新密码’);
举例:mysql>set password for root@localhost = password(‘123456’); - 使用sql语句更新 mysql 库中的 user 表(需登录)
mysql> use mysql;
mysql> update user set password=password('123') where user='root' and host='localhost';
mysql> flush privileges;
- mysqladmin命令,明文(不需登录)
语法:mysqladmin -u用户名 -p旧密码 password 新密码
举例:mysqladmin -uroot -p123 password 123456 - mysqladmin命令,密文(不需登录)
语法:mysqladmin -u用户名 -p旧密码 password+回车,然后输入两次新密码
举例:mysqladmin -uroot -p123 password