【Ubuntu】在Ubuntu server中安装mysql

在ubuntu系统中安装mysql数据库,相对简单一些,主要的步骤如下:

1. 先查看是否有mysql安装,执行如下命令:

dpkg -l | grep mysql

如果没有安装mysql则不会有内容显示。
2. 执行mysql 安装的shell命令:

sudo apt-get install mysql-server
# 如果是root用户登录则可以省略掉sudo

等待执行完毕即可。
3. 执行:

netstat -tap | grep mysql

如果看到有 mysql 的socket处于 LISTEN 状态则表示安装成功。
在这里插入图片描述
此时,再次执行" dpkg -l | grep mysql"语句,会看到如下内容:
在这里插入图片描述

此时虽然成功安装了mysql,但是此时的root账户是没有密码的,接下里就要为mysql进行初始化的操作。

4. 对mysql进行初始化的操作,执行如下shell命令:

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:			#输入要为root管理员设置的数据库密码

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) : 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	#是否禁止root管理员从远程登录

 ... 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		#删除test数据库并取消对它的访问权限
 - 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的初始化安全配置。

5. 设置mysql可以进行远程访问,首先编辑 /etc/mysql/mysql.conf.d/mysqld.cnf 配置文件:

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

在打开的文件中注释掉:bind-address = 127.0.0.1 ,然后保存退出文件。

进入mysql数据库进行如下配置:

mysql -u root -p     		#登录mysql数据库
mysql> grant all privileges on *.* to root@'%' identified by 'root用户的密码' with grant option;  	#给名称为root的账户设置远程访问的权限
mysql> flush privileges; 	# 刷新权限
mysql> exit

mysql 给用户赋予权限:
mysql> grant 权限1, 权限2, …权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’;
用户地址为"%",表示可以为任意地址;
数据库名称.表名称 用*.* 表示可以操作所有的数据库,所有的数据表。

6. 重启mysql服务器

	systemctl status mysql    #查看mysql服务器的状态
	service mysql stop/start/restart        #停止/启动/重启mysql服务器
发布了66 篇原创文章 · 获赞 6 · 访问量 9398

猜你喜欢

转载自blog.csdn.net/qgnczmnmn/article/details/103923188