[转]ubuntu16.04安装mysql5.7

2020/12/22 14:33
阅读数 11

转自:How to install mysql server 5.7 on Ubuntu 16.04 LTS ( Xenial Xerus )

本教程将介绍如何在Ubuntu Linux 16.04 LTS(Xenial Xerus)上安装,配置和管理MySQL。MySQL数据库服务器的最新版本是5.7,可以使用apt-get命令apt命令安装

  1. mysql-server – Metapackage取决于最新版本(服务器)

  2. mysql-client – Metapackage取决于最新版本(客户端)

步骤1:通过键入以下命令来更新系统:

 sudo apt update  
 sudo apt upgrade

开始安装之前先更新服务器

步骤2:在Ubuntu 16.04上安装mysql版本5.7

键入以下命令:

sudo apt install mysql-server mysql-client

示例输出:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  libterm-readkey-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common
  mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl
  libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
  libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  libterm-readkey-perl mysql-client mysql-client-5.7 mysql-client-core-5.7
  mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded.
Need to get 21.7 MB of archives.
After this operation, 155 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

您需要输入MySQL root用户的密码: 并确认密码 请记住这个密码

步骤3:运行mysql_secure_installation以确保安装安全(这一步不是必须要的,根据自己的实际情况选择YorN)

对于所有生产服务器,必须运行以下命令:

sudo mysql_secure_installation

示例输出:

mysql_secure_installation命令将更改root密码,从MySQL服务器禁用和删除匿名用户,关闭MySQL root用户的远程登录,删除测试数据库,并重新加载系统上的特权表。如果您已完成所有步骤,然后对这些问题回答“是”。现在,您的MySQL安装应该是安全的。

步骤4:如何使用MySQL服务器?

要以root用户身份登录,请键入:

mysql -u root -p

在出现提示时,输入MySQL root密码,您应该看到mysql>如下提示:

步骤5:如何创建新的MySQL服务器数据库和用户帐户?

sql语法如下,以创建新的mysql数据库和设置密码:

CREATE  DATABASE数据库-名称-此处;
GRANT  ALL  ON DATABASE -姓名- HERE *  TO  '数据库USERNAME-HERE'  IDENTIFIED  BY  '数据库密码-HERE' ;

例如,创建一个wpblogdb作为数据库,创建vivek作为用户,并创建fooBarPwd8-4_2作为用户的密码,运行(在mysql>提示符下键入:) 示例会话:

create database auth;

grant all on auth.* to 'aim' identified by '2828286';

quit;

现在,来使用新的用户重新登录.

mysql -u USERNAME -p DB-NAME-HERE
mysql -u aim -p auth

当然,您也可以创建mysql表并插入数据: 示例输出:

 CREATE TABLE authors (id INT, name VARCHAR(20), email VARCHAR(20));
 INSERT INTO authors (id,name,email) VALUES(1,"Vivek","xuz@foo.gmail.com");
 INSERT INTO authors (id,name,email) VALUES(2,"Wendy","bar@foo.gmail.com");
 INSERT INTO authors (id,name,email) VALUES(3,"Tom","tom@foo.gmail.com");
 SELECT * FROM authors;
 quit;

如何启动MySQL服务器?

sudo systemctl start mysql

或者

sudo systemctl start mysql.service

如何停止MySQL服务器?

sudo systemctl stop mysql

或者

sudo systemctl stop mysql.service

如何确定MySQL是否正在运行/活动?

sudo systemctl status mysql.service

示例输出:

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enab
   Active: active (running) since Mon 2016-03-28 14:20:54 CDT; 8s ago
  Process: 24181 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exi
  Process: 24176 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exite
 Main PID: 24180 (mysqld_safe)
    Tasks: 23 (limit: 512)
   Memory: 106.1M
      CPU: 514ms
   CGroup: /system.slice/mysql.service
           ├─24180 /bin/sh /usr/bin/mysqld_safe
           └─24528 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plug

Mar 28 14:20:53 xenial systemd[1]: Starting MySQL Community Server...
Mar 28 14:20:53 xenial mysqld_safe[24180]: 160328 14:20:53 mysqld_safe Can't log t
Mar 28 14:20:53 xenial mysqld_safe[24180]: 160328 14:20:53 mysqld_safe Logging to 
Mar 28 14:20:53 xenial mysqld_safe[24180]: 160328 14:20:53 mysqld_safe Starting my
Mar 28 14:20:54 xenial systemd[1]: Started MySQL Community Server.

如何重设mysql根帐户密码?

如果您想更改MySQL根密码,则需要键入以下命令:

sudo dpkg-reconfigure mysql-server-5.7

关于MySQL服务器配置的注释

您可以编辑/etc/mysql/my.cnf文件以配置基本设置,例如TCP / IP端口,IP地址绑定和其他选项。但是,Ubuntu 16.04 LTS上的MySQL数据库服务器配置文件位于,/etc/mysql/mysql.conf.d/mysqld.cnf您可以使用文本编辑器(如vi或nano)进行编辑:

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

或者

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

在更改 /etc/mysql/mysql.conf.d/mysqld.cnf MySQL服务器之后,需要重新启动:

sudo systemctl restart mysql.service
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部