当前位置:网站首页>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 :

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 -hlocalhostIn 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 :

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 :

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 :

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”:

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:

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 .
边栏推荐
- Swagger: how to generate beautiful static document description pages
- The successfully resolved idea cannot use the log normally after referencing Lombok's @slf4j
- 关于不等式取值转义的思路
- 云计算技术的发展与芯片处理器的关系
- On the origin of the dispute between the tradition and the future of database -- AWS series column
- JWT操作工具类分享
- C language simple login
- Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
- 郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
- 0 basic C language (2)
猜你喜欢
随机推荐
Minimum spanning tree, shortest path, topology sorting, critical path
mysql的充值问题
0 basic C language (2)
[MySQL series] collection of common working SQL (continuous update)
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data
710. 黑名单中的随机数
Separate save file for debug symbols after strip
Detailed explanation of shutter textfield
C# 练习。类列表加记录,显示记录和清空记录
关于不等式取值转义的思路
开户可以在网上开么?能安全吗?
Tiktok practice ~ sharing module ~ short video download (save to photo album)
Boot指标监测
[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)
2022/02/14 line generation
股票开户的具体步骤是什么?网上开户安全吗?
0基础学c语言(3)
Good thing recommendation: mobile terminal development security tool
What are the specific steps for opening a stock account? Is it safe to open an account online?
Uni app uses canvas to draw QR code







