Step1 MySQL数据库的安装
# 可直接使用apt安装
$ sudo apt-get install mysql
# 或者分别安装
$ sudo apt-get install mysql-server
$ sudo apt install mysql-client
$ sudo apt install libmysqlclient-dev
# 安装过程中会提示输入mysql的root账户密码,请务必记住
Step2 验证安装
sudo netstat -tap | grep mysql
Step3 设置MySQL允许远程访问
编辑mysql
配置文件:
# 该配置文件的名称及位置可能因MySQL版本而异
$ sudo vim /etc/mysql/my.cnf
···
# 找到 bind-address = 127.0.0.1 并将其注释掉
# bind-address = 127.0.0.1
Step4 重新启动MySQL
$ sudo /etc/init.d/mysql restart
Step5 重新登录MySQL
mysql -u root -p
···
mysql> grant all privileges on *.* to 'root'@'%' identified by 'xxxxxx';
mysql> flush privileges;
Step6 检查MySQL端口占用
$ netstat -nlt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
网络监听从 127.0.0.1:3306
变成 0 0.0.0.0:3306
,表示MySQL已经允许远程登陆访问。
Step7 设置字符编码为UTF-8
默认情况下,MySQL的字符集是latin1
,因此在存储中文的时候,会出现乱码的情况,所以我们需要把字符集统一改成utf8
。
$ sudo vim /etc/mysql/my.cnf
···
# 在相应节下追加下列设置
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
[mysql]
default-character-set=utf8
# 保存并退出,重新启动MySQL服务器
# 验证字符编码修改
$ mysql -u root -p
···
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.03 sec)
网友评论