Mysql / Mariadb


On-Board


Change Password: SET PASSWORD FOR 'sharqa'@'localhost' = PASSWORD('linux@'); Text password, not hash;
Grant Privileges GRANT ALL PRIVILEGES ON *.* to 'alexander'@'localhost' WITH GRANT OPTION;
Display Users select * from mysql.user; OR select Host,User,Password from mysql.user;
Grants from host % vs localhost Hi I googled if I needed a separate user with grants from localhost even if % was already created. Posts from 10 years ago said yes as localhost used a different socket. Is this still the case? I prefer to keep mysql.user as uncluttered as possible. Thanks! Sorry if this has been asked before. Answer Answered by Ian Gilfillan in this comment. Separate users are not needed. Note however that some setups create anonymous users, which can cause failures to log in as expected from localhost. See Troubleshooting Connection Issues#localhost and % ↑ Frequently Asked Questions ↑
Comments 2 years, 7 months ago Ian Gilfillan Re: Grants from host % vs localhost [Answer] Separate users are not needed. Note however that some setups create anonymous users, which can cause failures to log in as expected from localhost. See Troubleshooting Connection Issues#localhost and % 2 years, 8 months ago Daniel Black Re: Grants from host % vs localhost Yes, separate users are still needed. To simplify grants you can use a role and grant the role to both users. Then all database grants can be placed on the role and equally apply to the foo@% and foo@localhost users. MariaDB [(none)]> create user foo identified by 'foo'; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> create user foo@localhost identified by 'foo'; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> create role foo_role; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> grant foo_role to foo; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> grant foo_role to foo@localhost; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> create database fdatabase; Query OK, 1 row affected (0.001 sec) MariaDB [(none)]> grant all on fdatabase.* to foo_role; Query OK, 0 rows affected (0.001 sec) 2 years, 8 months ago Anel Husakovic Re: Grants from host % vs localhost Hi, It would be much easier if you could pass the links and provide an example what you would like to achieve, but let me see if I understood you correctly. When you grant privileges to the user, you can specify to which user@host pair it should apply. Let's look an example. Let's create 2 users and look the privs: MariaDB [(none)]> create user foo@'%' identified by 'foo1'; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]> create user foo@'localhost' identified by 'foo2'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | foo | % | | root | 127.0.0.1 | | root | ::1 | | | anel | | root | anel | | | localhost | | foo | localhost | | root | localhost | +------+-----------+ 8 rows in set (0.00 sec) MariaDB [(none)]> show grants for foo; +---------------------------------------- - -------------------------+ | Grants for foo@% | +---------------------------------------- - -------------------------+ | GRANT USAGE ON *.* TO 'foo'@'%' IDENTIFIED BY PASSWORD '*6EAC5249D53050AE649BDB0CC4B85D1AE90CA16E' | +---------------------------------------- - -------------------------+ 1 row in set (0.00 sec) MariaDB [(none)]> show grants for foo@localhost; +---------------------------------------- - ----------------------------+ | Grants for foo@localhost | +------------------------------------- - ------------------------------+ | GRANT USAGE ON *.* TO 'foo'@'localhost' IDENTIFIED BY PASSWORD '*3A4DD2A68933D2F38A899323EE91A540A7557C43' | +----------------------------------- - -------------------------------+ 1 row in set (0.00 sec) Now we can specify different privileges for them: MariaDB [(none)]> grant select on mysql.* to foo@localhost; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> grant select,update on mysql.* to foo@'%'; Query OK, 0 rows affected (0.00 sec) And let's verify they are different: MariaDB [(none)]> show grants for foo; +-------------------------------------- - -------------------------+ | Grants for foo@% | +---------------------------------------- - -------------------------+ | GRANT USAGE ON *.* TO 'foo'@'%' IDENTIFIED BY PASSWORD '*6EAC5249D53050AE649BDB0CC4B85D1AE90CA16E' | | GRANT SELECT, UPDATE ON `mysql`.* TO 'foo'@'%' | +------------------------------------------ - -------------------------+ 2 rows in set (0.00 sec) MariaDB [(none)]> show grants for foo@localhost; +-------------------------------------------- - -----------------------+ | Grants for foo@localhost | +----------------------------------------------- - -------------------+ | GRANT USAGE ON *.* TO 'foo'@'localhost' IDENTIFIED BY PASSWORD '*3A4DD2A68933D2F38A899323EE91A540A7557C43' | | GRANT SELECT ON `mysql`.* TO 'foo'@'localhost' | +------------------------------- - -------------------------------+ I guess this was what you are looking for? B
Special Characters Special Characters From: https://mariadb.com/kb/en/meta/editing-help-special-characters/ HTML ASCII Characters Several HTML ASCII characters can be created in our documentation with Creole Wiki by using the <<entity>> macro. Just type the entity name, without the leading '&' and trailing ';'. For example, to create an em-dash you would enter *lt;<entity>>mdash<</entity>> Below is a list characters that are allowed in Creole Wiki, along with their HTML equivalent and how to format them in Creole Wiki:
CharacterHTMLCreole WikiDescription
&mdash;<<entity>>mdash<</entity>>Em-Dash
&ndash;<<entity>>ndash<</entity>>En-Dash
&bull;<<entity>>bull<</entity>>Bullet
·&middot;<<entity>>middot<</entity>>Middle Dot
&lsquo;<<entity>>lsquo<</entity>>Left Single-Quote
&rsquo;<<entity>>rsquo<</entity>>Right Single-Quote
&ldquo;<<entity>>ldquo<</entity>>Left Double-Quote
&rdquo;<<entity>>rdquo<</entity>>Right Double-Quote
&nbsp;<<entity>>nbsp<</entity>>Non-Breaking Space
&&&<<entity>>&<</entity>>Ampersand
®&reg;<<entity>>reg<</entity>>Registered Mark
©&copy;<<entity>>copy<</entity>>Copyright Mark
&trade;<<entity>>trade<</entity>>Trade Mark
&para;<<entity>>para<</entity>>Paragraph
§&sect;<<entity>>sect<</entity>>Section

