一、 MYSQL配置:
- 下载对应的mysql:downloads.mysql.com/archives/
- 环境变量→系统变量→Path,新建一个环境变量,添加mysql的bin文件目录。例如:D:\mysql-8.0.18winx64\bin
- 以管理员权限打开cmd,输入:①cd D:\mysql-8.0.18winx64\bin(cd 空格 mysql的bin路径)②mysqld -install ③mysqld --initialize ④ net start mysql(显示MySQL服务正在启动... MySQL服务已经启动成功)
- 输入 mysql -u root -p,然后输入密码进入数据库(如果输入密码时无法直接回车键进去并忘记默认的密码,提示ERROR 1405(28000),则参考blog.csdn.net/uniquewonderq/article/details/95077299)
二、Navicat连接MYSQL:
文件→新建连接→MySQL,连接名随意;主机、端口、用户名、密码与所要连接的数据库要匹配
三、UNITY简易账号密码登陆
using MySql.Data.MySqlClient;
using System;
using System.Data;
using UnityEngine;
public class LoginController : MonoBehaviour
{
private static MySqlConnection sqlConnection;
private static string connStr = "Database=mysql_test;Data Source=127.0.0.1;User Id=root;Password=root;port=3306";
//连接数据库
public static MySqlConnection MysqlConn()
{
MySqlConnection connection = null;
connection = new MySqlConnection(connStr);
return connection;
}
//在DataSet中尝试匹配玩家输入的账号密码
public bool MatchAccAndPassw(string sqlTable, string acc, string passw)
{
string query = "SELECT * FROM " + sqlTable;
DataSet tableSet = QueryAcc(query);
DataTable table = tableSet.Tables[0];
if (table != null)
{
for (int i = 0; i < table.Rows.Count; i++)
{
Debug.Log("账号:" + table.Rows[i][0].ToString() + ",密码:" + table.Rows[i][1].ToString());
if (table.Rows[i][0].ToString() == acc)
{
if (table.Rows[i][1].ToString() == passw)
{
Debug.Log("登陆成功");
return true;
}
else
{
Debug.LogError("密码不正确,请重试");
return false;
}
}
else
{
Debug.LogError("账号不存在,请重试");
return false;
}
}
}
return false;
}
//将查询到的数据存到DataSet并返回
//sqlStr:数据库查询指令
public DataSet QueryAcc(string sqlStr)
{
using (var connection = MysqlConn())
{
DataSet ds = new DataSet();
try
{
connection.Open();
MySqlDataAdapter dataAda = new MySqlDataAdapter(sqlStr, connection);
dataAda.Fill(ds);
}
catch (Exception e)
{
connection.Close();
throw new Exception(e.Message);
}
finally
{
connection.Close();
}
return ds;
}
}
}
***报错MySQL Client does not support authentication:
管理员权限打开cmd,进入mysql:
//alter user '用户名'@'主机' identified with mysql_native_password by '密码';
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456';
mysql> flush privileges;