Your one-stop destination to share, collaborate and learn about the latest innovations in open source software for s390x processor architecture (IBM Z and LinuxONE)
For the purpose of this blog, we have taken an example of the .NET 7 application that connects to MySQL server running on the same local host. However, this sample application can be modified to connect to a remote server as well. This application has been verified on an IBM Z LPAR running Ubuntu 20.04 LTS.
There were crashes and Big-endian issues while establishing the connection. These issues are fixed, and the fixes are up streamed.
Install the MySQL server using the apt software package manager.
# Install MySQL Server $ sudo apt install -y mysql-server
Start the MySQL service.
# Start MySQL service $ systemctl start mysql.service # Enable MySQL service to start at boot $ systemctl enable mysql.service
Create a database for testing purpose using the command line client.
# Invoke mysql client with the root user $ mysql -u root # Create database mysql>$ CREATE DATABASE <Your Database Name>; # Switch to newly created database mysql>$ USE <YourDatabaseName>; # Create table in database with different columns mysql>$ CREATE TABLE <YourTableName> ( <ColoumnName1> <datatype1>, <ColoumnName2> <datatype2>, ..); # Insert some dummy data into table mysql>$ INSERT INTO <YourTableName> VALUES (<DummyData1>, DummyData1>, ...); # View table mysql>$ SELECT * FROM <YourTableName> ; #exit from MariaDB client prompt mysql>$ quit
The output for the test database and table that you created in step 3 should look as follows.
$ mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1059 Server version: 8.0.33-0ubuntu0.20.04.2 (Ubuntu) Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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> CREATE DATABASE TestDB; Query OK, 1 row affected (0.000 sec) mysql> USE TestDB; Database changed mysql> CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) ); Query OK, 0 rows affected (0.003 sec) mysql> INSERT INTO Persons VALUES (102, 'LMN', 'PQR', 'abc road', 'xyz city'); Query OK, 1 row affected (0.001 sec) mysql> INSERT INTO Persons VALUES (101, 'ABC', 'DEF', 'xyz road', 'Pune'); Query OK, 1 row affected (0.001 sec) mysql> SELECT * FROM Persons ; +----------+----------+-----------+----------+----------+ | PersonID | LastName | FirstName | Address | City | +----------+----------+-----------+----------+----------+ | 102 | LMN | PQR | abc road | xyz city | | 101 | ABC | DEF | xyz road | Pune | +----------+----------+-----------+----------+----------+ 2 rows in set (0.000 sec) mysql> quit Bye
The Pull Request to add support for s390x machine is not merged to the main GitHub project of the MySql.Data connector. To build the connector to work with s390x the connector needs to be built locally with the PR changes.
Clone the MySql.Data project from GitHub and checkout to the PR
$ git clone https://github.com/mysql/mysql-connector-net.git $ git fetch origin pull/54/head:BigEndianFix $ git checkout BigEndianFix
Build the package using dotnet build
$ cd mysql-connector-net/MySQL.Data/ $ dotnet build
The build should produce a few warnings and build the Mysql.Data and the various package test dlls.
MySql.Data -> /home/.../mysql-connector-net/MySQL.Data/src/bin/Debug/net462/MySql.Data.dll MySql.Data -> /home/.../mysql-connector-net/MySQL.Data/src/bin/Debug/net48/MySql.Data.dll 8.0 * BigEndianFix MySql.Data -> /home/.../mysql-connector-net/MySQL.Data/src/bin/Debug/netstandard2.0/MySql.Data.dll 8.0 * BigEndianFix MySql.Data -> /home/.../mysql-connector-net/MySQL.Data/src/bin/Debug/netstandard2.1/MySql.Data.dll 8.0 * BigEndianFix MySql.Data -> /home/.../mysql-connector-net/MySQL.Data/src/bin/Debug/net6.0/MySql.Data.dll 8.0 * BigEndianFix MySql.Data -> /home/.../mysql-connector-net/MySQL.Data/src/bin/Debug/net7.0/MySql.Data.dll 8.0 * BigEndianFix
Install cross built .NET with support for s390x architecture and verify the installation by:
$ dotnet --info # Should produce an output similar to the following .NET SDK: Version: 7.0.100-rc.2.22477.23 Commit: 0a5360315a Runtime Environment: OS Name: ubuntu OS Version: 20.04 OS Platform: Linux RID: linux-s390x Base Path: /home/.../Dotnetv2/sdk/7.0.100-rc.2.22477.23/ Host: Version: 7.0.0-rc.2.22472.3 Architecture: s390x Commit: 550605cc93 .NET SDKs installed: 7.0.100-rc.2.22477.23 [/home/.../Dotnetv2/sdk] .NET runtimes installed: Microsoft.AspNetCore.App 7.0.0-rc.2.22476.2 [/home/.../Dotnetv2/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 7.0.0-rc.2.22472.3 [/home/.../Dotnetv2/shared/Microsoft.NETCore.App] Other architectures found: None Environment variables: DOTNET_ROOT [/home/.../Dotnetv2]
Complete the following steps to create a .NET 7 console application:
Create a console application using the dotnet new command.
$ dotnet new console -o MySqlADO The template "Console App" was created successfully. Processing post-creation actions... Restoring /.../MySqlADO/MySqlADO.csproj: Determining projects to restore... Restored /.../MySqlADO/MySqlADO.csproj: (in 137 ms). Restore succeeded. $ cd MySqlADO
If the MySql.Data package has been locally built proceed with the following steps:
Add package reference to the csproj of the dotnet application with the path to the build dll as the HintPath
<ItemGroup> <Reference Include="MySql.Data"> <HintPath>/.../mysql-connector-net/MySQL.Data/src/bin/Debug/net7.0/MySql.Data.dll</HintPath> </Reference> </ItemGroup>
Add two additional packages needed by the package.
$ dotnet add package System.Configuration.ConfigurationManager $ dotnet add package System.Security.permissions
In the future, after the PR has been merged, the previous steps can be replaced with a dotnet add package to add the latest nuget MySql.Data package.
$ dotnet add package MySql.Data
Copy the following code in the Program.cs file. This sample application is used to connect to the YourTestDatabase on the local MySql server and to read data from all rows and columns from YourTestTable, and to display the same on console output.
using System; using MySql.Data; using MySql.Data.MySqlClient; namespace ConsoleApplication { class Program { static void PrintTable(MySqlConnection Connection, string tableName) { string Query = $"SELECT * FROM {tableName}"; MySqlCommand Command = new MySqlCommand(Query, Connection); MySqlDataReader Reader = Command.ExecuteReader(); int fCount = Reader.FieldCount; Console.Write(":"); for (int i = 0; i < fCount; i++) { String fName = Reader.GetName(i); Console.Write(fName + ":"); } Console.WriteLine(); while(Reader.Read()) { Console.Write(":"); for(int i = 0; i < fCount; i++) { String col = Reader.GetString(i); Console.Write(col + ":"); } Console.WriteLine(); } Reader.Close(); Command.Dispose(); } static void Main(string[] args){ string MyConString = "SERVER=localhost; " + "Database=TestDB; " + "Uid=root;" + "Pwd=;"; MySqlConnection Connection = new MySqlConnection(MyConString); Connection.Open(); Console.WriteLine("Writing to the Database"); string inQuery = "INSERT INTO Persons VALUES (103, \"ABC\", \"DEF\", \"xyz road\", \"pqr city\")"; MySqlCommand CommandIn = new MySqlCommand(inQuery, Connection); CommandIn.ExecuteNonQuery(); CommandIn.Dispose(); PrintTable(Connection, "Persons"); Console.WriteLine(); Console.WriteLine("Updating in the Database"); string updateQuery = "UPDATE Persons SET LastName='Potato' WHERE PersonID=103"; MySqlCommand CommandUpdate = new MySqlCommand(updateQuery, Connection); CommandUpdate.ExecuteNonQuery(); CommandUpdate.Dispose(); PrintTable(Connection, "Persons"); Console.WriteLine(); Console.WriteLine("Deleteing from the database"); string deleteQuery = "DELETE FROM Persons Where PersonID=103"; MySqlCommand CommandDelete = new MySqlCommand(deleteQuery, Connection); CommandDelete.ExecuteNonQuery(); CommandDelete.Dispose(); PrintTable(Connection, "Persons"); Console.WriteLine(); Connection.Close(); } } }
Build and run the project using the dotnet build and dotnet run commands.
$ dotnet build $ dotnet run
which should produce the output:
Writing to the Database :PersonID:LastName:FirstName:Address:City: :102:PQR:XYZ:abc road:def city: :101:LMN:ABC:def road:xyz city: :103:ABC:DEF:xyz road:pqr city: Updating in the Database :PersonID:LastName:FirstName:Address:City: :102:PQR:XYZ:abc road:def city: :101:LMN:ABC:def road:xyz city: :103:Potato:DEF:xyz road:pqr city: Deleting from the database :PersonID:LastName:FirstName:Address:City: :102:PQR:XYZ:abc road:def city: :101:LMN:ABC:def road:xyz city:
Conclusion
The blog demonstrated how to connect to MySql Database Server through the ADO.Net connector on an IBM Z machine.
References
Introduction to MySQL connector
Using MySQL
MySql.Data Nuget Package
Source Repository mysql-connector-net
MySQL Connector .NET Developer Guide
Copy