检查mysql服务状态:
systemctl status mysql
查看有没有安装MySQL:
dpkg -l | grep mysql
查找合适的mysql版本:
apt-cache search mysql-server
安装MySQL:
apt install mysql-server -y
安装完成之后可以使用如下命令来检查是否安装成功:
netstat -tap | grep mysql
看到有 mysql 的socket处于 LISTEN 状态则表示安装成功
到这里已经数据库已经安装成功,并且可以使用,且没有密码
登录mysql数据库可以通过如下命令:
mysql -u root -p
-u 表示选择登陆的用户名, -p 表示登陆的用户密码,现在是mysql数据库是没有密码的,Enter password:处直接回车,就能够进入mysql数据库。
密码查看
cat /etc/mysql/debian.cnf
#数据库初始化
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: 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 #选择密码强度
Please set the password for root here.
New password: #输入新密码
Re-enter new password: #再次输入密码
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y #您想继续使用提供的密码吗?(按y | y表示是,按任何其他键表示否)
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服务状态:
systemctl status mysql
配置mysql允许远程访问
netstat -an | grep 3306
说明mysql端口目前只监听本地连接127.0.0.1。然后需要修改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
#mysql8之前版本执行下边语句
mysql> grant all on *.* to root@'%' identified by '你的密码' with grant option;
#mysql8之后版本执行下边语句
mysql>create user root@'%' identified by '你的密码';
mysql>grant all privileges on *.* to root@'%' with grant option;
#都执行刷新权限并退出
mysql> flush privileges;
mysql> quit
重启mysql
sudo service mysql restart