ubuntu16.04安装mysql数据库

  •   安装
sudo apt-get install mysql-server(安装过程中按提示设置root密码)
sudo apt-get install mysql-client
sudo apt-get install libmysqlclient-dev
  查看数据库是否安装成功:
sudo netstat -tap | grep mysql
  一般情况下安装好数据库就自己运行了。手动运行、停止、重启数据库命令:
service mysql start
service mysql restart
service mysql stop
  • ubuntu命令行操作数据库
    • 登录mysql数据库

(base) bryant@VM:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 119
Server version: 5.7.27-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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>

  按提示输入root密码进入mysql命令行。
    • 退出mysql命令行

mysql> quit
Bye

或者在mysl命令行按CTR+D退出到shell界面

    • 显示数据库列表

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| permdb |
| stocks |
| sys |
| test |
+--------------------+
7 rows in set (0.00 sec)

    • 选中数据库

mysql> use permdb ;

Database changed

 
    • 显示数据库中的所有表

mysql> show tables;
+--------------------------------+
| Tables_in_permdb |
+--------------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| guardian_groupobjectpermission |
| guardian_userobjectpermission |
| perm_task |
+--------------------------------+
13 rows in set (0.00 sec)

    • help查看可用命令

mysql> help

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
...

 
  •  让MySQL服务器被远程访问
  打开mysql配置文件
  注意:不同 mysql 版本此配置文件位置和名字可能不同(5.7.26的配置文件:/etc/mysql/mysql.conf.d/mysqld.cnf)
sudo vim /etc/mysql/my.cnf
  找到将bind-address = 127.0.0.1注释
  #bind-address = 127.0.0.1
  修改后,重启MySQL服务器
     
  • 将字符编码设置为UTF-8
  默认情况下,MySQL的字符集是latin1,因此在存储中文的时候,会出现乱码的情况,所以我们需要把字符集统一改成UTF-8。
  a)打开mysql配置文件:
sudo vim /etc/mysql/my.cnf
  b)在[client]下追加:

default-character-set=utf8
  c)在[mysqld]下追加:
character-set-server=utf8
  d)在[mysql]下追加:
default-character-set=utf8
  修改后,重启MySQL服务器。登录mysql查看字符串编码:
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
  • 设置utf8字符的另一种方法
  进去mysql后,
set character_set_database=utf8;
set character_set_server=utf8;
  然后重启服务器。
 
 
 
 
 
 
 
 
posted @ 2019-09-02 01:49  BRYANT_2019  阅读(329)  评论(0编辑  收藏  举报