Beginning with MySQL

Discussion in 'Mac Programming' started by stoid, Mar 30, 2005.

  1. macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #1
    I would like to be able to experiment with basic MySQL on my PowerBook, doing all work locally. I have downloaded the MySQL bit from VersionTracker, and installed it and the PrefPane no problem. I have also downloaded a front end app, CocoaMySQL but don't know how to configure the two to work together.

    Also, where is a good place to get basic info on MySQL and what it's uses are.

    Note: I have roughly zero experience here, and need things explained in beginner terms.

    Thanks!

    stoid
     
  2. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #2
    Ok, I've downloaded the MySQL Manual from their website, and know that I've got it installed properly because when I go into the terminal and type:
    Code:
    sudo /Library/StartupItems/MySQL/MySQL start
    like it says it starts and does not give me any error messages.

    Next step is connecting. Do I use the
    Code:
    mysql -h [I]host[/I] -u [I]user[/I] -p
    ?

    Command Line from the Terminal?

    For the time being I'm ditching CocoaMySQL, BTW.

    I don't know what I should put though for host, I'm guessing I user my user short name and pass though.

    Please help! Thanks


    stoid
     
  3. macrumors 6502a

    Hoef

    Joined:
    Jul 11, 2004
    Location:
    Houston, TX..... (keep walking)
    #3
    If you are also in PHP you might want to try: PHP and MySQL by Ullman . Awesome book with great examples ... still using it.
     
  4. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #4
    I'd really like to be able to do it just on my local machine, but when I get ready to MySQL on the internet I'll take a look. Thanks!

    Is it possible to access the install of MySQL on my laptop or does that only work on an external server.
     
  5. macrumors 68000

    Joined:
    Jun 13, 2004
    Location:
    afk
    #5
    I believe the mysql installation should reside in /usr/local if I am not wrong. In that directory, you should see the mysql folder (rather long name with the version number). The bin folder inside the mysql should allow you to perform mysqld_safe, mysql so on.
     
  6. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #6
    BINGO! That seems to have worked, at least now I'm getting a terminal window with the mysql> prompt like the manual shows. I'll play around a bit more, so maybe I can get this one on my own, but a friend sent me a basic sql script (filename.sql) How do I run it?
     
  7. macrumors 68000

    Joined:
    Jun 13, 2004
    Location:
    afk
    #7
    There are 2 ways to use it. Both required you to use the mysql command. First way is to go into to mysql console then perform this command, "source ./filename.sql" where the filename.sql should be in the same folder. or you can directly input from the terminal command line by using "mysql database_name -u user_name -p << filename.sql" , I would prefer the first one. less prone to typo errors.

    EDIT: Couldn't remember whether its "<<" or "<"
     
  8. macrumors 65816

    wrc fan

    Joined:
    Jan 19, 2003
    Location:
    In a world where LPs are made like pancakes
    #8
    to connect from the terminal to your local host you just need to to `mysql -u root` unless you set up a password. To run the sql file just do `mysql -u root < filename.sql` (make sure before you do that that you're in the directory where that script is kept, or do the full path to the script)

    EDIT: and to set a password, from inside the mysql prompt you can enter `SET PASSWORD FOR root@localhost=PASSWORD('your_new_pass');`
     
  9. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #9
    I opened up the script in BBEdit, and I see that it's just a few short and simple create table and insert into functions. I am getting the basic math/version/date functions to work, and could read/write the test database, but I'm getting permission errors when I try to create a new database.

    SELECT USER(); returns:

    Code:
    +-----------------+
    | user()          |
    +-----------------+
    | brian@localhost |
    +-----------------+
    1 row in set (0.00 sec)
     
  10. macrumors 65816

    wrc fan

    Joined:
    Jan 19, 2003
    Location:
    In a world where LPs are made like pancakes
    #10
    hmm... looks like your install wasn't set up with a root user, instead use `mysql -u brian` to replace all occourances of `mysql -u root`

    Oh, and if the script doesn't create a database, you'll need to do that

    mysql > CREATE DATABASE database_name;

    and then run the script from the command line;

    mysql -u brian database_name < filename.sql
     
  11. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #11
    I'm a bit lost here, where do I use the 'mysql -u brian'? In a standard Terminal window? When I do, I get this:

    Code:
    Last login: Sun Apr  3 21:10:33 on ttyp1
    Welcome to Darwin!
    <ComputerName>:~ brian$ mysql -u brian
    -bash: mysql: command not found
     
  12. macrumors 68000

    Joined:
    Jun 13, 2004
    Location:
    afk
    #12
    You have to locate the mysql command which should be in the bin folder of mysql installation and execute from there.
     
  13. macrumors 65816

    wrc fan

    Joined:
    Jan 19, 2003
    Location:
    In a world where LPs are made like pancakes
    #13
    Assuming mysql is installed in the default location do this to add it to your path:

    echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.profile

    then do:

    source ~/.profile

    and then mysql will be in your path and you can run the command from anywhere.
     
  14. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #14
    Maybe this is what you were trying to tell me to do, but I typed:

    Code:
    sudo /usr/local/mysql/bin/mysql -u root
    into a bash terminal window, and it logged me into the mysql as user 'root@localhost' and is letting me create a database. Unless there is a simpler way to login in the future... but this works for now. Thanks again for all your help!!
     
  15. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #15
    http://www.sitepoint.com/

    down at the bottom right is a like for a book on php and mysql. it has the first 3 chapters in pdf. it will answer all the question you are having. it even has Mac specific examples.

    This only applies if you are using bash. You can tell by typing echo $SHELL. if you see: '/bin/bash/' you are using bash.

    as to your question about logining in. you can simply matters by making a script by editing your the ~/.bashrc file. Another poster tried to get you do it but his ~/.profile was wrong. don't use textedit causes problems. the easiest way is from the command like and type 'pico .bashrc'. although, you can edit it with emacs or vi if you would like.

    anyways add this line to your ~/.bashrc

    Code:
    export PATH=/usr/local/mysql/bin:$PATH
    ctrl-x to save.
    exit out of terminal and open a new session.

    the line will make every command in /usr/local/mysql/bin accessable to you without typing the long command line.

    also, i use a script to login to mysql. use 'pico lm' and enter (ctrl-x to save)

    Code:
    mysql -h localhost -u loginName -p
    then from terminal do chmod 766 lm. now you do './lm' and simply login.
     
  16. macrumors 65816

    wrc fan

    Joined:
    Jan 19, 2003
    Location:
    In a world where LPs are made like pancakes
    #16
    No it's not wrong. Try adding something to your .profile file, and you'll see it works just fine. Just cause one thing is your custom doesn't mean doing it a different way is wrong.

    I could just as easily say that's wrong because there's no reason to specify host when connecting to localhost, but since it works fine, you can't really say it's wrong.


    Now back to stoid, I think your problem is that you didn't start up mysql with sudo. So next time you want to turn it on use `sudo mysql_safe`. The reason I say that is because you shouldn't have to run the mysql command as an administrator. The only other thing might be that the /usr/local/mysql/data folder might not have the correct permissions. So if running the `sudo mysql_safe` doesn't fix it, then you should run `chown -R mysql /usr/local/mysql/data`
     
  17. macrumors member

    Joined:
    Apr 12, 2005
    Location:
    Cincinnati, OH
    #17
    If you want to use CocoaMySQL just use "localhost" as the host, then use a valid MySQL username and password. Just remember that these are NOT the same as your Mac's username(s)/password(s).
     

Share This Page