Creating sql connections multiple times in loop causes to drop connection - mysql

I have one csv file. I am reading userId from it.It has around 30,000 users.
I have created one shell script to read that csv and search for that users in mysql database
and use that info to grab user related data from other tables.
I am running follwing kinds of query multiple times
mysql -uUserId -ppwd -hlocalhost -Pportno databaseName -e "Select *from UserInfo where UserName='Test'"
Its causing to drop the mysql connection after certain entries and automatically reconnecting.
Some entires get searched and some entries not.
Does Select query in loop for multiple times cause this problem
Any solution for it.

Related

How can I copy a table from one database to another database along with its data?

I have two separate databases installed on the same server. I name the representation as db1 and db2. I have a table named sample_table in my db1 database. I am trying to copy this table with its records to my db2 database with the following queries:
CREATE TABLE db2.sample_table LIKE db1.sample_table
insert into db2.sample_table select * from db1.sample_table
As a result of my research, I found that this method works in most places. However, when I type and run my query from the db2 console, I am having a user permission problem for db1. The query might be running, but should there be a configuration link between these two databases? If so, how can I get this link? What are the alternative solution methods I can overcome this problem?

"SELECT *" except for columns the user is not allowed to view

We've recently added a few restrictions as to what data an analyst can retrieve - specifically the password column in a users table.
The problem is she has thousands of queries which feature SELECT * from users, or joins, etc etc, in his scripts.
Now, when she attempts to run these, MySQL returns:
SQL Error (1142): SELECT command denied to user 'foo'#'bar' for table 'users'
The RDBMS is actually AWS Aurora MySQL 5.6.10a, if this helps. The analyst is reading from a read-replica, so has no write access, but can use and create temporary tables.
Is there a MySQL setting or something we can do, rather than getting the analyst to specify every single column?

How to get all the queries which we have used in a mysqldb

I have a mysql database with lots of tables and records in it. I want to get the list of queries which were used to create tables , insert records etc in that mysqldb. Is it possible. I have seen it in Oracle sql developer. If we click on the table we can view the queries which we have used, for DDL. How to do that in msyql. I am using mysql command line client.
You could use the mysqldump-utility for that
In Linux:
mysqldump my_database_name >dumpfile.sql

How can I run an sql command on all databases at once?

I have circa 80 wordpress blogs, which write about my various sites. I want to update all posts in all databases for a specific string of text, in this instance, a domain name.
The script works perfectly fine, on one database, but I will need to make several changes, to ALL databases which will simply take far too long.
I need to be able to run these commands across every database at once, rather than one database at a time.
My script, as it works currently on a single database:
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://www.old-domain.com', 'http://mixudo.com');
Many thanks
Try executing this query:
SELECT CONCAT('UPDATE ',
schema_name, '.\'wp_posts\' SET \'post_content\' = REPLACE(post_content, \'http://www.old-domain.com\', \'http://mixudo.com\');')
FROM information_schema.schemata
WHERE schema_name NOT IN ('information_schema','mysql','performance_schema','phpmyadmin', 'webauth');
and then reexecuting the result.
Source: https://dba.stackexchange.com/a/20251
Create a list of database names with usernames/passwords that you need the script to be executed on
For each database in this list do:
Log into that database and select it
Run the script on that database
That is the only way to do your task safely.
You can get a list of databases on a mysql server using the SHOW DATABASES command. Using that as input you can write a shell script in whatever language you are capable that will foreach() through the list of databases, and running the UPDATE statement on each, by executing
use DBNAME;
Where DBNAME is the variable in the foreach. The update statement should work the same on all the databases, as I assume each db has the same set of tables with the same tablenames in each.
To Ed Heal's point, this also assumes you're using the root/admin user for the server so that it will have rights to update all the tables.

SQL Query to list the contents of all the tables from the database

I wrote a PHP script to list all the tables present in the database using a query
$query = "SHOW TABLES FROM $dbName";
Now, i wanted to know what sql query can be use to list the "contents" of all the tables from the database simultaneously to display it and then store it in a file.
Thanks ahead of time..
You can use mysqldump to dump all the data in the database. Else you can run for look on all the table names and then use select * from x; to get all the data from the table name x which you retrieved earlier.
IF what you want is to backup your database and restore it check out this link
MYSQL backup and restore
and you can use this command to create a backup of your databse
MYSQLDUMP