1. Install MySQL
Download the MySQL installation package link: https://dev.mysql.com/downloads/ , as shown in the figure, clickMySQL Community Downloads
Continue to select MySQL Community Server, as shown in the figure
Select the version to be downloaded, select the first one for Download, as shown in the figure:
Click No thanks on this page, just start my download
After the download is complete, unzip it directly, write the MySQL configuration file, and create a new my.ini file, as shown in the figure:
The content of the file is as follows:
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录 ----------Mysql解压之后的文件路径-------------
basedir=D:\mysql-8.0.34-winx64
# 设置mysql数据库的数据的存放目录 ---------文件夹自行创建
datadir=D:\mysql-8.0.34-winx64\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。
max_connect_errors=10
# 服务端使用的字符集默认为utf8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
#mysql_native_password
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8
Note that # 设置mysql数据库的数据的存放目录 ---------文件夹自行创建 datadir=D:\mysql-8.0.34-winx64\data
it is best to create a new data folder in the decompression directory, otherwise you may not be able to enter mysql later
Open CMD as an administrator and enter the bin directory of mysql, as shown in the figure:
Excuting an order
mysqld --initialize --console
As shown in the figure, the initial password is different for each person, and the position is marked with a yellow mark in the figure
Install mysql service
mysqld --install mysql
then start
net start mysql
as the picture shows:
input the command
mysql -uroot -p
Then enter the initial password:
Change login password
ALTER USER 'root'@'localhost' IDENTIFIED BY '666666';
change the password to666666
Exit mysql, enter
exit
To stop the mysql service, enter
net stop mysql
Configure environment variables, as shown in the figure:
New in system variables
变量名:MYSQL_HOME
变量值:MySQL的解压目录
as the picture shows:
Then find PATH in the system variable, add
%MYSQL_HOME%\bin
as the picture shows:
2. DBeaver installation (not required)
DBeaver is a database connection tool, free, cross-platform
Download URL: https://dbeaver.io/download/
After decompression or installation directly, as shown in the figure:
Open the software, as shown in the figure:
Select MySQL, as shown in the figure:
Enter the password, just set666666
Download the driver file automatically, as shown in the figure:
start mysql
The connection is successful, as shown in the figure:
Can operate on the database:
3. C# WPF connects to MySQL
Create a project, as shown in the figure:
Create a WPF application:
Create project:
Design interface:
Place controls:
Automatically install the required dll package, as shown in the figure:
After the installation is complete, the error message disappears:
Connect to MySQL, as shown in the figure:
Run the project:
Click the button and the connection is successful:
The test code is as follows:
using MySql.Data.MySqlClient;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace FM_NVM_App
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Process p = Process.Start("E:\\software\\FM_FIG-Trans_V1.0\\dist\\FM_FIG-Trans_GUI\\FM_FIG-Trans_GUI.exe");
p.WaitForExit();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
String strConn = "Database = sys;Server = localhost;Port = 3306;Password = 666666;UserID = root;charset = utf8";
MySqlConnection conn = new MySqlConnection(strConn);
MySqlCommand comm = new MySqlCommand();
comm.Connection = conn;
try
{
conn.Open();
txtbox.Text = strConn;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
}
I hope this article is helpful to everyone. If there is anything wrong with the above, please correct me.
Sharing determines the height, and learning widens the gap