Scripting the initialization of a MySQL database - mysql

I'm attempting to script the creation of a machine image that includes MySQL with some seed data.
I'm looking for a way to start the MySQL database engine, run a .sql script to create the data, and then shutdown the engine. Is there a best way to do this?
It looks like I could use the --init-file argument of mysqld to launch mysql and run the script, but how would I cause the database engine to shutdown after the script completes?
I'm on Ubuntu if it matters.

Just do these three commands in one script:
service mysql start
mysql -umyuser -pmypass < /path/to/your/startup.sql
service mysql stop
You may consider using skip-networking in your /etc/mysql/my.cnf file so that the database is not accidentally accessed by anyone else for the short perios when it's up and running. In which case add the --socket argument to your mysql client command.

Related

Problem:: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I can't login my MySQL server. I got many solutions via the internet but not working any single solution.
The first thing is to ensure that MySQL server is up and running. This procedure is different for different distros, but you can try
$ sudo /etc/init.d/mysql start
to start
MySQL is not starting.
As suggested, your first step is to bang out the typical commands: service mysql status and journalctl -xe to see what errors are reported. The usual suspects are permissions on the mysql data folder typically located in/var/lib/mysql (permissions should be mysql:mysql) and also errors in my.cnf (/etc/mysql/my.cnf or /etc/mysql/mariadb.conf.d/50-server.cnf for recent mariadb installs)
If your MySQL was previously running and now wont start, you might be dealing with table corruption. Get mysqlcheck running on it if this is the case. If you are going to attempt a database startup with innodb_force_recovery flag, make sure you have your data backed up prior to launching the database.
Let us know the output of these commands so we can further assist.

Error use mysql-slow.log permission

I've a Galera Cluster Installation with three nodes. As requirement my MariaDB installation must be in different path that default, and user cannot be mysql. So I've moved all mysql data path and I've created a new user and group to manage the mysql service.
MariaDB service is working fine, but I've noticed that a mysql-slow.log has been created with permissions to mysql:root for a third program installation that monitor queries. Opening mysql.err I've found the following error:
[ERROR] Could not use mysql-slow.log for logging (error 13).Turning logging off for the whole duration of the MySQL server process. To turn it on again: fix the cause, shutdown the MySQL server and restart it.
So I've made chown myuser:mygroup mysql-slow.log and restart the service, but there still having same permissions. Then I've set in my.cnf the slow_query_log=1, in order to try that the myuser create the file at start, but the wrong permission still there also after remove the files and restart.
All the files, in my mariadb data path, are not managed by mysql:mysql user, in my installation are managed by myuser:mygroup so when the mysql-slow.log is created the permissions mysql:root are assigned so I have not permission to write there with myuser:mygroup and if I assign them manually there is not effect because are override with mysql:root after mysql restart.
Somebody knows if there are any way to set by default the user and group owners of the mysql-slow.log file when created?
Thanks.
Regards,
Sergio
Fix the permissions:
chmod 666 mysql-slow.log
See what your script is doing; perhaps it needs fixing, too.

Set on mysql only with one running schema

Can anyone help me to set up a mysql database only with one schema?
I tried to reset root password to database, but while switching on mysqld I've got an error that mysqld is ended.
I tried to change my.cnf etc. but I have no time to tries because shop is still running on this database so I shouldn't switching down a database.
Ones upon a time I was reseting a root password but then I had no problems with it. I'm thinking that InnoDB could be main problem on mysql because I had some errors on logs while I tried to set up mysqld with --skip-grant-tables and --skip-networking. I would like to start mysqld only with main mysql schema running but I can not Google any tutorials for that. Do anyone know how to do this? That could be easier and faster then trying to set up my.cnf correctly for mysqld. I spend few hours on that and I have no f**king idea what am I doing wrong?! (switching off innodb on my.cnf doesn't helped me - I tried) I'm thinking about dump all databases and reinstall mysql on Ubuntu but it sucks... (I have access but not by root)
//get it using DATABASE() function -
SELECT DATABASE();
// set it using USE statement -
USE database1;

How do you start a MySQL server on linux?

Pretty simple question here. I just want a SQL database on my version of Kali linux so I can practice SQL.
I opened the command line and entered tried to start mysql and get an error.
> mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
I also made sure it was already installed using apt-get.
What are the steps I need to take to be able to make a database with tables and data that I can query?
I don't know about Kali, but on Ubuntu it would be
$ sudo service mysql start
Once that command returns, the mysqld service has started, so you can use the mysql client to connect to it.
Of course, you also have to make sure you have the mysql-server package installed, not just mysql-client and mysql-common, and that you've initialized the database instance. Complete post-installation instructions can be found in the official documentation, but the short version is
Make sure the installer has created the mysql user account. This is the account that will "own" the server process once it starts.
Change to your data directory. (I used the installer's default of /var/lib/mysql; you can change this by editing my.cnf.)
As root, execute the server daemon with the --initialize switch. Check whereis to determine the correct path, then
$ sudo /path/to/mysqld --initialize --user=mysql
This command will twiddle itself for a while, then display an automatically-generated password and exit. Once the command returns, the database instance has been initialized and the system tables created. You can now start the database instance normally (using service start), then log in as the database user root (which is not the same as the system user root) using the password from above, then change your password, create a new database user, log in as that user, create a user database, and start creating tables.
Again, the official documentation is the place to look for this; if any of the instructions in the official documentation differ from my instructions, you should ignore me and follow the official documentation's instructions.
If sudo service mysql start doesn't work for you, please try running mysqld_safe and don't kill the process. Use another tab to check the status of mysql service. This should solve your mysqld.sock issue.
If it doesn't work out, then please edit your my.cnf file and add the following:
socket=/var/lib/mysql/mysql.sock
And the permissions,
sudo chmod -R 755 /var/lib/mysql/
Hopefully, this should do it.
On my version of kali (2022-rolling), even though it's installed as MySQL, it's run as mariadb. To start it is:
sudo /etc/init.d/mariadb start
Try this
sudo service mysql start

Launchd and user mappings

I'm trying to get MySQL to run under launchd so I set up a launchd item including a "UserName" key with "mysql" as value. I did this because when I start mysql I usually go: mysqld_safe --user=mysql.
When I load it, I get:
10/22/10 11:13:02 AM com.apple.launchd[1] (com.mysql.mysqld463) Suspicious setup: User "mysql" maps to user: _mysql
10/22/10 11:13:02 AM com.apple.launchd[1] (com.mysql.mysqld) Throttling respawn: Will start in 10 seconds
And MySQL never gets to run. Should I use "_mysql" as the user? I wonder about user mappings because I installed my databases following instructions included in MySQL distribution: using the mysql user to install databases and to run the daemon.
Thank you in advance.
Just have launchd run exactly the command you would normally run. The one change would be to use an absolute path to mysqld_safe. mysqld_safe will then spawn a mysql process running as the user mysql, and you'll be back in familiar territory.