Ubuntu 18.04 安装配置 MySQL 5.7

Ubuntu 18.04 安装 mysql 的过程当中,居然没有让你输入秘密?!(以前在 Ubuntu 14.04 下,安装过程当中会询问密码),这致使安装完 mysql 初始秘密不知道的问题。python

$ sudo apt-get install mysql-server-5.7

解决方法以下:mysql

1)安装完成后,会生成文件 /etc/mysql/debian.cnf ,初始用户名和秘密以下 (这里密码是随机的)sql

$ sudo cat /etc/mysql/debian.cnf 
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = 63vIY3PtyKh10cmZ
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = 63vIY3PtyKh10cmZ
socket   = /var/run/mysqld/mysqld.sock

2)使用这个用户名和秘密登录 mysql数据库

$ mysql -h 127.0.0.1 -u debian-sys-maint -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-0ubuntu0.18.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> 

3)而后作以下操做(这里设置秘密为 1,读者请自行设置本身的密码)ubuntu

mysql> update mysql.user set authentication_string=password('1') where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> update user set plugin='mysql_native_password';
Query OK, 1 row affected (0.00 sec)
Rows matched: 4  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

4)从新启动 mysqlsocket

$ sudo service mysql restart

5)以后就可使用 root 登录了ui

$ mysql -h 127.0.0.1 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-0ubuntu0.18.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> 

6)检查默认字符编码编码

mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

  将其改成 utf8,打开 /etc/mysql/mysql.conf.d/mysqld.cnf,在最后添加一句,spa

character-set-server=utf8

  从新启动 mysql,.net

$ sudo service mysql restart

  再次查看字符编码,

mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| 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.01 sec)

 

7)使用 mycli 自动补全

 默认 mysql 命令行登录后,输入命令不会提示及自动补全,很是麻烦,经过使用 mycli 自动补全会方便不少。

安装以下,

$ sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ mycli

使用 mycli 启动数据库链接,

$ mycli -h 127.0.0.1 -u root -p ******** ( here your DB password)

 

8)  安装 mysql 的 python 借口 mysqlclient (Django 对此接口的支持比 mysql-connector-python 好)

$ sudo apt-get install default-libmysqlclient-dev
$ sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ mysqlclient

 

本文参考网友博客:http://www.javashuo.com/article/p-nxxxmvqx-ng.html

相关文章
相关标签/搜索