So im trying to import a MySQL dump that contains two databases with Ansible on a Windows box.
The Ansible playbook should be correct.
- hosts: win
gather_facts: no
tasks:
- name: Import DB
win_command: 'mysql -uroot -p12345678910 -hlocalhost < "C:\sql\dump.sql"'
args:
chdir: C:\Program Files\MySQL\MySQL Server 5.6\bin\
On the windows box if I open CMD and run
cd C:\Program Files\MySQL\MySQL Server 5.6\bin\ and then 'mysql -uroot -p12345678910 -hlocalhost < "C:\sql\dump.sql"' it works perfectly.
Im getting a giant error. What am I doing wrong?
Im able to check the DB with a very similar Playbook:
- hosts: win
gather_facts: no
tasks:
- name: Check DB
win_command: 'mysql -uroot -p12345678910 -e "SHOW DATABASES;"'
args:
chdir: C:\Program Files\MySQL\MySQL Server 5.6\bin\
The problem might be input redirection. Perhaps you can try something like:
win_command: 'Get-Content C:\sql\dump.sql | mysql -uroot -p12345678910 -hlocalhost'
See Redirecting standard input\output in Windows PowerShell
Related
It is a super weird issue. I have a simple playbook to execute the mysqldump command to backup MySQL:
tasks:
- name: Run mysqldump SQL server
ansible.builtin.shell:
cmd: mysqldump --max_allowed_packet=512M --set-gtid-purged=OFF -u root -p{{ mysql_root_password }} myDB > /tmp/myDB-{{ ansible_date_time.date|replace('-','') }}.sql
If I manually run this playbook, everything is good. The size of my backup file is 800MB. But If I run this playbook by cronjob. I lost some data and tables in the DB. The size of the backup file only has 450MB
* 11 * * * ansible-playbook -i ~/my_hosts.ini --vault-password-file ~/ansible_vault_password ~/mysql_backup.yml
Does anyone have the same issue?
I am very new to Ansible, any help is appreciated!
I tried something like this snippet below, but the second task gets executed on the host's shell terminal, rather than the mysql shell. How do i execute commands on the opened mysqlsh in the first task?
Eg: i want to login to the shell and check the status of the mysql cluster.
tasks:
- name: "connect as clusteradmin"
shell: mysqlsh --uri admin#host1 -p'pass'
- name: "get cluster"
shell: var cluster=dba.getCluster('cluster')
- name: "check status of cluster"
shell: cluster.status()
register: Clustersts
try to put your request into one line with parameter '--execute=command'.
Here is something which works for me:
mysqlsh -uUSER -pPASSWORD -P 3306 --execute='util.checkForServerUpgrade()'
Yours could look like as follows:
- name: "connect as clusteradmin and get cluster"
shell: mysqlsh --uri admin#host1 -p'pass' --execute='var cluster=dba.getCluster('cluster')'
and then:
- name: "connect as clusteradmin and check status of cluster"
shell: mysqlsh --uri admin#host1 -p'pass' --execute='cluster.status()'
In Kubuntu 18 I create docker for laravel 6 app with mysql defined :
mysql:
container_name: "vanilla-crm-db"
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: "MYSQL_ROOT_PASSWORD"
MYSQL_DATABASE: "vanilla-crm-dev"
MYSQL_USER: "MYSQL_USER"
MYSQL_PASSWORD: "MYSQL_PASSWORD"
ports:
- "3330:3306"
volumes:
- "./docker/mysql/data:/var/lib/mysql"
and it works for me.
I use MySql Workbench 6.3 for db access.
Text I make http tests and I need to create new database for this and load dump of my database in it.
Name of this database is wriiten in
config/database.php under 'mysql_testing' block
I open Workbench and try to create new database : https://prnt.sc/unjie8
But I do not find “Create database” option, But I see “Create new schema” option
and in sql-statement preview I see command
CREATE SCHEMA `vanilla-crm-testing` ;
I expected
Create database ...
command.
Is it the same?
and error next:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE SCHEMA `vanilla-crm-testing` ;
ERROR 1044: Access denied for user 'vanilla-crm-usr'#'%' to database 'vanilla-crm-testing'
SQL Statement:
CREATE SCHEMA `vanilla-crm-testing`
Which is valid way to create testing database?
UPDATED :
I tried to create new database in mysql console, like:
mysql
CREATE DATABASE vanilla-crm-testing;
but I got error in docker command line:
$ docker-compose exec app bash
root#09649d3a2b81:/app# mysql
bash: mysql: command not found
My docker app has in Dockerfile :
FROM php:7.3-apache
...
RUN apt-get update && apt-get install --no-install-recommends -y \
apt-utils ghostscript jq libicu-dev libmagick++-dev libpq-dev libfreetype6-dev libjpeg62-turbo-dev zlib1g-dev libzip-dev git zip && \
docker-php-ext-install intl && \
docker-php-ext-install opcache && \
docker-php-ext-install pdo_mysql && \
and no more mysql commands. Are there some more packages I need to install in Dockerfile to have mysql console under docker?
UPDATED # 2:
I enter the bash with command :
docker-compose exec mysql bash
root#f216ef80c104:/# uname -a
Linux f216ef80c104 4.15.0-118-generic #119-Ubuntu SMP Tue Sep 8 12:30:01 UTC 2020 x86_64 GNU/Linux
Where mysql is container_name in docker-compose.yml
Usually I enter mysql console with command:
mysql -u root -h localhost -p
But which must be format of this command in the docker console?
I tried several ways and failed...
UPDATED # 3:
I installed DBeaver Version 7.2.1.202009201907 and logged into
my database and tried to create new database for testing. I got error:
https://prnt.sc/uol0z7
How to fix it ?
Have I to add some more right my mysql container definitions?
Thanks!
To answer your question seems your main problem is you are trying to create a new test database but you are login with a non-root user. Non-root user has very little permission to do some operations. That's why you got that error message and can't create a new database. To solve this, try to log-in with the root user and make sure your Dbeaver config is correct:
host: 127.0.0.1
port: <Exposed MySQL port based on your docker-compose, e.g: 3330>
username: root
password: <MYSQL_ROOT_PASSWORD from your docker-compose>
With the root user, you should be able to create any new database. Another thing, if you wanted to connect via mysql cli command, make sure you also provide the correct port to the docker container. The command should be like this:
mysql -u root -h 127.0.0.1 -P <Exposed MySQL port based on your docker-compose, e.g: 3330> -p
Hope it helps and solved your problem. :)
I'm getting ERROR 2002 (HY000): Can't connect to local MySQL when trying to execute a mysql command during my CI process.
Here is my bitbucket-pipelines.yml file
image: theotherperson/php-ci:5.6
pipelines:
default:
- step:
caches:
- composer
script:
- apt-get update && apt-get install -y unzip mysql-client
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install --no-scripts --no-plugins
- cp test-assets/vhosts/000-default.conf /etc/apache2/sites-enabled/000-default.conf
- cp test-assets/hosts/hosts /etc/hosts
- rm /var/www/html/index.html
- cp -R $BITBUCKET_CLONE_DIR /var/www/html
- service apache2 restart
- mysql -u root -p$MYSQL_ROOT_PASSWORD -e "test < $BITBUCKET_CLONE_DIR/data/test/test.sql"
- phantomjs --webdriver=4444 &
- vendor/bin/behat -p test_behat
services:
- mysql
definitions:
services:
mysql:
image: mysql
environment:
MYSQL_DATABASE: 'test'
MYSQL_ROOT_PASSWORD: 'mypassword'
And here is the error:
+ mysql -u root -p$MYSQL_ROOT_PASSWORD -e "test < $BITBUCKET_CLONE_DIR/data/test/test.sql"
Enter password: ERROR 2002 (HY000): Can't connect to local MySQL
What do I need to do to be able to access mysql from this command line?
look at their documentation
Host name: 127.0.0.1 (avoid using localhost, as some clients will attempt to connect via a local "Unix socket", which will not work in Pipelines)
In CircleCI 1.0, we used to have this code:
services:
- mysql
database:
override:
- mysql -u ubuntu circle_test < scripts/db/mysql_setup.sql
Now we are migrating to 2.0 and mysql is a docker instead of a service:
version: 2
jobs:
build:
docker:
- image: circleci/openjdk:8-jdk
- image: redis:3.2.11
- image: donilan/mysql-utf8mb4
We need to prepare our database, how can I execute this mysql -u ubuntu circle_test < scripts/db/mysql_setup.sql when mysql is dockerized?
I ended up connecting to database directly from our app.
There is another approach though (that I was not successful with):
Download mysql-client: apt-get mysql-client
mysql -h 127.0.0.1 -u root OR maybe using sockets mysql -u root
If anyone is successful, let me know please!