String Literals From: https://mariadb.com/kb/en/string-literals/ Strings are sequences of characters and are enclosed with quotes. The syntax is: [_charset_name]'string' [COLLATE collation_name] For example: 'The MariaDB Foundation' _utf8 'Foundation' COLLATE utf8_unicode_ci; Strings can either be enclosed in single quotes or in double quotes (the same character must be used to both open and close the string). The ANSI SQL-standard does not permit double quotes for enclosing strings, and although MariaDB does by default, if the MariaDB server has enabled the ANSI_QUOTES_SQL SQL_MODE, double quotes will be treated as being used for identifiers instead of strings. Strings that are next to each other are automatically concatenated. For example: 'The ' 'MariaDB ' 'Foundation' and 'The MariaDB Foundation' are equivalent. The \ (backslash character) is used to escape characters (unless the SQL_MODE hasn't been set to NO_BACKSLASH_ESCAPES). For example: 'MariaDB's new features' is not a valid string because of the single quote in the middle of the string, which is treated as if it closes the string, but is actually meant as part of the string, an apostrophe. The backslash character helps in situations like this: 'MariaDB\'s new features' is now a valid string, and if displayed, will appear without the backslash. SELECT 'MariaDB\'s new features'; +------------------------+ | MariaDB's new features | +------------------------+ | MariaDB's new features | +------------------------+ Another way to escape the quoting character is repeating it twice: SELECT 'I''m here', """Double"""; +----------+----------+ | I'm here | "Double" | +----------+----------+ | I'm here | "Double" | +----------+----------+
Escape Sequences There are other escape sequences also. Here is a full list:
Escape sequenceCharacter
\0ASCII NUL (0x00).
\'Single quote (“'”).
\"Double quote (“"”).
\bBackspace.
\nNewline, or linefeed,.
\rCarriage return.
\tTab.
\ZASCII 26 (Control+Z). See note following the table.
\\Backslash (“\”).
\%“%” character. See note following the table.
\_A “_” character. See note following the table.
Escaping the % and _ (Underscore) characters can be necessary when using the LIKE operator, which treats them as special characters. The ASCII 26 character (\Z) needs to be escaped when included in a batch file which needs to be executed in Windows. The reason is that ASCII 26, in Windows, is the end of file (EOF). Backslash (\) , if not used as an escape character, must always be escaped. When followed by a character that is not in the above table, backslashes will simply be ignored.
Regular Expressions Overview From: https://mariadb.com/kb/en/regular-expressions-overview/ Contents
  1. Special Characters
    1. ^
    2. $
    3. .
    4. *
    5. +
    6. ?
    7. ()
    8. {}
    9. []
      1. ^
        1. Word boundaries
        2. Character Classes
        3. Character Names
      2. Combining
      3. Escaping
      Regular Expressions allow MariaDB to perform complex pattern matching on a string. In many cases, the simple pattern matching provided by LIKE is sufficient. LIKE performs two kinds of matches:
      • _ - the underscore, matching a single character
      • % - the percentage sign, matching any number of characters.
      In other cases you may need more control over the returned matches, and will need to use regular expressions. Until MariaDB 10.0.5, MariaDB used the POSIX 1003.2 compliant regular expression library. The current PCRE library is mostly backwards compatible with what is described below - see the PCRE Regular Expressions article for the enhancements made in 10.0.5. Regular expression matches are performed with the REGEXP function. RLIKE is a synonym for REGEXP. Comparisons are performed on the byte value, so characters that are treated as equivalent by a collation, but do not have the same byte-value, such as accented characters, could evaluate as unequal. Without any special characters, a regular expression match is true if the characters match. The match is case-insensitive, except in the case of BINARY strings. SELECT 'Maria' REGEXP 'Maria'; +------------------------+ | 'Maria' REGEXP 'Maria' | +------------------------+ | 1 | +------------------------+ SELECT 'Maria' REGEXP 'maria'; +------------------------+ | 'Maria' REGEXP 'maria' | +------------------------+ | 1 | +------------------------+ SELECT BINARY 'Maria' REGEXP 'maria'; +-------------------------------+ | BINARY 'Maria' REGEXP 'maria' | +-------------------------------+ | 0 | +-------------------------------+ Note that the word being matched must match the whole pattern: SELECT 'Maria' REGEXP 'Mari'; +-----------------------+ | 'Maria' REGEXP 'Mari' | +-----------------------+ | 1 | +-----------------------+ SELECT 'Mari' REGEXP 'Maria'; +-----------------------+ | 'Mari' REGEXP 'Maria' | +-----------------------+ | 0 | +-----------------------+ The first returns true because the pattern "Mari" exists in the expression "Maria". When the order is reversed, the result is false, as the pattern "Maria" does not exist in the expression "Mari" A match can be performed against more than one word with the | character. For example: SELECT 'Maria' REGEXP 'Monty|Maria'; +------------------------------+ | 'Maria' REGEXP 'Monty|Maria' | +------------------------------+ | 1 | +------------------------------+ Special Characters The above examples introduce the syntax, but are not very useful on their own. It's the special characters that give regular expressions their power. ^ ^ matches the beginning of a string (inside square brackets it can also mean NOT - see below): SELECT 'Maria' REGEXP '^Ma'; +----------------------+ | 'Maria' REGEXP '^Ma' | +----------------------+ | 1 | +----------------------+ $ $ matches the end of a string: SELECT 'Maria' REGEXP 'ia$'; +----------------------+ | 'Maria' REGEXP 'ia$' | +----------------------+ | 1 | +----------------------+ . . matches any single character: SELECT 'Maria' REGEXP 'Ma.ia'; +------------------------+ | 'Maria' REGEXP 'Ma.ia' | +------------------------+ | 1 | +------------------------+ SELECT 'Maria' REGEXP 'Ma..ia'; +-------------------------+ | 'Maria' REGEXP 'Ma..ia' | +-------------------------+ | 0 | +-------------------------+ * x* matches zero or more of a character x. In the examples below, it's the r character. SELECT 'Maria' REGEXP 'Mar*ia'; +-------------------------+ | 'Maria' REGEXP 'Mar*ia' | +-------------------------+ | 1 | +-------------------------+ SELECT 'Maia' REGEXP 'Mar*ia'; +------------------------+ | 'Maia' REGEXP 'Mar*ia' | +------------------------+ | 1 | +------------------------+ SELECT 'Marrria' REGEXP 'Mar*ia'; +---------------------------+ | 'Marrria' REGEXP 'Mar*ia' | +---------------------------+ | 1 | +---------------------------+ + x+ matches one or more of a character x. In the examples below, it's the r character. SELECT 'Maria' REGEXP 'Mar+ia'; +-------------------------+ | 'Maria' REGEXP 'Mar+ia' | +-------------------------+ | 1 | +-------------------------+ SELECT 'Maia' REGEXP 'Mar+ia'; +------------------------+ | 'Maia' REGEXP 'Mar+ia' | +------------------------+ | 0 | +------------------------+ SELECT 'Marrria' REGEXP 'Mar+ia'; +---------------------------+ | 'Marrria' REGEXP 'Mar+ia' | +---------------------------+ | 1 | +---------------------------+ ? x? matches zero or one of a character x. In the examples below, it's the r character. SELECT 'Maria' REGEXP 'Mar?ia'; +-------------------------+ | 'Maria' REGEXP 'Mar?ia' | +-------------------------+ | 1 | +-------------------------+ SELECT 'Maia' REGEXP 'Mar?ia'; +------------------------+ | 'Maia' REGEXP 'Mar?ia' | +------------------------+ | 1 | +------------------------+ SELECT 'Marrria' REGEXP 'Mar?ia'; +---------------------------+ | 'Marrria' REGEXP 'Mar?ia' | +---------------------------+ | 0 | +---------------------------+ () (xyz) - combine a sequence, for example (xyz)+ or (xyz)* SELECT 'Maria' REGEXP '(ari)+'; +-------------------------+ | 'Maria' REGEXP '(ari)+' | +-------------------------+ | 1 | +-------------------------+ {} x{n} and x{m,n} This notation is used to match many instances of the x. In the case of x{n} the match must be exactly that many times. In the case of x{m,n}, the match can occur from m to n times. For example, to match zero or one instance of the string ari (which is identical to (ari)?), the following can be used: SELECT 'Maria' REGEXP '(ari){0,1}'; +-----------------------------+ | 'Maria' REGEXP '(ari){0,1}' | +-----------------------------+ | 1 | +-----------------------------+ [] [xy] groups characters for matching purposes. For example, to match either the p or the r character: SELECT 'Maria' REGEXP 'Ma[pr]ia'; +---------------------------+ | 'Maria' REGEXP 'Ma[pr]ia' | +---------------------------+ | 1 | +---------------------------+ The square brackets also permit a range match, for example, to match any character from a-z, [a-z] is used. Numeric ranges are also permitted. SELECT 'Maria' REGEXP 'Ma[a-z]ia'; +----------------------------+ | 'Maria' REGEXP 'Ma[a-z]ia' | +----------------------------+ | 1 | +----------------------------+ The following does not match, as r falls outside of the range a-p. SELECT 'Maria' REGEXP 'Ma[a-p]ia'; +----------------------------+ | 'Maria' REGEXP 'Ma[a-p]ia' | +----------------------------+ | 0 | +----------------------------+ ^ The ^ character means does NOT match, for example: SELECT 'Maria' REGEXP 'Ma[^p]ia'; +---------------------------+ | 'Maria' REGEXP 'Ma[^p]ia' | +---------------------------+ | 1 | +---------------------------+ SELECT 'Maria' REGEXP 'Ma[^r]ia'; +---------------------------+ | 'Maria' REGEXP 'Ma[^r]ia' | +---------------------------+ | 0 | +---------------------------+ The [ and ] characters on their own can be literally matched inside a [] block, without escaping, as long as they immediately match the opening bracket: SELECT '[Maria' REGEXP '[[]'; +-----------------------+ | '[Maria' REGEXP '[[]' | +-----------------------+ | 1 | +-----------------------+ SELECT '[Maria' REGEXP '[]]'; +-----------------------+ | '[Maria' REGEXP '[]]' | +-----------------------+ | 0 | +-----------------------+ SELECT ']Maria' REGEXP '[]]'; +-----------------------+ | ']Maria' REGEXP '[]]' | +-----------------------+ | 1 | +-----------------------+ SELECT ']Maria' REGEXP '[]a]'; +------------------------+ | ']Maria' REGEXP '[]a]' | +------------------------+ | 1 | +------------------------+ Incorrect order, so no match: SELECT ']Maria' REGEXP '[a]]'; +------------------------+ | ']Maria' REGEXP '[a]]' | +------------------------+ | 0 | +------------------------+ The - character can also be matched in the same way: SELECT '-Maria' REGEXP '[1-10]'; +--------------------------+ | '-Maria' REGEXP '[1-10]' | +--------------------------+ | 0 | +--------------------------+ SELECT '-Maria' REGEXP '[-1-10]'; +---------------------------+ | '-Maria' REGEXP '[-1-10]' | +---------------------------+ | 1 | +---------------------------+ Word boundaries The :<: and :>: patterns match the beginning and the end of a word respectively. For example: SELECT 'How do I upgrade MariaDB?' REGEXP '[[:<:]]MariaDB[[:>:]]'; +------------------------------------------------------------+ | 'How do I upgrade MariaDB?' REGEXP '[[:<:]]MariaDB[[:>:]]' | +------------------------------------------------------------+ | 1 | +------------------------------------------------------------+ SELECT 'How do I upgrade MariaDB?' REGEXP '[[:<:]]Maria[[:>:]]'; +----------------------------------------------------------+ | 'How do I upgrade MariaDB?' REGEXP '[[:<:]]Maria[[:>:]]' | +----------------------------------------------------------+ | 0 | +----------------------------------------------------------+ Character Classes There are a number of shortcuts to match particular preset character classes. These are matched with the [:character_class:] pattern (inside a [] set). The following character classes exist: Character Class Description alnum Alphanumeric alpha Alphabetic blank Whitespace cntrl Control characters digit Digits graph Graphic characters lower Lowercase alphabetic print Graphic or space characters punct Punctuation space Space, tab, newline, and carriage return upper Uppercase alphabetic xdigit Hexadecimal digit For example: SELECT 'Maria' REGEXP 'Mar[[:alnum:]]*'; +--------------------------------+ | 'Maria' REGEXP 'Mar[:alnum:]*' | +--------------------------------+ | 1 | +--------------------------------+ Remember that matches are by default case-insensitive, unless a binary string is used, so the following example, specifically looking for an uppercase, counter-intuitively matches a lowercase character: SELECT 'Mari' REGEXP 'Mar[[:upper:]]+'; +---------------------------------+ | 'Mari' REGEXP 'Mar[[:upper:]]+' | +---------------------------------+ | 1 | +---------------------------------+ SELECT BINARY 'Mari' REGEXP 'Mar[[:upper:]]+'; +----------------------------------------+ | BINARY 'Mari' REGEXP 'Mar[[:upper:]]+' | +----------------------------------------+ | 0 | +----------------------------------------+ Character Names There are also number of shortcuts to match particular preset character names. These are matched with the [.character.] pattern (inside a [] set). The following character classes exist: Name Character NUL 0 SOH 001 STX 002 ETX 003 EOT 004 ENQ 005 ACK 006 BEL 007 alert 007 BS 010 backspace '\b' HT 011 tab '\t' LF 012 newline '\n' VT 013 vertical-tab '\v' FF 014 form-feed '\f' CR 015 carriage-return '\r' SO 016 SI 017 DLE 020 DC1 021 DC2 022 DC3 023 DC4 024 NAK 025 SYN 026 ETB 027 CAN 030 EM 031 SUB 032 ESC 033 IS4 034 FS 034 IS3 035 GS 035 IS2 036 RS 036 IS1 037 US 037 space ' ' exclamation-mark '!' quotation-mark '"' number-sign '#' dollar-sign '$' percent-sign '%' ampersand '&' apostrophe '\'' left-parenthesis '(' right-parenthesis ')' asterisk '*' plus-sign '+' comma ',' hyphen '-' hyphen-minus '-' period '.' full-stop '.' slash '/' solidus '/' zero '0' one '1' two '2' three '3' four '4' five '5' six '6' seven '7' eight '8' nine '9' colon ':' semicolon ';' less-than-sign '<' equals-sign '=' greater-than-sign '>' question-mark '?' commercial-at '@' left-square-bracket '[' backslash ' ' reverse-solidus ' ' right-square-bracket ']' circumflex '^' circumflex-accent '^' underscore '_' low-line '_' grave-accent '`' left-brace '{' left-curly-bracket '{' vertical-line '|' right-brace '}' right-curly-bracket '}' tilde '' DEL 177 For example: SELECT '|' REGEXP '[[.vertical-line.]]'; +----------------------------------+ | '|' REGEXP '[[.vertical-line.]]' | +----------------------------------+ | 1 | +----------------------------------+ Combining The true power of regular expressions is unleashed when the above is combined, to form more complex examples. Regular expression's reputation for complexity stems from the seeming complexity of multiple combined regular expressions, when in reality, it's simply a matter of understanding the characters and how they apply: The first example fails to match, as while the Ma matches, either i or r only matches once before the ia characters at the end. SELECT 'Maria' REGEXP 'Ma[ir]{2}ia'; +------------------------------+ | 'Maria' REGEXP 'Ma[ir]{2}ia' | +------------------------------+ | 0 | +------------------------------+ This example matches, as either i or r match exactly twice after the Ma, in this case one r and one i. SELECT 'Maria' REGEXP 'Ma[ir]{2}'; +----------------------------+ | 'Maria' REGEXP 'Ma[ir]{2}' | +----------------------------+ | 1 | +----------------------------+ Escaping With the large number of special characters, care needs to be taken to properly escape characters. Two backslash characters, (one for the MariaDB parser, one for the regex library), are required to properly escape a character. For example: To match the literal (Ma: SELECT '(Maria)' REGEXP '(Ma'; ERROR 1139 (42000): Got error 'parentheses not balanced' from regexp SELECT '(Maria)' REGEXP '\(Ma'; ERROR 1139 (42000): Got error 'parentheses not balanced' from regexp SELECT '(Maria)' REGEXP '\\(Ma'; +--------------------------+ | '(Maria)' REGEXP '\\(Ma' | +--------------------------+ | 1 | +--------------------------+ To match r+: The first two examples are incorrect, as they match r one or more times, not r+: SELECT 'Mar+ia' REGEXP 'r+'; +----------------------+ | 'Mar+ia' REGEXP 'r+' | +----------------------+ | 1 | +----------------------+ SELECT 'Maria' REGEXP 'r+'; +---------------------+ | 'Maria' REGEXP 'r+' | +---------------------+ | 1 | +---------------------+ SELECT 'Maria' REGEXP 'r\\+'; +-----------------------+ | 'Maria' REGEXP 'r\\+' | +-----------------------+ | 0 | +-----------------------+ SELECT 'Maria' REGEXP 'r+'; +---------------------+ | 'Maria' REGEXP 'r+' | +---------------------+ | 1 | +---------------------+ ↑ Regular Expressions Functions ↑ PCRE - Perl Compatible Regular Expressions → Comments Include Archived 4 years, 8 months ago Daniel Tubbs Re: Regular Expressions Overview According to this bug: https://jira.mariadb.org/browse/MDEV-7127 The Character Names only work for collating elements. There are also number of shortcuts to match particular preset character names. These are matched with the [.character.] pattern (inside a [] set). Adding a note to that section would be helpful.