1.查看系统中是否已安装 MySQL 服务
#rpm -qa | grep mysql
#yum list installed | grep mysql
如果已经有安装的,用命令删掉
#yum -y remove ...
2.下载安装
#wget http://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm //下载
#rpm -ivh mysql57-community-release-el7-9.noarch.rpm
#yum install mysql-server //一路选y,直到安装完成
3.配置
安装完毕后,在 /var/log/mysqld.log 文件中会自动生成一个随机的密码,需要先取得这个随机密码
#grep "password" /var/log/mysqld.log
2017-08-06T04:23:22.939636Z 1 [Note] A temporary password is generated for root@localhost: aZqHg=eQG9Nq
使用临时密码登陆mysql并更新用户 root 的密码
#[root@iZjafzbmqur2lzZ ~]# mysql -u root -paZqHg=eQG9Nq
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 5
Server version: 5.7.19
Copyright (c) 2000, 2017, 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>
打印以上信息表示登陆成功,设置用户 root 可以在任意 IP 下被访问:
mysql> set password = password('新密码');
mysql> grant all privileges on *.* to root@"%" identified by "新密码"; //设置用户 root 可以在任意 IP 下被访问
mysql> grant all privileges on *.* to root@"localhost" identified by "新密码";
mysql> show databases; //查看当前都内置了哪些数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit //退出
4. 启动
#service mysqld start //启动 MySQL 服务
#service mysqld restart //重启 MySQL
#ervice mysqld status //查看 MySQL 的状态
5. 忘记密码时,可用如下方法重置:
#service mysqld stop
# mysqld_safe --user=root --skip-grant-tables --skip-networking &
# mysql -u root
mysql> use mysql;
mysql> update user set password=password("new_password") where user="root";
mysql> flush privileges;