How to Make the Initial Connection to MySQL (From Various Access Points)
1. Making the connection
Now that you have installed the MySQL Server and Workbench, you should find the following MySQL products accessible from your Windows machine: the Workbench, the Command Line - Unicode, and Command Line. Often, you'll find online how-to articles with instructions that you need to input into a MySQL Shell. But where can you find this tool?
Do you need the MySQL Shell?
Well, no. It is not a critical component at this stage. The Shell from MySQL is a separate release that bridges the non-SQL (or NoSQL) and the SQL words. Some parts of MySQL manual from their website guide you to start from the shell command line, but mainly only to connect to the server.
You already can connect to the MySQL server with your Windows machine! And without installing the additional Shell.. If you haven't yet installed MySQL or is considering to, these hubs can help:
Connect from the command prompt
Within Windows, you can just go to the command prompt, which is a Windows tool that you access by typing "cmd" onto the search box. From there, you will be able to connect to MySQL, and the rest is command-line client queries--the things you would input if you were using the MySQL command-line in the first place.
The command prompt is also known by other false names, such as the MS-DOS or the DOS prompt, which it certainly is not. The names may have originated from the command interpreters used for older Windows version, circa XP.
Alternative location route
Another way that you can access the prompt is by typing onto your Run command the location "C:\Windows\system32\cmd.exe", which will open the same window you would see if you typed in "cmd" into the Windows search box. Remember, the primary use of the command prompt is to do certain Windows operating tasks. To access MySQL from the prompt, we'll use the "mysql" command.
MySQL not recognized?
A common type of error you could find is the "'MySQL' is not recognized as internal or external command, operable program or batch file'".
Get the path, avoid such errors
Just to be sure that we eliminate any chances of getting the dreaded "MySQL not recognized" error, it's a good idea to check on your installation:
- Go to "This PC" or type in "PC" into the Windows search box, and then click right on the desktop app icon and then select "Properties"
- Click on “Advanced system settings” > “Environment Variables” and look at “System variables”.
- Scroll down to one of the variables that says “Path”. Click on the value, and then click “Edit”. Among the variables you need to specify MySQL installation path.
- The installation path will most likely be on the C: folder > Program Files > MySQL > MySQL Server > bin. Copy the address using the “Copy address as text” option.
- Back on the variable, click on “New”, and then paste the copied address, and then click OK (each time you need to), and navigate back to the command prompt.
Connect from MySQL command line
Finally, you can easily access your MySQL Command Line Client and enter your root password, and then work from there. However, once you are within the MySQL command line client, you cannot switch users. To be able to log-in as a new user (with the privileges you have already specified), you need to access the program from the command prompt and specify your username.
2. Creating a new user (for tests)
In my previous articles on MySQL installation, I have provided a step-by-step instruction on how you can set the root password and create a new user during the custom setup installation. These user administration tasks can also be done within the MySQL command-line client, where you create users with custom restrictions, as needed.
Now let's create a new user with some access to our test database. The first thing we want to do is access the command line using our root account and create the user from there. But before all that, why don't we check out the list of all of the available users?
SELECT user FROM mysql.user; SELECT user, host FROM mysql.user;
Name your new user
To create a new user from the command line, you need to specify it a username and a secure password. We will be providing this user with all the privileges at first, and then log in to try out the connection with the test database. After the test, we’ll delete the user.
I'm going to name my new user "espresso_gal" and assign her the password "password" just for the purpose of this connection test:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Grant some privileges
Now we have successfully created a user, but without privileges, so espresso_gal won’t be able to do anything with our databases yet. We gave her the common ‘localhost’ for the host; it actually refers to this computer, or My Computer.
Next, we specify the privileges to our test user. We want her to be able to read, edit, and perform all the necessary actions to all of our databases. So our command line would look like:
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
Note: You SHOULD NOT grant all privileges to every new user you create. It is more practical to make sure that your users are provided only the required privileges for them, although you can change this later on. Now that's a wrap for our espresso_gal. We are going to finalize her privileges and let the changes take effect.
3. Setting up a test database
To have a go at a near real-life scenario of a remote access from a specific user, we are going to create a test database from the MySQL Workbench. Our reference is the test database detailed in a Make Use Of guide for MySQL installation.
- First, launch the Workbench application and navigate to the connection you have created during the installation with the root account.
- From the workbench, create a new schema by clicking right on the blank part of the Navigator box that says "SCHEMAS" on the top-left of the dashboard. From the selection, click "Create Schema".
- Give your schema a name. Mine is called "coffee" to suit espresso_gal. You can name it anything you want. (If you're stuck, "Test" is available :))
- Right-click on this new schema from the navigator box, and select “Set as Default Schema.”
- Next, we're going to create a table named "Test_Table" using two lines of code, and then we're going to execute the SQL script.
SQL script code
CREATE TABLE Test_Table ( id smallint unsigned not null auto_increment, name varchar(25) not null, constraint my_example primary key (id) ); INSERT INTO Test_Table ( id, name ) VALUES ( null, 'Test data' );
Create the test table:
a. Click on the SQL icon on the menu. This will create a new SQL tab for you to execute the queries.
b. Select the database you just created by clicking on it until the name turns bold.
c. Copy the first line of code from the SQL script above and paste it on to the new tab.
d. Click on the lightning icon to execute the first line of code. If it is successfully executed, you will see a green check on the report in the Output box below.
e. Next, copy the second line of code and paste it on the tab, below the first line of code. Again, click the lightning icon and it will execute.
To check that the table has been successfully created, you can to go to your new schema and look at the tables tab. If nothing is showing, click right and "Refresh All" from the options. Once you see your new table there, right-click on it and select the first option, "Select Rows - Limit 1000". You will see your test table along with the data.
4. Accessing your MySQL database remotely (the test)
We're going to have espresso_gal establish a remote connection to our coffee database, which we have just created using the two lines of code. Now how do we set a remote connection exactly? Right from the MySQL Workbench.
- Launch the Workbench and navigate to the "Database" tab on the top of the page, located just under the home icon, between View and Tools. From the options, select "Connect to Database".
- On the next window, make sure that the TCP/IP setting is the same as the one you set up earlier during installation. On the Username box, type in the new user you created for the test, and on the Default Schema box, input the name of your test database.
- Once you click okay, you will be asked to input the password for the new user.
The remote access is successful if you see that the MySQL Workbench connects to the MySQL database, allowing you to browse through your schema and input queries to modify your test database.
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.
Questions & Answers
© 2018 Lovelli Fuad