当前位置:网站首页>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 .
边栏推荐
- StringUtils判断字符串是否为空
- Boot指标监测
- 515. 在每个树行中找最大值
- Muke 8. Service fault tolerance Sentinel
- 30. 串联所有单词的子串
- Six necessary threat tracking tools for threat hunters
- Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -
- C: 反转链表
- [recommended collection] these 8 common missing value filling skills must be mastered
- Keep alive cache component in Vue
猜你喜欢

黑客用机器学习发动攻击的九种方法

Muke 8. Service fault tolerance Sentinel

Unit test of boot

Boot的单元测试

On the escape of inequality value

Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005

Detailed explanation of shutter textfield

windows系统下怎么安装mysql8.0数据库?(图文教程)

威胁猎人必备的六个威胁追踪工具

慕课11、微服务的用户认证与授权
随机推荐
【贝叶斯分类3】半朴素贝叶斯分类器
SentinelResource注解详解
JWT operation tool class sharing
[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)
Swagger: how to generate beautiful static document description pages
Super VRT
0 basic C language (1)
Is it safe to open a securities account? Is there any danger
郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
Review of watermelon book (VII): Bayesian classifier (manual push + code demo)
网上开户万一免五到底安不安全?
0基础c语言(0)
Gee: calculate the maximum and minimum values of pixels in the image area
[most detailed] the latest and complete redis interview (70)
Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading
【最详细】最新最全Redis面试大全(42道)
Boot indicator monitoring
GEE:计算image区域内像素最大最小值
Flutter TextField详解
swagger:如何生成漂亮的静态文档说明页