想不进入 mysql 容器内执行 sql 语句(命令行执行是没问题的):
CREATE USER 'root'@'%' IDENTIFIED BY 'abcd1234';
GRANT ALL ON *.* TO 'root'@'%';
宿主机执行:
docker exec -it mysql bash -c 'mysql <<< "CREATE USER 'root'@'%' IDENTIFIED BY 'abcd1234';GRANT ALL ON *.* TO 'root'@'%';"'
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '% IDENTIFIED BY abcd1234' at line 1
报语法错误,引号太多了,我仔细看,感觉也没毛病,但确实是执行不了,各位帮忙看一下,多谢了
1
css3 OP %前加了\也不行😭
|
2
MrUser 2020-02-05 20:53:20 +08:00
试试这个:
``` docker exec -it mysql bash -c 'mysql <<< "CREATE USER \'root\'@\'%\' IDENTIFIED BY \'abcd1234\';GRANT ALL ON *.* TO \'root\'@\'%\';"' ``` |
4
GM 2020-02-05 21:14:12 +08:00
这种容易由于转义导致的问题,建议弄个 sql 文件,然后 mysql < script.sql 这样使用。
|
7
hcymk2 2020-02-05 21:56:39 +08:00
mysql -e
|
8
msg7086 2020-02-06 02:28:12 +08:00 1
'mysql <<< "CREATE USER '
root '@' % ' IDENTIFIED BY ' abcd1234 ';GRANT ALL ON *.* TO ' root '@' % ';"' 你单引号都没转义,就会被 bash 转义,然后就会变成这样。 我是建议你最外层用双引号,这样就只要转义一对双引号就行了。 docker exec -it mysql bash -c "mysql <<< \"CREATE USER 'root'@'%' IDENTIFIED BY 'abcd1234';GRANT ALL ON *.* TO 'root'@'%';\" " 这样试试看。 |