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了。
