Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we have MySQL installed, and this week we're going to look at how we can configure MySQL. Specifically, there's two changes I want us to make. The first of those is I want us to add MySQL to our PATH. That's going to allow us to access MySQL from the command line inside Unix. We'll talk about what the PATH is and how to work with it in just a moment. The second change that I want us to make is I want us to set a root password for MySQL. By default, there is no password required to log into MySQL as the root user, that is the user with the most access privileges so its wide open by default.
That's not a great security measure to have. Even though we're just on our local machine it's still a good idea to always go ahead and set a root password. To make sure that your database is locked down and only someone with that password has access to the data that's inside of them. So we're going to take care of those two things, and we can do those from the command line. So I'm going to open up terminal. And if you're in a new window, then you should already be in your user directory. You'll just want to make sure, pwd will tell you, and cd with that little tilde after it, will make sure that you're in that user directory, that's where we all want to be. So the first task is that we want to set up MySQL so that it's accessible from our PATH.
So now let's talk about what the path is and why it's important that we make this change. The path is a list of directories uses in order to locate programs on the machine that it can run. So for example, we have PHP installed and I can type which php. and it'll come back and tell me where it's located. It's in the directory user/bin, that's where it is. And user bin is in my PATH, it's one of the directories that Unix looks in to find programs. But MySQL is not in one of those, which mysql comes back if it didn't find it in any of the preset directories. So we have to add the directory where MySQL resides to that list. We can take a look at that list by just saying echo, dollar sign, all capital PATH.
This is a variable that contains a list of the directories it's going to check in with colons in between each one of those. So it's first going to look in user bin, bin and bin, bin and user s bin, then an s bin and then user local bin. And that's the order of priority. It's going to look in each of those directories until it finds the thing that we're looking for. So that's how it was able to find php. And it went through all of those and did not find MySQL, so we need to add our location for that. Now, where is MySQL located? Well the place that it installs it is going to be inside, I'll just type ls here, /user/local/mysql/bin.
That's the directory where it's located, I'm going to do a listing of that directory, and here it is, MySQL. There's also a lot of other little programs that installs along with it, but that's the program that we're looking for, MySQL. So what we need to do, is essentially add this, to this long list here. The way we can do that is by editing one of our bash configuration files. Bash is the environment that we're using in Unix. Don't worry too much about that. But if we type ls dash la from our user directory, you will see a list of the files thatb are used in the directory, and you'll see that I have one here called .bash_profile. You may or may not have this file.
I have it because I've done some configuration in here before. Don't worry about whether you have it or not, this is the file that we want. If you have one that's called .bashrc, that would work as an alternative. Bash profile it's a little simpler, if you don't know the difference between the two go ahead and just use bash underscore profile. If you have bashrc and know the difference then you can use that. Now in order to edit that file, I am just going to type nano.bash_profile that's going to work even if you don't have that file. Okay, so the nano.bash_profile is going to open it up in the nano text editor and you can see I have just got some basic configuration in here.
What we're going to want to do is add a new line to it that's going to say, export. And then, PATH without the dollar sign, equals, and then in double quotes I'm going to put dollar sign, PATH. That's going to echo back the value of PATH that we just saw. But before we echo it, we're also going to append to it, user/local/mysql/bin:. You see what I did? So it's basically saying the path now is going to be equal to this new directory, followed by whatever was previously set by the operating system.
I'm going to just add my new PATH to the front of it, so that's it. That's all there is to it, and we want to make sure that we export it, make sure that you spelled everything right, make sure you've got your quotes right. And then Ctrl+X to exit, Y to Save changes, and Return to accept the name. You can do cat .bash_profile, that's a Unix command that will just output it for us, so that we can see what it looks like. We see that it's there Now if we say echo dollar sign PATH, you'll see that its not listed there. That's because it's changes haven't taken effect. We can either close this terminal window and reopen it and they would take effect because it reads that dash profile every time it starts up.
Or the other way we can do it is just to say source .bash_profile, and that will read in that code. Now we'll do echo path and you'll see that now it has upended the beginning. So it's going to look in that place, we say which mysql, now it has located it, now it knows where MySQL is. And that's great because we want to use another program that's in that same folder to set the root password, and that's going to be the MySQL admin program. If we say which mysqladmin, you can see that it's located in that same spot.
So this is the program that lets us set passwords, among other things. Mysqladmin and the user we want to set it for is our root user. Root is the name of the sort of most powerful user on any Unix system, and it's true for MySQL as well. So, the root user, and we want to set a password. Now, we could put the password in quotes after it, but an even better practice is to just hit Return after this line, and it'll come up and ask me for my password. Now, there was no password set previously. I'm just setting a new password. It can be anything you want.
I'm going to type secret, SECRET, and it doesn't show what you're typing. Don't worry about that, that's a security measure. Hit Return, it asks me to type it again. I'm going to type secret again. And now it's set, now my password is there. If I try and run that command again you'll see it says oops, sorry you can't do that because did not provide a password. If you want to change your password, well, then you just need to provide the dash p option to that. And it'll come up and say, all right, give me your current password, SECRET, and the new password.
I'm going to make my new one John, Paul, George and Ringo. Type that again, John, Paul, George, Ringo. Okay, so now that's my new password. All lowercase, all run together. John Paul George Ringo is my new password. Now you can't see that, but you hopefully put in your own password anyway and not the same password as me. Now we can still access MySQL. We can still run the MySQL program from here. But we won't be able to access it remotely at all.
Only from the local computer. Only when we're sitting right here can we still get into the MySQL program. And it's important to still have access to MySQL in some way from this computer. Because that's going to allow us to reset the password if we ever lose it. And there are instructions on how to go about resetting a lost root password on the MySQL site. So if you ever lose it you can go to their site and they'll help you to get it reset. Okay, now that we've got our password set, we've completed the configuration of MySQL and we're ready to use it.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97424 Viewers
61 Video lessons · 84654 Viewers
71 Video lessons · 68860 Viewers
56 Video lessons · 101318 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.