How to hook up mysql to my website? - php

I am runing linux mint. I have a mysql db called Test. Do i need apache or something to hook it up to my db?
When i try to do my php myqlconnect i get this:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Any ideas? thanks!

In order to make connection to MySQL database from in PHP script, you need:
Correctly configured MySQL server, with user, which has corresponding permissions
Correctly specified parameters in mysql_connect() function
Network connection from host where PHP script is started to host where MySQL database is running
PHP script may be executed either in command-line, or as web application. Obviously, Apache web server is necessary only for the second case.
For your specific error, you may take a look at corresponding questions at ServerFault, for example, at this one.

What you are trying to do is set up what's called a "LAMP" server (Linux, Apache, MySQL, PHP). There are plenty of guides on how to set these up, such as http://www.ubuntugeek.com/step-by-step-ubuntu-810-intrepid-ibex-lamp-server-setup.html

This is not Apache related; it's related to the configuration of the MYSQL server and PHP. The database can be reached via the network or local UNIX domain sockets(depending on configuration). In your case, PHP (via the MySQL client lib) is configured find a socket in /var/lib/mysql.sock.
Check the configuration. Ensure the database is running. Check for the socket in /var/lib/mysql.
BTW on my system, the socket is in /var/run/mysqld/mysqld.sock. That was configured from /etc/mysql/my.cnf.

First of all, you need Apache to run PHP even if you are not making any connections to your database. Usually Apache runs PHP as a DLL and you need to enter some commands in the httpd.conf (configuration) file of your Apache installation. Are you familiar with these?
Second, you have to have the MySQL server already up and running, in order to make a connection to your database through mysql_connect. I am not a Linux user, but in Windows, I have registered MySQL as a service to be started at startup. However, they may well be a way of doing this in Linux as well.

Again, these are for Windows - sorry!
You should replace the paths with the ones you have in your Linux Apache installation and the name of the DLL. And these are for version 5 of PHP (if that is what you are using)
PHPIniDir "C:/Program Files/PHP/"
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"

Related

How to connect to SQLYog localhost or otherwise

