Installing MySQL Python on Mac OS X

后端 未结 15 1914
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 03:02

Long story short, when I write the following:

sudo easy_install MySQL-python

I get the error

EnvironmentError: mysql

相关标签:
15条回答
  • 2020-11-27 03:37

    It's time to be a big boy and install from source. Try this:

    1) Download the MySQL-python-1.X.X.tar.gz file(by default will go to your Downloads directory)

    2) Open a Terminal window and cd to the Downloads directory.

    3) Unzip the file you downloaded:

    ~/Downloads$ tar xfvz MySQL-python-1.X.X.tar.gz
    

    That will create a directory inside your Downloads directory called MySQL-python

    4) cd into the newly created directory.

    5) Typically, you just open the file called README or INSTALL and follow the instructions--but generally to install a python module all you do is:

    $ sudo python setup.py install
    

    If you care to look, there should be a file called setup.py inside your newly created MySQL-python directory, and you are invoking that program to install the module.

    Also note that this:

    export PATH=$PATH:/usr/local/mysql/bin
    

    is not permanent if you did that on the command line. You need to put that line in a file called .bashrc in your home directory (~/ or equivalently /Users/YOUR_USER_NAME). To see if .bashrc already exists(it's a hidden file), issue the command:

    $ ls -al 
    

    and look for .bashrc. If .bashrc doesn't exist, then create it.

    0 讨论(0)
  • 2020-11-27 03:38

    Here's what I would install, especially if you want to use homebrew:

    • XCode and the command line tools (as suggested by @7stud, @kjti)
    • Install homebrew
    • brew install mysql-connector-c
    • pip install mysql-python
    0 讨论(0)
  • 2020-11-27 03:39

    I used PyMySQL instead and its working fine!

    sudo easy_install-3.7 pymysql
    
    0 讨论(0)
  • 2020-11-27 03:40

    On Mojave, I ran into errors with finding the SSL libraries, here's what finally worked without having to modify mysql_config:

    sudo pip install MySQL-Python --global-option=build_ext --global-option="-I/usr/local/opt/openssl/include" --global-option="-L/usr/local/opt/openssl/lib"
    

    Hopefully that will save someone a few hours of heartache

    0 讨论(0)
  • 2020-11-27 03:41

    Another option is to use pymysql it is a pure Python client connection to MySQL so you don't have to mess around with compiling, a good exercise, but it can be frustrating if you are just trying to get something done. pymysql follows the same API as MySQLdb, it can essentially be used as a drop in replacement.

    Also, it used to be that MySQLdb, did not work with Python 3, but this may have changed, pymysql didn't have that problem which also induced me to switch, this may have changed though. pymysql can be slower than MySQLdb but you'll have to see if you notice that, it is also under a different license (MIT for pymysql, GPL for MySQLdb)

    0 讨论(0)
  • 2020-11-27 03:44

    To install PyMySQL

    install pip => sudo easy_install pip

    install PyMySQL=> sudo easy_install-3.7 pymysql

    terminal command to check whether installed or not => pip3 list

    or

    install PyMySQL=> sudo pip install PyMySQL

    terminal command to check whether installed or not => pip3 list

    MySQL

    The macOS Sierra Public Beta’s didn’t play well with MySQL 5.7.x, but these issues are now resolved by using MySQL 5.7.16

    MySQL doesn’t come pre-loaded with macOS Sierra and needs to be dowloaded from the MySQL site.

    ( https://dev.mysql.com/downloads/mysql/) The latest version of MySQL 5.7.16 does work with the public release of macOS.

    If you already have MySQL 5.7 and you have upgraded OS from El Capitan to Sierra I expect that to be ok, but will be interested if anyone comments on that.

    Use the Mac OS X 10.11 (x86, 64-bit), DMG Archive version (works on macOS Sierra).

    If you are upgrading from a previous OSX and have an older MySQL version you do not have to update it. One thing with MySQL upgrades always take a data dump of your database in case things go south and before you upgrade to macOS Sierra make sure your MySQL Server is not running.

    When downloading you don’t have to sign up, look for » No thanks, just take me to the downloads! – go straight to the download mirrors and download the software from a mirror which is closest to you.

    Once downloaded open the .dmg and run the installer.

    When it is finished installing you get a dialog box with a temporary mysql root password – that is a MySQL root password not a macOS admin password, copy and paste it so you can use it. But I have found that the temporary password is pretty much useless so we’ll need to change it straight away.

    You are also told: If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.(https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html)

    Change the MySQL root password

    Note that this is not the same as the root or admin password of macOS – this is a unique password for the mysql root user, use one and remember/jot down somewhere what it is.

    Stop MySQL

    sudo /usr/local/mysql/support-files/mysql.server stop
    

    if stop throws permission issue

    Check the error file first.

    tail -f /usr/local/mysql/data/*.err
    

    Do a complete shut down or kill the process. Confirm that no mysql process is running

    mysqladmin -uroot shutdown
    sudo killall mysqld 
    
    ps -ef | grep mysql
    

    Give permisiions

    sudo chown -RL root:mysql /usr/local/mysql
    sudo chown -RL mysql:mysql /usr/local/mysql/data
    chmod -R 755 /usr/local/mysql/data
    chmod -R 755 /usr/local/mysql/data/accountname.local.pid
    
    or Right click->get info and change the permission for 
    /usr/local/mysql/data
    /usr/local/mysql/data/Pushparajas-MacBook-Pro.local.pid
    

    Start mysql

    sudo mysql.server start
    

    Start it in safe mode:

    sudo mysqld_safe --skip-grant-tables
    

    This will be an ongoing command until the process is finished so open another shell/terminal window, and log in with a password which is temporary generated:

    mysql -u root -p
    FLUSH PRIVILEGES;
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass'
    

    ; Change the lowercase ‘MyNewPass’ to what you want – and keep the single quotes.

    \q
    

    Start MySQL

    sudo /usr/local/mysql/support-files/mysql.server start
    

    Starting MySQL

    You can then start the MySQL server from the System Preferences or via the command line.

    Command line start MySQL.

    sudo /usr/local/mysql/support-files/mysql.server start
    

    To find the MySQL version from the terminal, type at the prompt:

    /usr/local/mysql/bin/mysql -v -uroot -p
    

    This also puts you in to a shell interactive dialogue with mySQL, type \q to exit.

    After installation, in order to use mysql commands without typing the full path to the commands you need to add the mysql directory to your shell path, (optional step) this is done in your “.bash_profile” file in your home directory, if you don’t have that file just create it using vi or nano:

    cd ; nano .bash_profile
    export PATH="/usr/local/mysql/bin:$PATH"
    

    The first command brings you to your home directory and opens the .bash_profile file or creates a new one if it doesn’t exist, then add in the line above which adds the mysql binary path to commands that you can run. Exit the file with type “control + x” and when prompted save the change by typing “y”. Last thing to do here is to reload the shell for the above to work straight away.

    source ~/.bash_profile
    mysql -v
    

    You will get the version number again, just type “q” to exit.

    Fix the 2002 MySQL Socket error

    Fix the looming 2002 socket error – which is linking where MySQL places the socket and where macOS thinks it should be, MySQL puts it in /tmp and macOS looks for it in /var/mysql the socket is a type of file that allows mysql client/server communication.

    sudo mkdir /var/mysql
    sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
    

    phpMyAdmin

    uncomment below line in httpd.conf file

    LoadModule php7_module libexec/apache2/libphp7.so
    

    First fix the 2002 socket error if you haven’t done so from the MySQL section-

    sudo mkdir /var/mysql
    sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
    

    Download phpMyAdmin, (https://www.phpmyadmin.net/downloads/) the zip English package will suit a lot of users, then unzip it and move the folder with its contents into the document root level(~/Sites/) renaming folder to ‘phpmyadmin’.

    Make the config folder

    mkdir ~/Sites/phpmyadmin/config
    

    Change the permissions

    chmod o+w ~/Sites/phpmyadmin/config
    

    Run the set up in the browser http://localhost/~username/phpmyadmin/setup/ or http://localhost/phpmyadmin/setup/

    You need to create a new localhost mysql server connection, click new server.

    Switch to the Authentication tab and set the local mysql root user and the password.

    Add in the username “root” (maybe already populated, add in the password that you set up earlier for the MySQL root user set up, click on save and you are returned to the previous screen. (This is not the macOS Admin or root password – it is the MySQL root user).

    Make sure you click on save, then a config.inc.php is now in the /config directory of phpmyadmin directory, move this file to the root level of /phpmyadmin and then remove the now empty /config directory.

    In the latest phpmyadmin, download the config.inc.php and place in phpmyadmin directory.

    If you want to setup new server move config.inc.php to some location and try http://localhost/~username/phpmyadmin/setup

    Now going to http://localhost/~username/phpmyadmin/ will now allow you to interact with your MySQL databases.

    To upgrade phpmyadmin just download the latest version and copy the older ‘config.inc.php‘ from the existing directory into the new folder and replace – backup the older one just in case.

    Permissions

    To run a website with no permission issues it is best to set the web root and its contents to be writeable by all, since it’s a local development it shouldn’t be a security issue.

    Lets say that you have a site in the User Sites folder at the following location ~/Sites/testsite you would set it to be writeable like so:

    sudo chmod -R a+w ~/Sites/testsite
    

    If you are concerned about security then instead of making it world writeable you can set the owner to be Apache _www but when working on files you would have to authenticate more as admin you are “not” the owner, you would do this like so:

    sudo chown -R _www ~/Sites/testsite
    

    This will set the contents recursively to be owned by the Apache user. If you had the website stored at the System level Document root at say ~/Sites/testsite then it would have to be the latter:

    sudo chown -R _www ~/Sites/testsite
    

    Another easier way to do this if you have a one user workstation is to change the Apache web user from _www to your account. That’s it! You now have the native AMP stack running on top of macOS Sierra.

    Ref Link - https://coolestguidesontheplanet.com/get-apache-mysql-php-and-phpmyadmin-working-on-macos-sierra/#ssu

    0 讨论(0)
提交回复
热议问题