1. 程式人生 > >ubuntu下安裝mysql及操作

ubuntu下安裝mysql及操作

開發十年,就只剩下這套架構體系了! >>>   

安裝

sudo apt-get install mysql-server mysql-client

未出現錯誤則安裝完成。

修改相關配置及root密碼

配置遠端連線

修改 /etc/mysql/mysql.conf.d/mysqld.cnf 配置檔案,找到 bind-address,修改如下:

#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
# 允許遠端連線
bind-address            = 0.0.0.0

授權 root 遠端連線,執行下列sql:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;

mysql> select host, user, authentication_string from user;
+-----------+------------------+-------------------------------------------+
| host      | user             | authentication_string                     |
+-----------+------------------+-------------------------------------------+
| localhost | root             |                                           |
| localhost | mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | debian-sys-maint | *42276B9FD61650D300927D4A0A6EFFA93BBFD11D |
| %         | root             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-----------+------------------+-------------------------------------------+
5 rows in set (0.00 sec)

修改字符集

/etc/mysql/conf.d/mysql.cnf 配置檔案,新增如下:

[mysql]
default-character-set=utf8

/etc/mysql/mysql.conf.d/mysqld.cnf 配置檔案,新增如下:

[mysqld]
character-set-server=utf8

檢視:

mysql> show variables like '%character%';
+--------------------------+----------------------------+
| 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)

修改root密碼

使用 mysqladmin

sudo mysqladmin -u root password newpassword

重啟

sudo service mysql restart

嘗試遠端連線:

➜  ~ mysql -h192.168.8.106 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.25-0ubuntu0.18.04.2 (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> \s
--------------
mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using  EditLine wrapper

Connection id:          15
Current database:
Current user:           [email protected]
SSL:                    Not in use
Current pager:          less
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.25-0ubuntu0.18.04.2 (Ubuntu)
Protocol version:       10
Connection:             192.168.8.106 via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:               3306
Uptime:                 2 hours 11 min 3 sec

Threads: 5  Questions: 818  Slow queries: 0  Opens: 314  Flush tables: 1  Open tables: 295  Queries per second avg: 0.104
--------------

到此 遠端連線字符集root密碼均配置完成,可以