I am new to working with servers, and am not able to use SQLYog because it is not able to connect to any server. When I try to use the default values to connect to MySQL (localhost, root, port 3306), I get "Error No. 2003: Can't connect to MySQL server on 'localhost' (0).
Is there something I am missing or that I have to correct? I am just trying to create a database.
SQLyog is just one way to connect to a MySQL database and interact with it. Other popular methods include MySQL Workbench, Navicat, phpMyAdmin, and others.
Before you can use any of these, you have to first make sure your MySQL server is running and accepting connections. You didn't mention (or tag) whether you are running MySQL on your local machine, or what operating system you are using.
A couple of the most common development environments are LAMP or WAMP, for "Linux, Apache, MySQL, php" or "Windows, Apache, MySQL, php" respectively. Since you tagged your question php, I'll assume one of these is appropriate. (For example you're using SQLyog on Windows but your MySQL server is on Linux.) (I'll presume Ubuntu for Linux examples.)
On the machine you've installed MySQL (along with any other components), you should be able to determine its running state by looking at your process list (ps aux in Ubuntu, tasklist or Task Manager (GUI) in Windows).
You're looking for mysqld (Linux) or mysqld.exe (Windows). If the process isn't running, you need to start it (Linux/Windows). If you're using WampServer (a popular Windows all-in-one package), for example, you can also look in your Windows Services for "wampmysqld" or "wampmysqld64" and start the service there.
If you can't get the process to start, you may have a configuration error. If the process is running, you may need to check the configuration any way to determine what port it is running on, if it's not the standard 3306.
The configuration file name and location can vary by system and package, but generally it will be called "my.cnf" (Linux) or "my.ini" (Windows). Check your documentation for help on the specific path.
Once you've got the service running, you should be able to connect!
This is the default SQLyog connection dialog (at least in my slightly older version). The defaults assume you are running your MySQL server on your local machine (localhost), that you have a user "root" with no password, and the default port 3306 is used.
You'll need to change any of this information that differs on your particular installation.
Be aware that if you're trying to connect to an instance of MySQL on another machine, a firewall may be blocking a port.
These steps should hopefully get you on the right track.

Python and PHP use the same mySQL database

I have a script in Python that retrieves data from a remote server into a MySQL database located on my computer (the one that runs the Python script).
This script is executed daily to retrieve fresh data into the MySQL database. I am using Workbench 6.0 for Windows 64.
I want to add a web GUI to the system that will present some of the data on a web page. I would like to write it in PHP and for my PHP program to use the same MySQL database that my Python script uses. I would like to make it a web server later on so users can log in to the sever and use this web GUI.
Can the PHP and the Python scripts use the same DB?
In the past I have worked with WAMP sever for PHP only. If I install WAMP on the sever, will it be able to use the same DB or can it cause a collision?
I'd comment, but I don't have enough rep. yet. You should be able to start the Apache and PHP services separately from the WAMP tray icon. If you can't, try this:
You should be able to use the WAMP menu on the tray icon to open WAMP's my.ini file. In there, just change the port number from 3306 to something else (might as well use 3307) and save the file, then restart WAMP. In the future, completely ignore WAMP's MySQL instance and just use the one that Python is connecting to.
If that still doesn't work, you might have Skype running. By default, Skype listens on Port 80 for some bizarre reason. Open Skype's settings, go to the "Connection" section and untick "Use port 80 as an alternative for incoming connections." Then try restarting WAMP again.

How to activate the mysql database in xampp on windows platform?

I've installed XAMPP on my Windows XP. I started the XAMPP control panel and it shows apache and mysql are running.
When I check the status by going to localhost/xampp it shows:
mysql : deactivated
When I run php files that access the mysql database, it shows the following errors:
Warning: mysql_connect() [function.mysql-connect]: [2002] No connection could
be made because the target machine actively refused it. (trying to connect
via tcp://localhost:3306) in C:\xampp\htdocs\Elo_algorithm.php on
line 18
I've been through the XAMPP troubleshooting page and FAQ but I don't understand what to do. The XAMPP control panel shows mysql is running, but xampp status shows: deactivated.
What is going on here?
OK I got this issue resolved in my case, hopefully will help you too. Problem was system itself. If you run Vista, Win7, win8 you probably have permission issue. Simple go to XAMPP root and find mysql_start.bat and run it. Immediately after clicking your firewall will appear with message asking for permission. All you have to do is to allow permission (for private networks such a home or work network).
MySQL database ACTIVATED - and that's what matters ;)
I hope that helps to all of you who did not get an answer so far.
Cheers!
You should view in the xampp installation the file "\xampp\mysql\data\mysql_error.log".
This file contais the error log of MySQL and in it you can detect any problem like por in use.
For example, this lines of log show that the port 3306 (the mysql default) is used by another application and can't be accessed.
111130 8:39:56 [ERROR] Can't start server: Bind on TCP/IP port: No such file or directory
111130 8:39:56 [ERROR] Do you already have another mysqld server running on port: 3306 ?
111130 8:39:56 [ERROR] Aborting
If all in the MySQL is correct probably the problem is related to the driver in the php application. Currently PHP has two MySQL connector types, the "mysql" and the "mysqli", MySQL "mysql" connector, that use "mysql_" (the method that you are using to connect is mysql_connect) prefixed functions, is used for old MySQL 4 applications and when the MySQL 5.x if is in configured with the "old password" parameter. The "mysqli" is used for the new mysql 5.x versions and in php you must use "mysqli_" prefixed functions like "mysqli_connect.
The version used by the lastest versions of xampp is MySQL 5.5 and you require to used the mysqli connector.
Check whether the value of port variable in the "my.ini" file in your xampp mysql folder is 3306 or 8888
i was facing the same problem. I had to run setup-xampp.bat to make it working.
By the way I came across a new WAMP stack called AMPPS. It looks quite easier than XAMP. You should try. http://www.ampps.com
I could not get XAMPP for Windows (1.7.4 Beta2--I know; ancient) to work on Windows 7 (32 bit) as the MySql would not "activate" or connect to Apache. Just by dumb luck I installed .NET Framework 4.6 for another application and suddenly--Whoopee! It started working!

Is it possible to use the same MySQL database on Windows (XAMPP) and Linux (LAMP)?

I have dual boot mode with Windows 7 and Ubuntu 10.
On Windows 7 I have XAMPP installed, on Linux I have LAMP installed.
Is it possible to force the MySQL DB servers installed onto different operating systems (even though they would be of identical versions, they are different) to use the same physical files?
So in dual boot mode, independently from the fact that I ran another operating system, I would be able to use the same physical data. Sometimes I'd like to switch OS but it would be great to be able to use the same databases.
in your my.ini (in Windows it's located somewhere like C:\Program Files\MySQL\MySQL Server 5.1. It's the main configuration file for MySQL) file, you should have this line:
datadir="C:/ProgramData/MySQL/MySQL Server 5.1/Data/" for example
change it in both Windows and Linux Ubuntu to point to one single physical folder (on a partition with a file system which Windows could recognize). It will work. File formats are identical.
Whether you boot from Ubuntu, or Windows 7, it won't matter, 2 different builds of MySQL will be looking for data in the same place. Once data is modified in Windows environment, you boot up from Ubuntu and the data is there, modified.
Whilst not ideal, this should be fine as long as:
You're using identical versions of MySQL on both operating systems.
You shutdown mysqld before you copy the data files across. (If you're going to be copying the data files between partitions rather than keeping them on a shared fat32 partition).
In essence, as long as MySQL is running on an architecture of the same "endianness", then the file formats should be transferable.
As a suggestion, you could just close ICQ, etc. and use the free memory to run Ubuntu within a VirtualBox virtual machine on top of Windows 7 - hence ensuring you can trivially access your development environment without having to restart, etc.
This is actually quite a nice set up as it means you can use a Windows development environment if you want and simply host the site web site data on a Samba mount on the Ubuntu virtual machine.
As long as the data is sharedable/reable/writeable on both OS, and both file format are identical on both OS, it should be doable
The first problem I can imagine is the case insensitivity in windows.
so, convert your database/table ti camel_case (or camelcase) if you are always using CamelCase.
More information to read up - http://dev.mysql.com/doc/refman/5.0/en/limits-windows.html
Of course you can.
But I dont think, it is possible for your dual boot system.
Lets assume, your MySQL server is installed under Win7 machine. You can perfectly access it from Win7 enviroment, and even from Linux enviroment (if you do not deny access in your MySQL server settings).
The system, which is your MySQL installed under, has to be running! Then you can access it from multiple systems if allowed ;-)
I goggled a lot of time and found that running a Linux kernel is a solvable way and feasible solution.
Vagrant
It is a tool for building and managing virtual machine environments in a single workflow. The main reason why I argue you using vagrant is that it is not too heavy and does not swallow much of your computer resources. I believe you get through Vagrant documentation that will allow you to launch a Linux based machine on your physical machine.
Let's assume that the host machine is assigned IP 192.168.1.2 and the virtual machine has the IP address as 192.168.1.10, and make sure that the host and guest machine could see each other. Please read thoroughly Networking section to customise the network configuration.
Verifying connection between host and guest machine
Install MySQL Server
MySQL is a database management system. Basically, it will organise and provide access to databases where our site can store information.
Open a terminal in the machine has been set up from the above step. Run the following command:
sudo apt-get install mysql-server-5.6
Notes: It's up to the version of Linux distribution installed, the above command would be adjustable to suit your need. For install, I used the core of Ubuntu 14.04, see the link.
During the installation, your server will ask you to select and confirm a password for the MySQL root user. This is an administrative account in MySQL that has increased privileges.
Verifying the installation
From a terminal in the guest machine (i.e. the virtual machine), run the following command:
mysql -u root -p
will ask the MySQL password, then provide the one you have set up during MySQL Server installation. The following is the screenshot if you feed correct information to MySQL server.
Turn MySQL Server remotely accessible
Because we need a centralised database server where other computers could access and connect to the database of interest. Again, open a terminal and run the following MySQL commands:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'192.168.1.2' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441' WITH GRANT OPTION;
GRANT PROXY ON ''#'' TO 'root'#'192.168.1.2' WITH GRANT OPTION;
FLUSH PRIVILEGES;
which 192.168.1.2 is the IP address of the host machine and hashed string of password gets from user table in mysql database.
Okay. You might take a rest and enjoy your drink if there is no issue so far.
Verifying the remote access
From a terminal on the host machine, fire up the following command:
mysql -h192.168.1.2 -uroot -p
which is asking you to enter the password. If the root credential is correct, you would see a screenshot like the above one. One thing needs to be paid attention is to add -h192.168.1.2 following mysql command because we are not in the machine where MySQL server is installed.
All in all, we have set up a MySQL server used for both host and guest machine. In reality, if I have another machine assigned 192.168.1.3 can also connect to the database server and exchange data between server and client.

Connecting to external MySQL DB from a web server not running MySQL

While I've been working with MySQL for years, this is the first time I've run across this very newbie-esq issue. Due to a client demand, I must host their website files (PHP) on a IIS server that is not running MySQL (instead, they are running MSSQL). However, I have developed the site using a MySQL database which is located on an external host (Rackspace Cloud). Obviously, my mysql_connect function is now bombing because MySQL is not running on localhost.
Question: Is it even possible to hit an external MySQL database if localhost is not running MySQL?
Apologies for the rookie question, and many thanks in advance.
* To clarify, I know how to connect to a remote MySQL server, but it is the fact that my IIS web server is not running ANY form of MySQL (neither server nor client) that is giving me trouble. Put another way, phpinfo() does not return anything about MySQL. *
Yes, you can use a MySQL database that's not on the same machine as Apache+PHP.
Basically, you'll connect from PHP to MySQL via a network connection -- TCP-based, I suppose ; which means :
MySQL must be configured to listen to, and accept connections on the network interface
Which means configuring MySQL to do that
And given the required privileges to your MySQL user, so he can connect from a remote server
And PHP must be able to connect to the server hosting MySQL.
Note, though, that habing MySQL on a server that's far away might not be great for performances : each SQL query will have to go through the network, and this could take some time...
If phpinfo is not returning anything about MySQL you need to install the MySQL plugin for PHP, easiest way to do that probably is to just upgrade PHP to the latest version. If not there is a .DLL file that you will need.
http://www.php.net/manual/en/mysql.installation.php
you will need to install the mysql extensions. this link should help: http://php.net/manual/en/install.windows.extensions.php
The MySQL server has nothing to do with PHP itself. What "mysql support" in PHP means is that it's been compiled with (or has a module loaded) that implements the MySQL client interface. For windows, it'd be 'mysql.dll', and on Unix-ish systems it'd be 'mysql.so'. Once those are loaded, then the various MySQL intefaces (mysql_xxx(), mysqli_xxx(), PDO, MDB2, etc...) will be able to access any MySQL server anywhere, as long as you have the proper connection string.

Categories