1.安装准备
1.1查看是否有自带的MySql 库,如果先有卸载
[root@ecs-cde0 ~]# rpm -qa | grep mysql
mysql-community-client-5.7.12-1.el6.x86_64
mysql-community-common-5.7.12-1.el6.x86_64
mysql-community-server-5.7.12-1.el6.x86_64
mysql-community-libs-5.7.12-1.el6.x86_64
rpm -e --nodeps mysql-community-server-5.7.12-1.el6.x86_64
rpm -e --nodeps mysql-community-common-5.7.12-1.el6.x86_64
rpm -e --nodeps mysql-community-libs-5.7.12-1.el6.x86_64
rpm -e --nodeps mysql-community-client-5.7.12-1.el6.x86_64
[root@ecs-cde0 mysql]# find / -name mysql
全部删除
[root@ecs-cde0 ~]# find / -name mysql
/root/software/mysql
/usr/bin/mysql
/usr/lib64/mysql
/usr/share/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/etc/logrotate.d/mysql
rm -rf **
2.下载mysql
[root@ecs-cde0 mysql]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar
[root@ecs-cde0 mysql]# ll
总用量 447216
-rw-r--r-- 1 root root 457492480 3月 29 2016 mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar
解压
[root@ecs-cde0 mysql]# tar -xvf mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar
mysql-community-libs-5.7.12-1.el6.x86_64.rpm
mysql-community-test-5.7.12-1.el6.x86_64.rpm
mysql-community-common-5.7.12-1.el6.x86_64.rpm
mysql-community-devel-5.7.12-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.12-1.el6.x86_64.rpm
mysql-community-embedded-5.7.12-1.el6.x86_64.rpm
mysql-community-server-5.7.12-1.el6.x86_64.rpm
mysql-community-embedded-devel-5.7.12-1.el6.x86_64.rpm
mysql-community-client-5.7.12-1.el6.x86_64.rpm
2.步骤:安装mysql-community-server之前,
必须安装mysql-community-client和mysql-community-common的rpm包。
而安装community-client和community-common包之前,必须删除mysql-lib(因为系统自带的版本过低) 删除mysql-lib(系统自带的版本过低)
yum remove mysql-libs
3.依次安装如下软件包(common、libs、client、server)
[root@localhost software]# rpm -ivh mysql-community-common-5.7.12-1.el6.x86_64.rpm
[root@localhost software]# rpm -ivh mysql-community-libs-5.7.12-1.el6.x86_64.rpm
[root@localhost software]# rpm -ivh mysql-community-client-5.7.12-1.el6.x86_64.rpm
[root@localhost software]# rpm -ivh mysql-community-server-5.7.12-1.el6.x86_64.rpm
[root@ecs-cde0 mysql]# service mysqld start
Starting mysqld (via systemctl): [ OK ]
安装成功,获取root密码,然后修改
[root@ecs-cde0 mysqld]# grep 'A temporary password' /var/log/mysqld.log
2018-08-24T07:31:28.820521Z 1 [Note] A temporary password is generated for root@localhost: =%dcgfO3+xpZ
[root@ecs-cde0 mysqld]# mysql -uroot -p=%dcgfO3+xpZ
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 4
Server version: 5.7.12
Copyright (c) 2000, 2016, 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> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
这一步一定要有不然不能下一步
mysql> SET PASSWORD = PASSWORD('xxx.');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> use mysql;
mysql> update user set authentication_string=PASSWORD('Kaiyun168.') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit;
Bye
[root@ecs-cde0 mysqld]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.12 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxx.' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
如果修改密码出现问题执行如下操作:
还有一种方式 就是直接相当于忘记密码
MySQL忘记root密码,而且不能使用操作系统认证直接进入数据库修改密码时,需要重置root密码。
1、在配置文件/etc/my.cnf添加skip-grant-tables一行,跳过密码验证。
2、重启mysql数据库主进程# /etc/init.d/mysqld restart(也可以直接先停掉MySQL进程后使用skip-grant-tables参数重启MySQL)
3、登录数据库修改密码。
注意:如果只想设置简单密码需要修改两个全局参数:
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.01 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.01 sec)
mysql> use mysql;
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> set password=password("Xxx888888.");
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
修改成功,顺便开启远程访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
启动mysql
systemctl start mysqld.service
结束
systemctl stop mysqld.service
重启
systemctl restart mysqld.service
开机自启
systemctl enable mysqld.service
查看mysql版本
select version();
到此可以 了
另外可能用到的优化参数
my.cnf配置的几个参数:
skip-name-resolve 跳过DNS反向解析过程.(这样就不能使用主机名连接mysql了,只能使用ip连接)
skip-grant-tables 跳过授权表(当mysql登陆密码忘记时的解决办法)
skip-networking 跳过TCP/IP连接
skip-host-cache 禁用主机名缓存;要想清除主机名缓存,执行FLUSH HOSTS语句或执行mysqladmin flush-hosts命令