Ubuntu22.04安装和使用MySQL

安装MySQL

更新列表

sudo apt-get update

安装MySQL服务器

sudo apt-get install mysql-server

在安装过程中,系统将提示您创建root密码。选择一个安全的,并确保记住它,因为后面需要用到这个密码。

安装MySQL客户端

sudo apt-get install mysql-client

配置MySQL

运行MySQL初始化安全脚本

sudo mysql_secure_installation

mysql_secure_installation脚本设置的东西:更改root密码、移除MySQL的匿名用户、禁止root远程登录、删除test数据库和重新加载权限。除了询问是否要更改root密码时,看情况是否需要更改,其余的问题都可以按Y,然后ENTER接受所有后续问题的默认值。使用上面的这些选项可以提高MySQL的安全。

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT 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 component?

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

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

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) : y
Success.


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

 ... 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) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

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

无论你如何安装它,MySQL应该已经开始自动运行。要测试它,请检查其状态。

sudo service mysql status

将看到类似于以下内容的输出:


● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: active (running) since Tue 2023-10-31 20:06:13 CST; 8min ago
    Process: 4885 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=e>
   Main PID: 4893 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 19050)
     Memory: 366.3M
        CPU: 5.410s
     CGroup: /system.slice/mysql.service
             └─4893 /usr/sbin/mysqld

 

MySQL数据库基本使用

启动MySQL数据库服务

sudo service mysql start
或
sudo systemctl start mysql.service

重启MySQL数据库服务

sudo service mysql restart
或
sudo systemctl restart mysql.service

停止MySQL数据库服务

sudo service mysql stop
或
sudo systemctl stop mysql.service

查看MySQL运行状态

sudo service mysql status
或
sudo systemctl status mysql.service

设置MySQL服务开机自启动

sudo service mysql enable
或
sudo systemctl enable mysql.service

停止MySQL服务开机自启动

sudo service mysql disable
或
sudo systemctl disable mysql.service

MySQL的配置文件

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

配置MySQL远程登录

有时候,为了开发方便,我们需要使用本地电脑远程访问和管理MySQL数据库。默认情况下,为了安全MySQL只允许本地登录,如果要开启远程连接,则需要修改/etc/mysql/mysql.conf.d/mysqld.cnf配置文件。

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 打开配置文件,找到bind-address = 127.0.0.1这一行
# 改为bind-address = 0.0.0.0即可或简单一点注释掉也行

# 修改完成保存后,需要重启MySQL服务才会生效
sudo systemctl restart mysql.service

接着需要为用户赋予远程登录的权限,使用以下用户授权步骤即可

MySQL创建用户与授权

使用root用户登录

# 登录mysql并输入密码 
mysql -u root -p

# mysql8 修改密码方式
alter user 'root'@'localhost' identified by '这里填你要的密码';

ubuntu用apt安装mysql8.0过程中没有提示设置密码

退出MySQL命令行

mysql> quit

创建用户

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

username:你将创建的用户名
host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器

注意,使用%通配符创建外网访问用户后,登录时需要明确访问的IP地址

用户授权

GRANT privileges ON databasename.tablename TO 'username'@'host';

privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
databasename:数据库名
tablename:表名
如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*

注意:用以上命令授权的用户不能给其它用户授权,如果想让被授权的用户可以将他的拥有的权限授给其他用户,用以下命令

GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;

刷新授权

flush privileges;

这一步一定要做,不然无法成功!这句表示从mysql数据库的grant表中重新加载权限数据,因为MySQL把权限都放在了cache中,所以在做完更改后需要重新加载。

设置与更改用户密码

ALTER USER 'username'@'host' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;  

撤销用户权限

REVOKE privileges ON databasename.tablename FROM 'username'@'host';

privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
databasename:数据库名
tablename:表名
如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,

删除用户

DROP USER 'username'@'host';

查看用户信息

SELECT User, Host FROM user;

查看用户权限

show grants for 'username'@'host';

查看加密的规则

select Host,User,plugin from mysql.user;

修改加密方式

ALTER USER 'username'@'host' IDENTIFIED WITH mysql_native_password BY 'password';

mysql_native_password:加密方式

password:用户密码

创建数据库

登录root用户后,使用 create 命令创建数据库,语法如下:

CREATE DATABASE 数据库名;

删除数据库

DROP DATABASE 数据库名称;

MySQL查看信息

使用MySQL时,需要了解当前数据库的情况,例如当前的数据库大小、字符集、用户等等。下面总结了一些查看数据库相关信息的命令。

查看显示所有数据库

show databases;

查看当前使用的数据库

select database();

查看数据库使用端口

show variables like 'port';

查看数据库的表信息

show tables;

查看表结构

show columns from table_name; 
或
describe table_name;

显示表结构,字段类型,主键,是否为空等属性,但不显示外键。

查看表生成的DDL

DDL(data definition language)数据库定义语言:其实就是我们在创建表的时候用到的一些sql,比如说:CREATE、ALTER、DROP等。DDL主要是用在定义或改变表的结构,数据类型,表之间的链接和约束等初始化。

show create table table_name;

该命令把创建表的DDL,表结构、类型,外键,备注等全部显示出来。

查看Mysql数据库大小

进入information_schema 数据库(存放了其他的数据库的信息)

use information_schema;

查询所有数据的大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;

查看指定数据库的大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='数据库名称';

查看指定数据库的某个表的大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='数据库名称' and table_name='表名称';

查看索引所占的空间大小

select concat(round(sum(index_length/1024/1024),2),'MB') as data from tables where table_schema='mysql';

查看数据文件存放路径

show variables like '%datadir%';

查看数据库编码

show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

character_set_client 为客户端编码方式;

character_set_connection 为建立连接使用的编码;

character_set_database 为数据库的编码;

character_set_results 为结果集的编码;

character_set_server 为数据库服务器的编码;

查看数据库的最大连接数

show variables like '%max_connections%';

查看数据库当前连接数,并发数

show status like 'Threads%';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_cached    | 0     |
| Threads_connected | 27    |
| Threads_created   | 48    |
| Threads_running   | 1     |
+-------------------+-------+
4 rows in set (0.00 sec)

Threads_cached 代表当前此时此刻线程缓存中有多少空闲线程。

Threads_connected 代表当前已建立连接的数量,因为一个连接就需要一个线程,所以也可以看成当前被使用的线程数。

Threads_created 代表从最近一次服务启动,已创建线程的数量。

Threads_running 代表当前激活的(非睡眠状态)线程数。并不是代表正在使用的线程数,有时候连接已建立,但是连接处于sleep状态,这里相对应的线程也是sleep状态。

 

 

 

 

THE END
分享
二维码
打赏
海报
Ubuntu22.04安装和使用MySQL
安装MySQL 更新列表 sudo apt-get update 安装MySQL服务器 sudo apt-get install mysql-server 在安装过程中,系统将提示您创建root密码。选择一个安全的,并……
<<上一篇
下一篇>>
文章目录
关闭
目 录