vlambda博客
学习文章列表

MySQL基础环境之中文字符乱码和网络远程读写

【场景】

MySQL数据库作为开源产品,常常作为Web平台开发的首选数据平台,其搭建初始时候,首先要解决:

1,中文字符不能乱码;

2,局域网联网通畅读写;


【方案】

一,MySQL中文乱码

1)登录远程数据库,管理员身份打开命令行窗口,输入:mysql -h10.100.3.138 -u root -p

2)输入命令查看编码:show variables like 'character%';

3)更新相应的'character%'设置,建议使用utf-8;

     比如:set global character_set_server=utf8;

4重启MySQL服务:注意不用分号结束,否则服务名不匹配

     net start MySQL

     net stop MySQL

5)创建表字段验证:create table user(name varchar(11)) default charset=utf8;

 

二,MySQL远程访问授权

1. 命令行数据法:

1MySQL Command Line创建数据库:mysql -h x.x.x.x -u root -p

2)创建数据库picm并导入库表脚本:

    create database if not exists picm default charset utf8;

    source /home/ftp/picm.sql;

3)远程用户访问授权:

    grant all privileges on [database|*].[table|*] to 'username'@'%' identified by 'password' with grant option;  

    flush privileges; 

    其中,%表示任何主机;192.168.1.%表示限制主机

*.*表示全部数据库和全部数据表;

4)退出:exit;

 

2. 修改表数据法:

1)登录远程数据库,管理员身份打开命令行窗口,输入:mysql -h10.100.3.138 -u root -p

2)切换数据库表空间且查看用户表:use mysql;  select host, user from user;

3)如果不是修改root账号的权限,则首先新建或修改用户表:

    update mysql.user set host = '%' where user = 'root';

低版本:insert into mysql.user(Host,User,Password)  values("%","root",password("123456"));

高版本:update user set host='%',  authentication_string=password("123456"),

                  plugin='mysql_native_password' where user='root';

4)用户权限刷新

    flush privileges;

5)退出:exit;


【总结】

MySQL的版本会对问题的解决过程中的某些操作造成不一样本文的方案均是基于MySQL5.1MySQL5.7作过必要的验证,因此,对各位的实际情况可能仅具有参考价值。

1,中文乱码:其本质是字符集的问题;

2,远程访问:其本质是用户权限问题;