Ubuntu18.4安装MySQL

原文地址

分类目录

Ubuntu18.4安装MySQL

Ubuntu下MySQL可视化工具mysql-workbench的安装和简单使用

MySQL服务命令

Mysql忘记密码

mysql用户操作

SQL语句小结

Linux下彻底卸载MySQL

下载安装apt安装支持,下载地址

1580885853377

需要登录才能完成下载,选择打开,通过~~~

1580885955078

下载完成后直接安装即可

Ubuntu中,默认情况下,只有最新版本的MySQL包含在APT软件包存储库中,要安装它,只需更新服务器上的包索引并安装默认包apt

$ sudo apt update

安装mysql

$ sudo apt install mysql-server

对mysql进行初始化配置

$ sudo 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
    # 设置(mysql的)root密码
    Please set the password for root here.
    # 设置密码
    New password: 
    # 再次输入密码
    Re-enter new password: 
    # MySQL安装过程默认有一个匿名用户,允许任何用户在没有自己的账号的情况下登录MySQL。这是指为了测试,也为了是安装过程更加顺畅,你应该在将MySQL应用到生产环境之前删掉这些匿名用户
    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.
    
    # 通常,root用户应该只允许本地登录,这是为了确保其他人不能通过网络破解root用户密码
    Normally, root should only be allowed to connect from
    'localhost'. This ensures that someone cannot guess at
    the root password from the network.
    # 是否禁用root用户远程登录?(请视自己情况而定)
    # 个人觉得即使需要远程登录,另外设置一个用户比较好,为其赋予相应权限即可
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N
    
     ... skipping.
    # 通常,MySQL安装会伴随产生一个任何人都可以访问的名为‘test’的数据库。这也只是为了测试,你应该在将MySQL应用到生产韩静之前删掉他
    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! 
    

因为上面没有启用密码验证插件,所以直接登录(直接键入命令mysql)也是可以成功的

出了个问题是,在(系统的)非root用户模式下无法登录,可以是因为我上面都是用sudo命令执行的原因?

登录

sudo mysql -u root -p
# 输入初始化配置中设置的密码

如果能顺利进入

$ sudo mysql -u root -p
[sudo] zy 的密码: 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, 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>模式下,就说明基本上安装成功了。

如果有需要还可以做一些修改,比如在初始化配置中的配置。

进入名为mysql的数据库

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

查看mysql数据库下的user表的一些表项

mysql> select User,authentication_string,Host from user;
+------------------+-------------------------------------------+-----------+
| User             | authentication_string                     | Host      |
+------------------+-------------------------------------------+-----------+
| root             |                                           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| debian-sys-maint | *C9421C749D53FA0BCB8F169CBD3ED58D0B1545C5 | localhost |
+------------------+-------------------------------------------+-----------+
4 rows in set (0.00 sec)

为了使显示不错位,再建另一个查询来查看plugin这个属性

mysql> select User,Host,plugin from user;
+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| root             | localhost | auth_socket           |
| mysql.session    | localhost | mysql_native_password |
| mysql.sys        | localhost | mysql_native_password |
| debian-sys-maint | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)

其中,authentication_string是加密后的密码,

可以通过以下命令修改用户的密码

set password for root@localhost= password('newpwd');	# password()的作用是为密码加密

Host是允许的访问来源,为‘%’表示可从任意IP访问,为某IP表示可以从某IP访问,为localhost表示只能本地(本机)访问,通过一下命令可以允许用户远程访问。

update user set Host = '%' where user = 'root';

plugin就是初始化配置中说道的密码验证插件,为‘auth_socket’表示可以无密码登录,‘mysql_native_password’表示需要密码方可登录,可以通过以下面临修改用户的这个插件。

update user set plugin='mysql_native_password' where user='root';

最后

flush privileges;

令截止目前的配置立即生效

更多用户操作可以参见 mysql用户操作

参考文献

Ubuntu19.04 安装 MySQL 8.0.16

mysql用户操作

发布了102 篇原创文章 · 获赞 68 · 访问量 5136

猜你喜欢

转载自blog.csdn.net/BBJG_001/article/details/104192728