Ubuntu安装mysql并修改密码

1、首先要更新源,执行apt update命令。

2、执行apt install mysql-server命令,然后输入Y即可安装。

3、安装好之后就开始配置,直接输入:mysql_secure_installation,具体配置如下:

 

    Securing the MySQL server deployment.   

    Connecting to MySQL using a blank password.   

    VALIDATE PASSWORD PLUGIN can be used to test passwords   and improve security. It checks the strength of password   and allows the users to set only those passwords which are  secure enough. Would you like to setup VALIDATE PASSWORD plugin?   

    Press y|Y for Yes, any other key for No: N

    Please set the password for root here.   

    New password:   

    Re-enter new password:

    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) : N

    

     ... 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) : Y

    Success.

    

    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) : N   

     ... 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) : Y

    Success.

    

    All done!

 

 

4、配置远程访问,首先编辑配置文件,通过:vim /etc/mysql/mysql.conf.d/mysqld.cnf命令进行编辑,将bind-address           = 127.0.0.1注释掉,如下图所示:

 

5、输入mysql -uroot -p命令进入mysql中,如下图所示:

 

6、然后输入:grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;表示允许任何主机远程连接,连接的密码是123456,如下图所示:

 

下面就登录mysql:

 

1.直接执行     sudo mysql -uroot -p

 

如果要求输入密码,直接按Enter键就能进入了。

 

2.选中mysql数据库   use mysql;

 

3.update user set authentication_string=PASSWORD(“在这里设置新密码”) where user=‘root’;

 

4.如果需要远程登录的话就执行

 

     update user set plugin=“mysql_native_password”;

 

5.执行\q退出mysql并重启mysql:/etc/init.d/mysql restart

 

然后就可以用新密码登录mysql了。

posted @ 2022-03-22 11:55  Jason_huawen  阅读(1066)  评论(0编辑  收藏  举报