MySql安装与使用
一、安装
1、下载
[root@localhost ~]# wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-7.noarch.rpm
2、将下载的文件引入你的repo库中
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-7.noarch.rpm
3、安装MySQL数据库服务器
[root@localhost ~]# yum -y install mysql-community-server
4、启动mysql服务
[root@localhost ~]# systemctl restart mysqld
5、查询root密码
[root@localhost ~]# grep “password” /var/log/mysqld.log
2018-10-08T05:22:38.311433Z 1 [Note] A temporary password is generated for root@ localhost: RglpQQrpr9#W
6、root账户登录MySql
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
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>
7、修改密码
输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库修改密码命令:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Houjianjun@1234';
Query OK, 0 rows affected (0.00 sec)
1)、查看MySQL完整的初始密码规则
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.01 sec)
2)、修改MySQL默认策略和密码长度
①修改密码策
因为当前的密码太复杂不方便后期做实验,所以使用命令修改密码策略两种方式:
mysql> set global validate_password_policy=0;
如何在Centos7下安装MySQL5.7
mysql> set global validate_password_policy=LOW;
如何在Centos7下安装MySQL5.7
注:密码策略分四种
1、OFF(关闭) 2、LOW(低) 3、MEDIUM(中) 4、STRONG(强)
②修改密码长度
上边改完策略之后我们在改长度 mysql> SET GLOBAL validate_password_length=4;
修改为简单密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '1234';
8、初始化数据库
[root@localhost ~]# mysql_secure_installation
Securing the MySQL server deployment.
#输入密码
Enter password for user root:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 50
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
... skipping.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
... skipping.
All done!
9、开启远程连接
mysql> grant all privileges on *.* to root@'%' identified by "1234";
mysql> flush privileges;
10、添加防火墙端口
[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
重新载入:
[root@localhost ~]# firewall-cmd --reload