Ubuntu快速安装MySQL全攻略
2026/6/12 2:21:05 网站建设 项目流程

这里介绍ubuntu发行版本上使用apt命令安装的步骤。不同发行版本安装方法有所不同。

一、安装mysq

安装前首先切换到管理员身份:

sudo su

然后再执行命令安装:

apt install mysql-server

stu@stu-virtual-machine:~$ sudo su [sudo] stu 的密码: root@stu-virtual-machine:/home/stu# apt install mysql-server

二、初始化配置

初始化配置使用命令:

mysql_secure_installation

1.设置数据库不进行密码强校验

root@stu-virtual-machine:/home/stu# mysql_secure_installation 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: N (输入N,不进行密码的强校验)

2.设置root管理员密码

注意是数据库的管理员的。密码不回显,根据提示输入两遍

Please set the password for root here. New password: (此处密码不回显) Re-enter new password: (此处密码不回显)

3.设置是否要删除匿名用户

这里不删除

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.

4.设置是否允许root用户远程登录

这里设置允许

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.

5.是否删除'test'

这里选择不删除

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.

6.设置修改的权限立即生效

此时所有配置初始化完成

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! root@stu-virtual-machine:/home/stu#

三、数据库服务启动停止

1.检查服务器状态

命令为:

service mysql status

或者

systemctl status mysql.service

root@stu-virtual-machine:/home/stu# service mysql status ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-06-21 13:36:08 CST; 24min ago Main PID: 4672 (mysqld) Status: "Server is operational" Tasks: 39 (limit: 4617) Memory: 335.5M CGroup: /system.slice/mysql.service └─4672 /usr/sbin/mysqld 6月 21 13:36:07 stu-virtual-machine systemd[1]: Starting MySQL Community Server... 6月 21 13:36:08 stu-virtual-machine systemd[1]: Started MySQL Community Server. root@stu-virtual-machine:/home/stu#

2.重启|停止|启动,数据库的命令

如果不是管理员需要在命令前面加上==sudo==

重启:/etc/init.d/mysqlrestart
停止:/etc/init.d/mysqlstop
启动:/etc/init.d/mysqlstart

或者执行如下命令

root@stu-virtual-machine:/home/stu# service mysql restart
root@stu-virtual-machine:/home/stu# service mysql stop
root@stu-virtual-machine:/home/stu# service mysql start

3.连接数据库命令

mysql -uroot -p

root@stu-virtual-machine:/home/stu# mysql -uroot -p Enter password: (此处输入设置的密码,如果没有密码直接回车) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.25-0ubuntu0.20.04.1 (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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> quit (退出数据库客户端的命令)

四、配置文件位置

配置文件在如下位置,需要修改可以用vi打开更改

root@stu-virtual-machine:/home/stu# vi /etc/mysql/mysql.conf.d/mysqld.cnf
例如,需远程登录,则需要将bind-address改为0.0.0.0,如下
1 # Instead of skip-networking the default is now to listen only on 2 # localhost which is more compatible and is not less secure. 3 bind-address = 127.0.0.1 (改为0.0.0.0)

五、安装c/c++开发库

安装开发c/c++的库,命令:

apt install libmysqlclient-dev

stu@stu-virtual-machine:~$ sudo su [sudo] stu 的密码: root@stu-virtual-machine:/home/stu# apt install libmysqlclient-dev
测试c语言连接数据库,这里连接'test',没有自己创建,注意‘111111’是数据库密码,需要输入自己的。
#include <stdio.h> #include <mysql/mysql.h> int main() { MYSQL connect;//mysql连接对象 mysql_init(&connect); //连接到mysql if(mysql_real_connect(&connect,"localhost","root","111111","test",0,NULL,0)) { printf("连接mysql成功\n"); } else { printf("err:%s\n",mysql_error(&connect)); printf("连接mysql失败\n"); } //关闭mysql连接 mysql_close(&connect); return 0; }

编译时,需要指定-l指定mysqlclient这个库:

gcc-omysqltest mysqltest.c-lmysqlclient

stu@stu-virtual-machine:~/lg$ gcc -o mysqltest mysqltest.c -lmysqlclient stu@stu-virtual-machine:~/lg$

运行程序时要注意,有些情况使系统用管理员身份运行可以成功:

root@stu-virtual-machine:/home/stu/lg# ./mysqltest 连接mysql成功 root@stu-virtual-machine:/home/stu/lg#
但普通用户可能出现失败,此时需要更改mysql数据库管理员密码,并设置好加密方式。要注意一个是 linux系统管理员root,还有一个是Mysql数据库的管理员,名字也是root。不要混淆。
stu@stu-virtual-machine:~/lg$ ./mysqltest err:Access denied for user 'root'@'localhost' 连接mysql失败 stu@stu-virtual-machine:~/lg$

六、用户管理与授权

1.查看用户信息

mysql> select user,host,plugin from mysql.user;

2.创建用户示例

mysql> create user'stu'@'localhost'identified by'Iabc_123456';

3.创建用户指定加密方式示例

mysql> create user'stu1'@'localhost'identified WITH mysql_native_password by
'Iabc_123456';

4.更新用户密码

注意密码强度大小写数字

ALTER user'root'@'localhost'IDENTIFIED WITH mysql_native_password BY
'Iabc_123456';

5.授权用户对那些数据库的那些表可以进行操作示例

指定user_name用户可以从任意地点登录访问所有数据库的所有表

GRANT ALL PRIVILEGES ON *.* TO'user_name'@'%'identified by'密码'
GRANT ALL ON database_name.table_name TO'user_name'@'localhost'

6.删除用户

drop user'name'@'localhost';

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询