目录

Ubuntu安装Mysql数据库

Ubuntu安装Mysql数据库

1、首先查看Ubuntu中是否安装过mysql,使用如下命令。

dpkg -l | grep mysql

若没有安装则不会出现任何提示,如下图是安装过的。

https://i-blog.csdnimg.cn/blog_migrate/041ab047a979d4bb1b35ad8918711d0a.png

2、安装mysql-server

sudo apt install mysql-server

https://i-blog.csdnimg.cn/blog_migrate/221f7eb63f4745c29c47ebe84e1f64ee.png

3、 再次验证是否安装mysql,上面有截图

dpkg -l | grep mysql

root@Eth002:/home/caojun/FISCO/WeBASE-Node-Manager# dpkg -l | grep mysql

ii  mysql-client-5.7                       5.7.40-0ubuntu0.18.04.1                   amd64        MySQL database client binaries

ii  mysql-client-core-5.7                  5.7.40-0ubuntu0.18.04.1                   amd64        MySQL database core client binaries

ii  mysql-common                           5.8+1.0.4                                 all          MySQL database common files, e.g. /etc/mysql/my.cnf

ii  mysql-server                           5.7.40-0ubuntu0.18.04.1                   all          MySQL database server (metapackage depending on the latest version)

ii  mysql-server-5.7                       5.7.40-0ubuntu0.18.04.1                   amd64        MySQL database server binaries and system database setup

ii  mysql-server-core-5.7                  5.7.40-0ubuntu0.18.04.1                   amd64        MySQL database server binaries

该命令也可以进行验证

netstat -tap | grep mysql

https://i-blog.csdnimg.cn/blog_migrate/3c58b09740eaca328e7b259d048069fe.png

在控制台输入下面的命令进行登录

mysql -u root -p

https://i-blog.csdnimg.cn/blog_migrate/d69cb41031539c308d2018ac67c245ca.png

查看数据库

show databases;

https://i-blog.csdnimg.cn/blog_migrate/1865ce06ec63be26c711dd71b6b1ad5b.png

创建数据库,直接复制即可,创建一个node的数据库。

CREATE DATABASE IF NOT EXISTS node DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

https://i-blog.csdnimg.cn/blog_migrate/62730e528f847263c06727e150e317af.png

exit退出控制台

https://i-blog.csdnimg.cn/blog_migrate/4e68f6cf48b779a2a94f00cb768b822c.png

初始化mysql数据库,退出mysql控制台后使用如下命令

mysql_secure_installation

https://i-blog.csdnimg.cn/blog_migrate/3886af38ab944b47c5b4137599761245.png

下面会出现一些选项配置,可以按照自己需要进行配置

1、首先是安装验证插件,选择N

https://i-blog.csdnimg.cn/blog_migrate/6ad19a96da5dcab32494e3d180483ce7.png

2、设置root的密码,重复输入两次

https://i-blog.csdnimg.cn/blog_migrate/305a3d4607a1a2d65a5ca157d9ed1ec4.png

3、删除匿名账户

https://i-blog.csdnimg.cn/blog_migrate/7a2f6f87fe672ab27471cb01e9db4737.png

4、删除默认的测试数据库

https://i-blog.csdnimg.cn/blog_migrate/b98c094ed9aa23b8d7f5bb0b6656f25f.png

5、刷新授权的

https://i-blog.csdnimg.cn/blog_migrate/4d8434f89d3104e74a097bccabd69989.png

6、查看mysql运行状态

systemctl status mysq

7、配置mysql远程访问机制,注销这一行(43行),保存退出

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

https://i-blog.csdnimg.cn/blog_migrate/e0feab5842c52e54640b834b1729b350.png

在[mysqld]里面添加 skip-grant-tables

https://i-blog.csdnimg.cn/blog_migrate/0ed28d09f53d0748055521a8d4690f0a.png

8、进行mysql控制台进行刷新权限,如下命令按照顺序执行即可

mysql -u root -p
grant all on *.* to root@'%' identified by '123456' with grant option;
flush privileges;
exit
systemctl restart mysql
systemctl status mysql

9、远程连接,在另外一台电脑上面进行远程连接测试,输入mysql数据库电脑的ip及数据库3306端口、用户名、密码

https://i-blog.csdnimg.cn/blog_migrate/fd1898f91dd84bed1d5dcf57394b8419.png

这里就是新建的数据库

https://i-blog.csdnimg.cn/blog_migrate/5b29f44974cdae454fc2089ad7807da4.png

附上:

root@Eth002:/home/caojun/FISCO/WeBASE-Node-Manager/dist/script# 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: 

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

 ... 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) : 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!