当前位置:网站首页>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 -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 :
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 .
边栏推荐
- C语言 文件光标 fseek
- 【山东大学】考研初试复试资料分享
- Developer survey: rust/postgresql is the most popular, and PHP salary is low
- 案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届
- Super VRT
- 30. 串联所有单词的子串
- Three basic backup methods of mongodb
- WebView load pdf
- Invocation failed Unexpected end of file from server
- C: 反转链表
猜你喜欢
关于Qt数据库开发的一些冷知识
IDEA 报错:Process terminated【已解决】
好物推荐:移动端开发安全工具
C language 99 multiplication table
Unity——Mathf. Similarities and differences between atan and atan2
Muke 8. Service fault tolerance Sentinel
Tiktok practice ~ sharing module ~ short video download (save to photo album)
Tiktok practice ~ search page ~ video details
Detailed explanation of stored procedures in MySQL
Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005
随机推荐
515. 在每个树行中找最大值
开户可以在网上开么?能安全吗?
飞天+CIPU体为元宇宙带来更大想象空间
Unity - URP get camera stack
vue中缓存组件keep-alive
c语言简单的登录
Muke 8. Service fault tolerance Sentinel
手机股票注册开户有没有什么风险?安全吗?
tsconfig. json
C exercise. Class list plus records, display records and clear records
BOM and DOM operations
Minimum spanning tree, shortest path, topology sorting, critical path
find_ path、find_ Library memo
swagger:如何生成漂亮的静态文档说明页
Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005
Boot的单元测试
孙老师版本JDBC(2022年6月12日21:34:25)
Boot指标监测
mongoDB的三种基础备份方法
关于不等式取值转义的思路