当前位置:网站首页>Establish a connection with MySQL

Establish a connection with MySQL

2022-06-26 20:42:00 Dream love night star

1. Command line connection MySQL

It was introduced in Windows Install the latest version of MySQL Initialize installation steps , start-up MySQL service , The recorded initial password can be used for “root” Log in to the account to perform relevant operations ,Windows and Linux The command line operation steps are the same :


 And MySQL Establishing a connection

The picture above shows MySQL Of Initial password . stay Windows Open down CMD window , Use the following command Sign in MySQL

  command  : mysql -uroot -p -P3306 -hlocalhost

In the command “-u” The following indicates the login user name ,"-p" Indicates that the password is entered separately ,"-P" Represents the port number ,"-h" Represents the host address , The following figure shows that you need to enter a password , You can right-click and paste the initial password recorded before and enter , The following figure shows that the login is successful :


 And MySQL Establishing a connection

After the first successful login with the initial password, you must Change the initial password , If you do not change the initial password , It will prompt the error message :


 And MySQL Establishing a connection

The command to modify the initial password is as follows :

  command  : ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

As shown in the figure below , It means to modify root Initial password succeeded :


 And MySQL Establishing a connection

2. Windows Client tool connection MySQL

Used in this section Windows The connection tool is Navicat12, Navicat12 It's a commercial fee version , You can choose to try 30 God , It should be enough to learn and use ,

stay Windows You can also use MySQL Login tool pair MySQL Conduct management , Use here Navicat Connect as shown in the figure , Click on the top left corner " Connect ", choice “MySQL”:


 And MySQL Establishing a connection

Fill in the corresponding input field as shown in the figure above , You can click on the " Connect the test ", The following figure shows that the connection is successful , choice “ determine ” Can connect MySQL:


 And MySQL Establishing a connection

3. Use the back-end language to connect MySQL

3.1 Use PHP7 Connect MySQL

<?php$serve = 'localhost:3306';// The host address : Port number $username = 'root'; // Connect  MySQL  The account of $password = '123456'; // Connect  MySQL  Password $dbname = 'dbname'; // Database name $link = mysqli_connect($serve,$username,$password,$dbname);mysqli_set_charset($link,'UTF-8'); //  Set database character set $result = mysqli_query($link,'select * from customers');$data = mysqli_fetch_all($result); //  Get all the data from the result set print_r($data); // Print query result set mysqli_close(); // Close the previously opened database connection 

3.2 Use Python Connect MySQL

import pymysqldb = pymysql.connect(" database ip"," user "," password "," database " ) #  Open database connection cursor.execute("SELECT VERSION()") #  Use  execute()  Method execution  SQL  Inquire about data = cursor.fetchone() #  Use  fetchone()  Method to get a single piece of data print ("Database version : %s " % data)db.close()

3.3 Use Java Connect MySQL

Java Connect MySQL There are five steps :

//  register JDBC drive Class.forName(JDBC_DRIVER);//  Open the link Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);//  Execute the query Statement stmt = conn.createStatement();String sql = "SELECT sno, sname FROM student";ResultSet rs = stmt.executeQuery(sql);//  Expand the result set database while(rs.next()){//  Retrieve... By fields String no = rs.getString("sno");String name = rs.getString("sname");//  Output data System.out.println("no: " + no + ", name: " + name);//  Close... When done rs.close();stmt.close();conn.close();

4. Summary

This section mainly helps you to familiarize yourself with how to communicate with... Through the client MySQL Establishing a connection , In the actual production environment , These kinds of connection methods will be used , It should be noted that all the above are connected locally , After the official project release , The database of a project is usually built on a remote server , You need to use an account with remote permission to establish a connection , Otherwise, the connection will fail , The following sections introduce MySQL The remote account permissions will be introduced in detail when you learn about permissions .

原网站

版权声明
本文为[Dream love night star]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262029206330.html